Bug 221161

Summary: devel/staf: stafproc passes a revoked file descriptor to children
Product: Ports & Packages Reporter: Alan Somers <asomers>
Component: Individual Port(s)Assignee: Alan Somers <asomers>
Status: Closed FIXED    
Severity: Affects Some People CC: skozlov
Priority: --- Keywords: needs-qa
Version: LatestFlags: bugzilla: maintainer-feedback? (skozlov)
koobs: merge-quarterly?
Hardware: Any   
OS: Any   
Attachments:
Description Flags
Close stdin when starting stafproc koobs: maintainer-approval+

Description Alan Somers freebsd_committer freebsd_triage 2017-08-01 22:11:32 UTC
The "staf local process start ..." command causes stafproc to fork and exec a new process.  Depending on the arguments, it may pass its own stdin to the child, unmodified.  staf's RC script uses daemon(8) to detach stafproc from its controlling terminal, but it does nothing to stdin.  The result is that stafproc has a stdin in the revoked state, which it passes to child processes.  Some programs can't handle revoked file descriptors, because they return EBADF despite being opened.  See the file descriptors in the "x" state in the listing below:

# procstat -f 1319
  PID COMM                FD T V FLAGS    REF  OFFSET PRO NAME        
 1319 STAFProc          text v r r-------   -       - -   /usr/local/bin/STAFProc
 1319 STAFProc           cwd v d r-------   -       - -   /                 
 1319 STAFProc          root v d r-------   -       - -   /                 
 1319 STAFProc             0 v x rw------  74   34840 -   -                 
 1319 STAFProc             1 v r -wa-----   6    1825 -   -                 
 1319 STAFProc             2 v r -wa-----   6    1825 -   -                 
 1319 STAFProc             3 v x rw------  74   34840 -   -                 
 1319 STAFProc             4 v r -wa-----   6    1825 -   -                 
 1319 STAFProc             5 v r -wa-----   6    1825 -   -                 
 1319 STAFProc             6 v r rw------   1       0 -   -                 
 1319 STAFProc             7 v r rw------   1       0 -   -                 
 1319 STAFProc             8 s - rw------   2       0 UDS /tmp/STAFIPC_STAF
 1319 STAFProc             9 s - rw------   1       0 TCP 0.0.0.0:6550 0.0.0.0:0
 1319 STAFProc            11 s - rw------   1       0 TCP 0.0.0.0:6500 0.0.0.0:0
 1319 STAFProc            12 ? - --------   2       0 -   - 


The easiest solution is to close stdin in stafproc's RC script.
Comment 1 Alan Somers freebsd_committer freebsd_triage 2017-08-01 22:20:59 UTC
Created attachment 184930 [details]
Close stdin when starting stafproc
Comment 2 Kubilay Kocak freebsd_committer freebsd_triage 2017-08-02 04:33:41 UTC
Comment on attachment 184930 [details]
Close stdin when starting stafproc

Approved by: portmgr (blanket)
Comment 3 Alan Somers freebsd_committer freebsd_triage 2017-08-02 19:50:06 UTC
koobs, what QA would you like to see?
Comment 4 commit-hook freebsd_committer freebsd_triage 2017-08-03 15:21:14 UTC
A commit references this bug:

Author: asomers
Date: Thu Aug  3 15:20:17 UTC 2017
New revision: 447227
URL: https://svnweb.freebsd.org/changeset/ports/447227

Log:
  devel/staf: stafproc passes a revoked file descriptor to children

  staf's rc script uses daemon(8) to detach stafproc from its controlling
  terminal, but it does nothing to stdin.  The result is that stafproc has a
  stdin in the revoked state, which it passes to child processes.  Some
  programs can't handle revoked file descriptors, because they return EBADF
  despite being opened.

  PR:		221161
  Approved by:	koobs (portmgr)
  MFH:		2017Q3
  Sponsored by:	Spectra Logic Corp

Changes:
  head/devel/staf/Makefile
  head/devel/staf/files/stafproc.in