Author: kevans
Date: Wed Jul 31 15:16:51 2019
New Revision: 350464
URL: https://svnweb.freebsd.org/changeset/base/350464

Log:
  kern_shm_open: push O_CLOEXEC into caller control
  
  The motivation for this change is to allow wrappers around shm to be written
  that don't set CLOEXEC. kern_shm_open currently accepts O_CLOEXEC but sets
  it unconditionally. kern_shm_open is used by the shm_open(2) syscall, which
  is mandated by POSIX to set CLOEXEC, and CloudABI's sys_fd_create1().
  Presumably O_CLOEXEC is intended in the latter caller, but it's unclear from
  the context.
  
  sys_shm_open() now unconditionally sets O_CLOEXEC to meet POSIX
  requirements, and a comment has been dropped in to kern_fd_open() to explain
  the situation and add a pointer to where O_CLOEXEC setting is maintained for
  shm_open(2) correctness. CloudABI's sys_fd_create1() also unconditionally
  sets O_CLOEXEC to match previous behavior.
  
  This also has the side-effect of making flags correctly reflect the
  O_CLOEXEC status on this fd for the rest of kern_shm_open(), but a
  glance-over leads me to believe that it didn't really matter.
  
  Reviewed by:  kib, markj
  MFC after:    1 week
  Differential Revision:        https://reviews.freebsd.org/D21119

Modified:
  head/sys/compat/cloudabi/cloudabi_fd.c
  head/sys/kern/uipc_shm.c

Modified: head/sys/compat/cloudabi/cloudabi_fd.c
==============================================================================
--- head/sys/compat/cloudabi/cloudabi_fd.c      Wed Jul 31 05:38:39 2019        
(r350463)
+++ head/sys/compat/cloudabi/cloudabi_fd.c      Wed Jul 31 15:16:51 2019        
(r350464)
@@ -94,7 +94,8 @@ cloudabi_sys_fd_create1(struct thread *td,
        case CLOUDABI_FILETYPE_SHARED_MEMORY:
                cap_rights_init(&fcaps.fc_rights, CAP_FSTAT, CAP_FTRUNCATE,
                    CAP_MMAP_RWX);
-               return (kern_shm_open(td, SHM_ANON, O_RDWR, 0, &fcaps));
+               return (kern_shm_open(td, SHM_ANON, O_RDWR | O_CLOEXEC, 0,
+                   &fcaps));
        default:
                return (EINVAL);
        }

Modified: head/sys/kern/uipc_shm.c
==============================================================================
--- head/sys/kern/uipc_shm.c    Wed Jul 31 05:38:39 2019        (r350463)
+++ head/sys/kern/uipc_shm.c    Wed Jul 31 15:16:51 2019        (r350464)
@@ -729,7 +729,14 @@ kern_shm_open(struct thread *td, const char *userpath,
        fdp = td->td_proc->p_fd;
        cmode = (mode & ~fdp->fd_cmask) & ACCESSPERMS;
 
-       error = falloc_caps(td, &fp, &fd, O_CLOEXEC, fcaps);
+       /*
+        * shm_open(2) created shm should always have O_CLOEXEC set, as mandated
+        * by POSIX.  We allow it to be unset here so that an in-kernel
+        * interface may be written as a thin layer around shm, optionally not
+        * setting CLOEXEC.  For shm_open(2), O_CLOEXEC is set unconditionally
+        * in sys_shm_open() to keep this implementation compliant.
+        */
+       error = falloc_caps(td, &fp, &fd, flags & O_CLOEXEC, fcaps);
        if (error)
                return (error);
 
@@ -844,7 +851,8 @@ int
 sys_shm_open(struct thread *td, struct shm_open_args *uap)
 {
 
-       return (kern_shm_open(td, uap->path, uap->flags, uap->mode, NULL));
+       return (kern_shm_open(td, uap->path, uap->flags | O_CLOEXEC, uap->mode,
+           NULL));
 }
 
 int
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to