Adding FreeBSD's Capsicum security framework
Subsystems (61)
v2 cover Adding FreeBSD's Capsicum security framework
The last couple of versions of FreeBSD (9.x/10.x) have included the Capsicum security framework [1], which allows security-aware applications to sandbox themselves in a very fine-grained way. For example, OpenSSH now (>= 6.5) uses Capsicum in its FreeBSD version to restrict sshd's credentials checking process, to reduce the chances of credential leakage.
It would be good to have equivalent functionality in Linux, so I've been working on getting the Capsicum framework running in the kernel, and I'd appreciate some feedback/opinions on the general approach.
I'm attaching a corresponding draft patchset for reference, but hopefully this cover email can cover the significant features to save everyone having to look through the code details. (It does mean this is a long email though -- apologies for that.)
1) Capsicum Capabilities
------------------------
The most significant aspect of Capsicum is associating *rights* with (some) file descriptors, so that the kernel only allows operations on an FD if the rights permit it. This allows userspace applications to sandbox themselves by tightly constraining what's allowed with both input and outputs; for example, tcpdump might restrict itself so it can only read from the network FD, and only write to stdout.
The kernel thus needs to police the rights checks for these file descriptors (referred to as 'Capsicum capabilities', completely different than POSIX.1e capabilities), and the best place to do this is at the points where a file descriptor from userspace is converted to a struct file * within the kernel.
[Policing the rights checks anywhere else, for example at the system call boundary, isn't a good idea because it opens up the possibility of time-of-check/time-of-use (TOCTOU) attacks [2] where FDs are changed (as openat/close/dup2 are allowed in capability mode) between the 'check' at syscall entry and the 'use' at fget() invocation.]
However, this does lead to quite an invasive change to the kernel -- every invocation of fget() or similar functions (fdget(), sockfd_lookup(), user_path_at(),...) needs to be annotated with the rights associated with the specific operations that will be performed on the struct file. There are ~100 such invocations that need annotation.
My current implementation approach is to use varargs variants of the fget() functions that include the required rights, varargs-macroed so that the only impact in a non-Capsicum build is the need to cope with an ERR_PTR on failure rather than just NULL:
#ifdef CONFIG_SECURITY_CAPSICUM #define fgetr(fd, ...) _fgetr((fd), __VA_ARGS__, CAP_LIST_END) /* + Other variants... */ #else #define fgetr(fd, ...) (fget(fd) ?: ERR_PTR(-EBADF)) /* + Other variants... */ #endif
For example, an existing chunk of code like:
SYSCALL_DEFINE1(fchdir, unsigned int, fd)
{
struct fd f = fdget_raw(fd);
struct inode *inode;
int error = -EBADF;
error = -EBADF;
if (!f.file)
goto out;
...might become:
SYSCALL_DEFINE1(fchdir, unsigned int, fd)
{
struct fd f = fdgetr_raw(fd, CAP_FCHDIR);
struct inode *inode;
int error = -EBADF;
if (IS_ERR(f.file)) {
error = PTR_ERR(f.file);
goto out;
}
...In a Capsicum build the fdgetr_raw() function performs rights checks (and potentially returns a new errno as ERR_PTR(-ENOTCAPABLE)), whereas in a non-Capsicum build the only change is that fdget_raw() returns ERR_PTR(-EBADF) rather than just NULL.
2) Capsicum Capabilities Data Structure
---------------------------------------
Internally, the rights associated with a Capsicum capability FD are stored in a special struct file wrapper. For a normal file, the rights check inside fget() falls through, but for a capability wrapper the rights in the wrapper are checked and (if capable) the underlying wrapped struct file is returned.
[This is approximately the implementation that was present in FreeBSD 9.x. For FreeBSD 10.x, the wrapper file was removed and the rights associated with a file descriptor are now stored in the fdtable. As that impacts memory use for all processes, whether Capsicum users or not, I've stuck with the FreeBSD 9.x approach.]
3) Allowing Capability Mode
---------------------------
Capsicum also includes 'capability mode', which locks down the available syscalls so the rights restrictions can't just be bypassed by opening new file descriptors. More precisely, capability mode prevents access to syscalls that access global namespaces, such as the filesystem or the IP:port space.
The existing seccomp-bpf functionality of the kernel is a good mechanism for implementing capability mode, but there are a few additional details that also need to be addressed.
a) The capability mode filter program needs to apply process-wide, not just to the current thread.
b) In capability mode, new files can still be opened with openat(2) but only if they are beneath an existing directory file descriptor.
c) In capability mode it should still be possible for a process to send signals to itself with kill(2)/tgkill(2).
This v2 patchset copes with these as follows:
a) Kees Cook's incoming seccomp(2) patchset covers thread synchronization of filters.
b) A new prctl(PR_SET_OPENAT_BENEATH) operation implicitly sets the O_BENEATH flag (see below) for all file-open operations for all threads of the current process, by adding a new openat_beneath flag in task_struct.
c) An extension to the seccomp_data structure that includes the current task's tid and tgid values allows for BPF programs that check a kill(2)/tgkill(2) argument against the current thread, in a manner that is robust against fork(2)/clone(2).
The combination of these features with the existing seccomp-bpf functionality gives the tools needed to implement capability mode.
4) New System Calls
-------------------
To allow userspace applications to access the Capsicum capability functionality, I'm proposing two new system calls: cap_rights_limit(2) and cap_rights_get(2). I guess these could potentially be implemented elsewhere (e.g. as fcntl(2) operations?) but the changes seem significant enough that new syscalls are warranted.
[FreeBSD 10.x actually includes six new syscalls for manipulating the rights associated with a Capsicum capability -- the capability rights can police that only specific fcntl(2) or ioctl(2) commands are allowed, and FreeBSD sets these with distinct syscalls.]
5) New openat(2) O_BENEATH Flag
-------------------------------
For Capsicum capabilities that are directory file descriptors, the Capsicum framework only allows openat(cap_dfd, path, ...) operations to work for files that are beneath the specified directory (and even that only when the directory FD has the CAP_LOOKUP right), rejecting paths that start with "/" or include "..". The same restriction applies process-wide for a process in capability mode.
As this seemed like functionality that might be more generally useful, I've implemented it independently as a new O_BENEATH flag for openat(2). The Capsicum code then always triggers the use of that flag when the dfd is a Capsicum capability, or when the prctl(2) command described above is in play.
[FreeBSD has the openat(2) relative-only behaviour for capability DFDs and processes in capability mode, but does not include the O_BENEATH flag.]
6) Patchset Notes
-----------------
I've appended the draft patchset (against v3.16-rc5) for the implementation of Capsicum capabilities, in case anyone wants to dive into the details.
However, I should point out that it might include some code that hasn't been compiled -- I attempted to change every fget() invocation I could find, even if it was for a build that I can't perform (but I have built allyesconfig on x86 & ARM).
Also, I've left a gap in the syscall and prctl(2) command numbering, to allow this to be merged on top of Kees Cook's seccomp(2) changes.
Regards,
David Drysdale
[1] http://www.cl.cam.ac.uk/research/security/capsicum/papers/2010usenix-security-capsicum-website.pdf
[2] http://www.watson.org/~robert/2007woot/
Changes since v1:
- removed gratuitous LSM hooks [Andy Lutomirski, Paul Moore]
- renamed O_BENEATH_ONLY to O_BENEATH [Christoph Hellwig]
- updated syscall numbers to allow for seccomp(2)
- added prctl(PR_SET_OPENAT_BENEATH) [Paolo Bonzini]
- added tid/tgid info to seccomp_data [Paolo Bonzini]
- update spacing for current checkpatch.pl
- [manpages] describe struct cap_rights [Andy Lutomirski]
- [manpages] clarify nioctl use [Andy Lutomirski]
- [manpages] clarify CAP_FCNTL use [Andy Lutomirski]
David Drysdale (11):
fs: add O_BENEATH flag to openat(2)
selftests: Add test of O_BENEATH & openat(2)
capsicum: rights values and structure definitions
capsicum: implement fgetr() and friends
capsicum: convert callers to use fgetr() etc
capsicum: implement sockfd_lookupr()
capsicum: convert callers to use sockfd_lookupr() etc
capsicum: invoke Capsicum on FD/file conversion
capsicum: add syscalls to limit FD rights
capsicum: prctl(2) to force use of O_BENEATH
seccomp: Add tgid and tid into seccomp_data
Documentation/security/capsicum.txt | 102 +++++++ arch/alpha/include/uapi/asm/fcntl.h | 1 + arch/alpha/kernel/osf_sys.c | 6 +- arch/ia64/kernel/perfmon.c | 54 ++-- arch/parisc/hpux/fs.c | 6 +- arch/parisc/include/uapi/asm/fcntl.h | 1 + arch/powerpc/kvm/powerpc.c | 4 +- arch/powerpc/platforms/cell/spu_syscalls.c | 15 +- arch/powerpc/platforms/cell/spufs/coredump.c | 2 + arch/sparc/include/uapi/asm/fcntl.h | 1 + arch/x86/syscalls/syscall_64.tbl | 2 + drivers/base/dma-buf.c | 6 +- drivers/block/loop.c | 14 +- drivers/block/nbd.c | 5 +- drivers/infiniband/core/ucma.c | 4 +- drivers/infiniband/core/uverbs_cmd.c | 6 +- drivers/infiniband/core/uverbs_main.c | 4 +- drivers/infiniband/hw/usnic/usnic_transport.c | 2 +- drivers/md/md.c | 8 +- drivers/scsi/iscsi_tcp.c | 2 +- drivers/staging/android/sync.c | 2 +- drivers/staging/lustre/lustre/llite/file.c | 6 +- drivers/staging/lustre/lustre/lmv/lmv_obd.c | 7 +- drivers/staging/lustre/lustre/mdc/lproc_mdc.c | 8 +- drivers/staging/lustre/lustre/mdc/mdc_request.c | 4 +- drivers/staging/usbip/stub_dev.c | 2 +- drivers/staging/usbip/vhci_sysfs.c | 2 +- drivers/vfio/pci/vfio_pci.c | 6 +- drivers/vfio/pci/vfio_pci_intrs.c | 6 +- drivers/vfio/vfio.c | 6 +- drivers/vhost/net.c | 8 +- drivers/video/fbdev/msm/mdp.c | 4 +- fs/aio.c | 37 ++- fs/autofs4/dev-ioctl.c | 16 +- fs/autofs4/inode.c | 4 +- fs/btrfs/ioctl.c | 20 +- fs/btrfs/send.c | 7 +- fs/cifs/ioctl.c | 6 +- fs/coda/inode.c | 4 +- fs/coda/psdev.c | 2 +- fs/compat.c | 18 +- fs/compat_ioctl.c | 14 +- fs/eventfd.c | 17 +- fs/eventpoll.c | 19 +- fs/ext4/ioctl.c | 6 +- fs/fcntl.c | 106 ++++++- fs/fhandle.c | 6 +- fs/file.c | 130 ++++++++ fs/fuse/inode.c | 10 +- fs/ioctl.c | 13 +- fs/locks.c | 11 +- fs/namei.c | 310 ++++++++++++++----- fs/ncpfs/inode.c | 5 +- fs/notify/dnotify/dnotify.c | 2 + fs/notify/fanotify/fanotify_user.c | 16 +- fs/notify/inotify/inotify_user.c | 12 +- fs/ocfs2/cluster/heartbeat.c | 8 +- fs/open.c | 46 +-- fs/proc/fd.c | 17 +- fs/proc/namespaces.c | 6 +- fs/read_write.c | 113 ++++--- fs/readdir.c | 18 +- fs/select.c | 11 +- fs/signalfd.c | 6 +- fs/splice.c | 34 ++- fs/stat.c | 10 +- fs/statfs.c | 8 +- fs/sync.c | 21 +- fs/timerfd.c | 40 ++- fs/utimes.c | 10 +- fs/xattr.c | 26 +- fs/xfs/xfs_ioctl.c | 14 +- include/linux/capsicum.h | 72 +++++ include/linux/file.h | 136 +++++++++ include/linux/namei.h | 10 + include/linux/net.h | 16 + include/linux/sched.h | 3 + include/linux/syscalls.h | 12 + include/uapi/asm-generic/errno.h | 3 + include/uapi/asm-generic/fcntl.h | 4 + include/uapi/linux/Kbuild | 1 + include/uapi/linux/capsicum.h | 343 +++++++++++++++++++++ include/uapi/linux/prctl.h | 14 + include/uapi/linux/seccomp.h | 10 + ipc/mqueue.c | 30 +- kernel/events/core.c | 14 +- kernel/module.c | 10 +- kernel/seccomp.c | 2 + kernel/sys.c | 33 +- kernel/sys_ni.c | 4 + kernel/taskstats.c | 4 +- kernel/time/posix-clock.c | 27 +- mm/fadvise.c | 7 +- mm/internal.h | 19 ++ mm/memcontrol.c | 12 +- mm/mmap.c | 7 +- mm/nommu.c | 9 +- mm/readahead.c | 6 +- net/9p/trans_fd.c | 10 +- net/bluetooth/bnep/sock.c | 2 +- net/bluetooth/cmtp/sock.c | 2 +- net/bluetooth/hidp/sock.c | 4 +- net/compat.c | 4 +- net/l2tp/l2tp_core.c | 11 +- net/l2tp/l2tp_core.h | 2 + net/sched/sch_atm.c | 2 +- net/socket.c | 207 ++++++++++--- net/sunrpc/svcsock.c | 4 +- security/Kconfig | 15 + security/Makefile | 2 +- security/capsicum-rights.c | 201 +++++++++++++ security/capsicum-rights.h | 10 + security/capsicum.c | 380 ++++++++++++++++++++++++ sound/core/pcm_native.c | 10 +- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/openat/.gitignore | 3 + tools/testing/selftests/openat/Makefile | 24 ++ tools/testing/selftests/openat/openat.c | 146 +++++++++ virt/kvm/eventfd.c | 6 +- virt/kvm/vfio.c | 12 +- 120 files changed, 2818 insertions(+), 533 deletions(-) create mode 100644 Documentation/security/capsicum.txt create mode 100644 include/linux/capsicum.h create mode 100644 include/uapi/linux/capsicum.h create mode 100644 security/capsicum-rights.c create mode 100644 security/capsicum-rights.h create mode 100644 security/capsicum.c create mode 100644 tools/testing/selftests/openat/.gitignore create mode 100644 tools/testing/selftests/openat/Makefile create mode 100644 tools/testing/selftests/openat/openat.c
--
2.0.0.526.g5318336
PATCH v2 1/11 fs: add O_BENEATH flag to openat(2)
Add a new O_BENEATH flag for openat(2) which restricts the provided path, rejecting (with -EACCES) paths that are not beneath the provided dfd. In particular, reject:
- paths that contain .. components
- paths that begin with /
- symlinks that have paths as above.
--- arch/alpha/include/uapi/asm/fcntl.h | 1 + arch/parisc/include/uapi/asm/fcntl.h | 1 + arch/sparc/include/uapi/asm/fcntl.h | 1 + fs/fcntl.c | 5 +++-- fs/namei.c | 43 ++++++++++++++++++++++++------------ fs/open.c | 4 +++- include/linux/namei.h | 1 + include/uapi/asm-generic/fcntl.h | 4 ++++ 8 files changed, 43 insertions(+), 17 deletions(-)
arch/alpha/include/uapi/asm/fcntl.h +1 -0
@@ -33,6 +33,7 @@#define O_PATH 040000000#define __O_TMPFILE 0100000000 +#define O_BENEATH 0200000000/* no / or .. in openat path */ #define F_GETLK 7#define F_SETLK 8
arch/parisc/include/uapi/asm/fcntl.h +1 -0
@@ -21,6 +21,7 @@#define O_PATH 020000000#define __O_TMPFILE 040000000 +#define O_BENEATH 080000000/* no / or .. in openat path */ #define F_GETLK64 8#define F_SETLK64 9
arch/sparc/include/uapi/asm/fcntl.h +1 -0
@@ -36,6 +36,7 @@#define O_PATH 0x1000000#define __O_TMPFILE 0x2000000 +#define O_BENEATH 0x4000000/* no / or .. in openat path */ #define F_GETOWN 5/* for sockets. */ #define F_SETOWN 6/* for sockets. */
fs/fcntl.c +3 -2
@@ -742,14 +742,15 @@ static int __init fcntl_init(void)* Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ -BUILD_BUG_ON (20 -1 /* for O_RDONLY being 0 */ !=HWEIGHT32 ( +BUILD_BUG_ON (21 -1 /* for O_RDONLY being 0 */ !=HWEIGHT32 (O_RDONLY |O_WRONLY |O_RDWR |O_CREAT |O_EXCL |O_NOCTTY |O_TRUNC |O_APPEND |/* O_NONBLOCK | */ __O_SYNC |O_DSYNC |FASYNC |O_DIRECT |O_LARGEFILE |O_DIRECTORY |O_NOFOLLOW |O_NOATIME |O_CLOEXEC | - __FMODE_EXEC |O_PATH | __O_TMPFILE + __FMODE_EXEC |O_PATH | __O_TMPFILE | +O_BENEATH )); fasync_cache = kmem_cache_create("fasync_cache",
fs/namei.c +29 -14
144 lines not on this page · raw message
fs/open.c +3 -1
@@ -875,7 +875,7 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o* If we have O_PATH in the open flag. Then we * cannot have anything other than the below set of flags */ - flags &=O_DIRECTORY |O_NOFOLLOW |O_PATH ; + flags &=O_DIRECTORY |O_NOFOLLOW |O_PATH |O_BENEATH ; acc_mode =0 ; }else { acc_mode =MAY_OPEN |ACC_MODE (flags);
@@ -906,6 +906,8 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o lookup_flags |=LOOKUP_DIRECTORY ;if (!(flags &O_NOFOLLOW )) lookup_flags |=LOOKUP_FOLLOW ; +if (flags &O_BENEATH ) + lookup_flags |=LOOKUP_BENEATH ; op->lookup_flags = lookup_flags;return 0 ; }
include/linux/namei.h +1 -0
@@ -39,6 +39,7 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};#define LOOKUP_FOLLOW 0x0001#define LOOKUP_DIRECTORY 0x0002#define LOOKUP_AUTOMOUNT 0x0004 +#define LOOKUP_BENEATH 0x0008#define LOOKUP_PARENT 0x0010#define LOOKUP_REVAL 0x0020
include/uapi/asm-generic/fcntl.h +4 -0
@@ -92,6 +92,10 @@#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)#define O_TMPFILE_MASK (__O_TMPFILE | O_DIRECTORY | O_CREAT) +#ifndef O_BENEATH +#define O_BENEATH 040000000/* no / or .. in openat path */ +#endif +#ifndef O_NDELAY #define O_NDELAY O_NONBLOCK#endif
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 2/11 selftests: Add test of O_BENEATH & openat(2)
In reply to David DrysdaleAdd simple tests of openat(2) variations, including examples that check the new O_BENEATH flag.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/openat/.gitignore | 3 + tools/testing/selftests/openat/Makefile | 24 +++++ tools/testing/selftests/openat/openat.c | 149 ++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 tools/testing/selftests/openat/.gitignore create mode 100644 tools/testing/selftests/openat/Makefile create mode 100644 tools/testing/selftests/openat/openat.c
tools/testing/selftests/Makefile +1 -0
@@ -11,6 +11,7 @@ TARGETS += vm TARGETS += powerpc TARGETS += user TARGETS += sysctl +TARGETS += openat all: for TARGET in $(TARGETS); do \
tools/testing/selftests/openat/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +openat +subdir +topfile
tools/testing/selftests/openat/Makefile new file +24 -0
@@ -0,0 +1,24 @@ +CC = $(CROSS_COMPILE)gcc +CFLAGS = -Wall +BINARIES = openat +DEPS = subdir topfile subdir/bottomfile subdir/symlinkup subdir/symlinkout +all: $(BINARIES) $(DEPS) + +subdir: + mkdir -p subdir +topfile: + echo 0123456789 > $@ +subdir/bottomfile: | subdir + echo 0123456789 > $@ +subdir/symlinkup: + ln -s ../topfile $@ +subdir/symlinkout: + ln -s /etc/passwd $@ +%: %.c + $(CC) $(CFLAGS) -o $@ $^ + +run_tests: all + ./openat + +clean: + rm -rf $(BINARIES) $(DEPS)
tools/testing/selftests/openat/openat.c new file +149 -0
149 lines not on this page · raw message
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 3/11 capsicum: rights values and structure definitions
In reply to David DrysdaleDefine (in include/uapi/linux/capsicum.h) values for primary rights associated with Capsicum capability file descriptors.
Also define the structure that primary rights reside in (struct cap_rights), and the complete compound rights structure (struct capsicum_rights).
- Primary rights describe the main operations that can be performed on a file.
- Secondary rights allow for specific fcntl() and ioctl() operations to be policed.
Add functions to manipulate these rights structures.
This change is adapted from the FreeBSD 10.x implementation of Capsicum, with the aim of preserving compatibility between the two implementations as closely as possible.
--- Documentation/security/capsicum.txt | 80 +++++++++ include/linux/capsicum.h | 50 ++++++ include/uapi/linux/Kbuild | 1 + include/uapi/linux/capsicum.h | 343 ++++++++++++++++++++++++++++++++++++ security/Kconfig | 15 ++ security/Makefile | 2 +- security/capsicum-rights.c | 205 +++++++++++++++++++++ security/capsicum-rights.h | 10 ++ 8 files changed, 705 insertions(+), 1 deletion(-) create mode 100644 Documentation/security/capsicum.txt create mode 100644 include/linux/capsicum.h create mode 100644 include/uapi/linux/capsicum.h create mode 100644 security/capsicum-rights.c create mode 100644 security/capsicum-rights.h
Documentation/security/capsicum.txt new file +80 -0
@@ -0,0 +1,80 @@ +Capsicum Object Capabilities +============================ + +Capsicum is a lightweight OS capability and sandbox framework, which allows +security-aware userspace applications to sandbox parts of their own code in a +highly granular way, reducing the attack surface in the event of subversion. + +Originally developed at the University of Cambridge Computer Laboratory, and +initially implemented in FreeBSD 9.x, Capsicum extends the POSIX API, providing +several new OS primitives to support object-capability security on UNIX-like +operating systems. + +Note that Capsicum capability file descriptors are radically different to the +POSIX.1e capabilities that are already available in Linux: + - POSIX.1e capabilities subdivide the root user's authority into different + areas of functionality. + - Capsicum capabilities restrict individual file descriptors so that + only operations permitted by that particular FD's rights are allowed. + + +Overview +-------- + +Capability-based security is a security model where objects can only be +accessed via capabilities, which are unforgeable tokens of authority that only +give rights to perform certain operations. + +Capsicum is a pragmatic blend of capability-based security with standard +UNIX/POSIX system semantics. A Capsicum capability is a file descriptor that +has an associated rights bitmask, and the kernel polices operations using that +file descriptor, failing operations with insufficient rights. + + +Capability Data Structure +------------------------- + +Internally, a capability is a particular kind of struct file that wraps an +underlying normal file. The private data for the wrapper indicates the +wrapped file, and holds the rights information for the capability. + + +FD to File Conversion +--------------------- + +The primary policing of Capsicum capabilities occurs when a user-provided file +descriptor is converted to a struct file object, normally using one of the +fgetr() family of functions. + +All such operations in the kernel are annotated with information about the +operations that are going to be performed on the retrieved struct file. For +example, a file that is retrieved for a read operation has its fgetr() call +annotated with CAP_READ, indicating that any capability FD that reaches this +point needs to include the CAP_READ right to progress further. If the +appropriate right is not available, -ENOTCAPABLE is returned. + +This change is the most significant change to the kernel, as it affects all +FD-to-file conversions. However, for a non-Capsicum build of the kernel the +impact is minimal as the additional rights parameters to fgetr*() are macroed +out. + + +Path Traversal +-------------- + +Capsicum does allow new files to be accessed beneath a directory for which the +application has a suitable capability FD (one including the CAP_LOOKUP right), +using the openat(2) system call. To prevent escape from the directory, path +traversals are policed for "/" and ".." components by implicitly setting the +O_BENEATH flag for file-open operations. + + +New System Calls +---------------- + +Capsicum implements the following new system calls: + - cap_rights_limit: restrict the rights associated with file descriptor, thus + turning it into a capability FD; internally this is implemented by wrapping + the original struct file with a capability file (security/capsicum.c) + - cap_rights_get: return the rights associated with a capability FD + (security/capsicum.c)
include/linux/capsicum.h new file +50 -0
50 lines not on this page · raw message
include/uapi/linux/Kbuild +1 -0
@@ -73,6 +73,7 @@ header-y += btrfs.h header-y += can.h header-y += capability.h header-y += capi.h +header-y += capsicum.h header-y += cciss_defs.h header-y += cciss_ioctl.h header-y += cdrom.h
include/uapi/linux/capsicum.h new file +343 -0
343 lines not on this page · raw message
security/Kconfig +15 -0
@@ -117,6 +117,21 @@ config LSM_MMAP_MIN_ADDR this low address space will need the permission specific to the systems running LSM. +config SECURITY_CAPSICUM + bool "Capsicum capabilities" + default y + depends on SECURITY + depends on SECURITY_PATH + depends on SECCOMP + help + Enable the Capsicum capability framework, which implements security + primitives that support fine-grained capabilities on file + descriptors; see Documentation/security/capsicum.txt for more + details. + + If you are unsure as to whether this is required, answer N. + + source security/selinux/Kconfig source security/smack/Kconfig source security/tomoyo/Kconfig
security/Makefile +1 -1
@@ -14,7 +14,7 @@ obj-y += commoncap.o obj-$(CONFIG_MMU) += min_addr.o # Object file lists -obj-$(CONFIG_SECURITY) += security.o capability.o +obj-$(CONFIG_SECURITY) += security.o capability.o capsicum-rights.o obj-$(CONFIG_SECURITYFS) += inode.o obj-$(CONFIG_SECURITY_SELINUX) += selinux/ obj-$(CONFIG_SECURITY_SMACK) += smack/
security/capsicum-rights.c new file +205 -0
205 lines not on this page · raw message
security/capsicum-rights.h new file +10 -0
@@ -0,0 +1,10 @@ +#ifndef _CAPSICUM_RIGHTS_H +#define _CAPSICUM_RIGHTS_H + +#ifdef CONFIG_SECURITY_CAPSICUM +bool cap_rights_regularize (struct capsicum_rights *rights); +bool cap_rights_contains (const struct capsicum_rights *big, +const struct capsicum_rights *little); +#endif + +#endif /* _CAPSICUM_RIGHTS_H */
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 4/11 capsicum: implement fgetr() and friends
In reply to David DrysdaleAdd variants of fget() and related functions where the caller indicates the operations that will be performed on the file.
If CONFIG_SECURITY_CAPSICUM is defined, these variants build a struct capsicum_rights instance holding the rights associated with the file operations; this will allow a future hook to check whether a rights-restricted file has those specific rights available.
If CONFIG_SECURITY_CAPSICUM is not defined, these variants expand to the underlying fget() function, with one difference: failures are returned as an ERR_PTR value rather than just NULL.
--- fs/file.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/namei.c | 50 ++++++++++++++++-- fs/read_write.c | 5 -- include/linux/file.h | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/namei.h | 9 ++++ 5 files changed, 331 insertions(+), 8 deletions(-)
fs/file.c +136 -0
@@ -13,6 +13,7 @@#include <linux/mmzone.h> #include <linux/time.h> #include <linux/sched.h> +#include <linux/security.h> #include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/file.h>
@@ -717,6 +718,141 @@ unsigned long __fdget_pos(unsigned int fd)return v; } +#ifdef CONFIG_SECURITY_CAPSICUM +/* +* We might want to change the return value of fget() and friends. This +* function is called with the intended return value, and fget() will /actually/ +* return whatever is returned from here. We adjust the reference counter if +* necessary. +*/ +static struct file *unwrap_file (struct file *orig, +const struct capsicum_rights *required_rights, +const struct capsicum_rights **actual_rights, +bool update_refcnt) +{ +struct file *f; + +if (orig ==NULL ) +return ERR_PTR (-EBADF ); +if (IS_ERR (orig)) +return orig; + f = orig;/* TODO: change the value of f here */ +if (f != orig && update_refcnt) { +/* We're not returning the original, and the calling code +* has already incremented the refcount on it, we need to +* release that reference and obtain a reference to the new +* return value, if any. +*/ +if (!IS_ERR (f) && !atomic_long_inc_not_zero (&f->f_count)) + f =ERR_PTR (-EBADF ); +atomic_long_dec (&orig->f_count); + } + +return f; +} + +struct file *fget_rights (unsigned int fd,const struct capsicum_rights *rights) +{ +return unwrap_file (fget (fd), rights,NULL , true); +} +EXPORT_SYMBOL (fget_rights); + +struct file *fget_raw_rights (unsigned int fd, +const struct capsicum_rights *rights) +{ +return unwrap_file (fget_raw (fd), rights,NULL , true); +} +EXPORT_SYMBOL (fget_raw_rights); + +struct fd fdget_rights (unsigned int fd,const struct capsicum_rights *rights) +{ +struct fd f =fdget (fd); + + f.file =unwrap_file (f.file, rights,NULL , (f.flags &FDPUT_FPUT )); +return f; +} +EXPORT_SYMBOL (fdget_rights); + +struct fd fdget_raw_rights (unsigned int fd, +const struct capsicum_rights **actual_rights, +const struct capsicum_rights *rights) +{ +struct fd f =fdget_raw (fd); + + f.file =unwrap_file (f.file, rights, actual_rights, + (f.flags &FDPUT_FPUT )); +return f; +} +EXPORT_SYMBOL (fdget_raw_rights); + +struct file *_fgetr (unsigned int fd, ...) +{ +struct capsicum_rights rights; +struct file *f; +va_list ap; + +va_start (ap, fd); + f =fget_rights (fd,cap_rights_vinit (&rights, ap)); +va_end (ap); +return f; +} +EXPORT_SYMBOL (_fgetr); + +struct file *_fgetr_raw (unsigned int fd, ...) +{ +struct capsicum_rights rights; +struct file *f; +va_list ap; + +va_start (ap, fd); + f =fget_raw_rights (fd,cap_rights_vinit (&rights, ap)); +va_end (ap); +return f; +} +EXPORT_SYMBOL (_fgetr_raw); + +struct fd _fdgetr (unsigned int fd, ...) +{ +struct fd f; +struct capsicum_rights rights; +va_list ap; + +va_start (ap, fd); + f =fdget_rights (fd,cap_rights_vinit (&rights, ap)); +va_end (ap); +return f; +} +EXPORT_SYMBOL (_fdgetr); + +struct fd _fdgetr_raw (unsigned int fd, ...) +{ +struct fd f; +struct capsicum_rights rights; +va_list ap; + +va_start (ap, fd); + f =fdget_raw_rights (fd,NULL ,cap_rights_vinit (&rights, ap)); +va_end (ap); +return f; +} +EXPORT_SYMBOL (_fdgetr_raw); + +struct fd _fdgetr_pos (unsigned int fd, ...) +{ +struct fd f; +struct capsicum_rights rights; +va_list ap; + + f =__to_fd (__fdget_pos (fd)); +va_start (ap, fd); + f.file =unwrap_file (f.file,cap_rights_vinit (&rights, ap),NULL , + (f.flags &FDPUT_FPUT )); +va_end (ap); +return f; +} +EXPORT_SYMBOL (_fdgetr_pos); +#endif +/* * We only lock f_pos if we have threads or if the file might be * shared with another process. In both cases we'll have an elevated
fs/namei.c +47 -3
74 lines not on this page · raw message
fs/read_write.c +0 -5
@@ -265,11 +265,6 @@ loff_t vfs_llseek(struct file *file, loff_t offset, int whence) }EXPORT_SYMBOL (vfs_llseek); -static inline struct fd fdget_pos (int fd) -{ -return __to_fd (__fdget_pos (fd)); -} -static inline void fdput_pos (struct fd f) {if (f.flags &FDPUT_POS_UNLOCK )
include/linux/file.h +139 -0
157 lines not on this page · raw message
include/linux/namei.h +9 -0
@@ -59,6 +59,15 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};extern int user_path_at (int ,const char __user *,unsigned ,struct path *);extern int user_path_at_empty (int ,const char __user *,unsigned ,struct path *,int *empty); +#ifdef CONFIG_SECURITY_CAPSICUM +extern int _user_path_atr (int ,const char __user *,unsigned , +struct path *, ...); +#define user_path_atr (f, n, x, p, ...) \ + _user_path_atr((f), (n), (x), (p), __VA_ARGS__, 0ULL) +#else +#define user_path_atr (f, n, x, p, ...) \ + user_path_at((f), (n), (x), (p)) +#endif #define user_path (name, path) user_path_at(AT_FDCWD, name, LOOKUP_FOLLOW, path)#define user_lpath (name, path) user_path_at(AT_FDCWD, name, 0, path)
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 5/11 capsicum: convert callers to use fgetr() etc
In reply to David DrysdaleConvert places that use fget()-like functions to use the equivalent fgetr() variant instead.
Annotate each such call with an indication of what operations will be performed on the retrieved struct file, to allow future policing of rights associated with file descriptors.
Also change each call site to cope with an ERR_PTR return from fgetr() rather than a plain NULL failure from fget().
--- arch/alpha/kernel/osf_sys.c | 6 +- arch/ia64/kernel/perfmon.c | 54 +++++++----- arch/parisc/hpux/fs.c | 6 +- arch/powerpc/kvm/powerpc.c | 4 +- arch/powerpc/platforms/cell/spu_syscalls.c | 15 ++-- drivers/base/dma-buf.c | 6 +- drivers/block/loop.c | 14 +-- drivers/block/nbd.c | 2 +- drivers/infiniband/core/ucma.c | 4 +- drivers/infiniband/core/uverbs_cmd.c | 6 +- drivers/infiniband/core/uverbs_main.c | 4 +- drivers/infiniband/hw/usnic/usnic_transport.c | 2 +- drivers/md/md.c | 8 +- drivers/staging/android/sync.c | 2 +- drivers/staging/lustre/lustre/llite/file.c | 6 +- drivers/staging/lustre/lustre/lmv/lmv_obd.c | 7 +- drivers/staging/lustre/lustre/mdc/lproc_mdc.c | 7 +- drivers/staging/lustre/lustre/mdc/mdc_request.c | 4 +- drivers/vfio/pci/vfio_pci.c | 7 +- drivers/vfio/pci/vfio_pci_intrs.c | 6 +- drivers/vfio/vfio.c | 6 +- drivers/vhost/net.c | 6 +- drivers/video/fbdev/msm/mdp.c | 5 +- fs/aio.c | 37 +++++++- fs/autofs4/dev-ioctl.c | 17 ++-- fs/autofs4/inode.c | 4 +- fs/btrfs/ioctl.c | 21 +++-- fs/btrfs/send.c | 7 +- fs/cifs/ioctl.c | 6 +- fs/coda/inode.c | 4 +- fs/coda/psdev.c | 2 +- fs/compat.c | 18 ++-- fs/compat_ioctl.c | 14 ++- fs/eventfd.c | 18 ++-- fs/eventpoll.c | 19 +++-- fs/ext4/ioctl.c | 6 +- fs/fcntl.c | 101 ++++++++++++++++++++-- fs/fhandle.c | 7 +- fs/fuse/inode.c | 10 ++- fs/ioctl.c | 12 ++- fs/locks.c | 8 +- fs/notify/fanotify/fanotify_user.c | 16 ++-- fs/notify/inotify/inotify_user.c | 12 +-- fs/ocfs2/cluster/heartbeat.c | 8 +- fs/open.c | 42 +++++---- fs/proc/namespaces.c | 6 +- fs/read_write.c | 109 ++++++++++++++---------- fs/readdir.c | 18 ++-- fs/select.c | 11 ++- fs/signalfd.c | 7 +- fs/splice.c | 35 +++++--- fs/stat.c | 10 ++- fs/statfs.c | 9 +- fs/sync.c | 21 +++-- fs/timerfd.c | 40 +++++++-- fs/utimes.c | 10 ++- fs/xattr.c | 26 +++--- fs/xfs/xfs_ioctl.c | 14 +-- ipc/mqueue.c | 30 +++---- kernel/events/core.c | 15 ++-- kernel/module.c | 10 ++- kernel/sys.c | 6 +- kernel/taskstats.c | 4 +- kernel/time/posix-clock.c | 27 +++--- mm/fadvise.c | 7 +- mm/internal.h | 19 +++++ mm/memcontrol.c | 12 +-- mm/mmap.c | 7 +- mm/nommu.c | 10 ++- mm/readahead.c | 6 +- net/9p/trans_fd.c | 10 +-- sound/core/pcm_native.c | 10 ++- virt/kvm/eventfd.c | 6 +- virt/kvm/vfio.c | 12 +-- 74 files changed, 698 insertions(+), 385 deletions(-)
arch/alpha/kernel/osf_sys.c +3 -3
@@ -146,7 +146,7 @@ SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd, long __user *, basep) {int error; -struct fd arg =fdget (fd); +struct fd arg =fdgetr (fd,CAP_READ );struct osf_dirent_callback buf = { .ctx.actor = osf_filldir, .dirent = dirent,
@@ -154,8 +154,8 @@ SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd, .count = count }; -if (!arg.file) -return -EBADF ; +if (IS_ERR (arg.file)) +return PTR_ERR (arg.file); error =iterate_dir (arg.file, &buf.ctx);if (error >=0 )
arch/ia64/kernel/perfmon.c +33 -21
91 lines not on this page · raw message
arch/parisc/hpux/fs.c +3 -3
@@ -105,9 +105,9 @@ int hpux_getdents(unsigned int fd, struct hpux_dirent __user *dirent, unsigned i };int error; - arg =fdget (fd); -if (!arg.file) -return -EBADF ; + arg =fdgetr (fd,CAP_READ ); +if (IS_ERR (arg.file)) +return PTR_ERR (arg.file); error =iterate_dir (arg.file, &buf.ctx);if (error >=0 )
arch/powerpc/kvm/powerpc.c +2 -2
@@ -933,7 +933,7 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,struct kvm_device *dev; r = -EBADF ; - f =fdget (cap->args[0 ]); + f =fdgetr (cap->args[0 ],CAP_FSTAT );if (!f.file)break ;
@@ -952,7 +952,7 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,struct kvm_device *dev; r = -EBADF ; - f =fdget (cap->args[0 ]); + f =fdgetr (cap->args[0 ],CAP_FSTAT );if (!f.file)break ;
arch/powerpc/platforms/cell/spu_syscalls.c +9 -6
@@ -77,11 +77,13 @@ SYSCALL_DEFINE4(spu_create, const char __user *, name, unsigned int, flags,return -ENOSYS ;if (flags &SPU_CREATE_AFFINITY_SPU ) { -struct fd neighbor =fdget (neighbor_fd); - ret = -EBADF ; -if (neighbor.file) { +struct fd neighbor =fdgetr (neighbor_fd,CAP_READ ,CAP_WRITE , +CAP_MAPEXEC ); +if (!IS_ERR (neighbor.file)) { ret = calls->create_thread (name, flags, mode, neighbor.file);fdput (neighbor); + }else { + ret =PTR_ERR (neighbor.file); } }else ret = calls->create_thread (name, flags, mode,NULL );
@@ -100,11 +102,12 @@ asmlinkage long sys_spu_run(int fd, __u32 __user *unpc, __u32 __user *ustatus)if (!calls)return -ENOSYS ; - ret = -EBADF ; - arg =fdget (fd); -if (arg.file) { + arg =fdgetr (fd,CAP_READ ,CAP_WRITE ,CAP_MAPEXEC ); +if (!IS_ERR (arg.file)) { ret = calls->spu_run (arg.file, unpc, ustatus);fdput (arg); + }else { + ret =PTR_ERR (arg.file); }spufs_calls_put (calls);
drivers/base/dma-buf.c +3 -3
@@ -216,10 +216,10 @@ struct dma_buf *dma_buf_get(int fd) {struct file *file; - file =fget (fd); + file =fgetr (fd,CAP_MMAP ,CAP_READ ,CAP_WRITE ); -if (!file) -return ERR_PTR (-EBADF ); +if (IS_ERR (file)) +return (struct dma_buf *)file;if (!is_dma_buf_file (file)) {fput (file);
drivers/block/loop.c +8 -6
@@ -652,10 +652,11 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,if (!(lo->lo_flags &LO_FLAGS_READ_ONLY )) goto out; - error = -EBADF ; - file =fget (arg); -if (!file) + file =fgetr (arg,CAP_PWRITE ,CAP_PREAD ,CAP_FSYNC ,CAP_FSTAT ); +if (IS_ERR (file)) { + error =PTR_ERR (file); goto out; + } inode = file->f_mapping->host; old_file = lo->lo_backing_file;
@@ -834,10 +835,11 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,/* This is safe, since we have a reference from open(). */ __module_get (THIS_MODULE ); - error = -EBADF ; - file =fget (arg); -if (!file) + file =fgetr (arg,CAP_PWRITE ,CAP_PREAD ,CAP_FSYNC ,CAP_FSTAT ); +if (IS_ERR (file)) { + error =PTR_ERR (file); goto out; + } error = -EBUSY ;if (lo->lo_state != Lo_unbound)
drivers/block/nbd.c +1 -1
@@ -651,7 +651,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, nbd->disconnect =0 ;/* we're connected now */ return 0 ; } -return -EINVAL ; +return err; }case NBD_SET_BLKSIZE :
drivers/infiniband/core/ucma.c +2 -2
@@ -1406,8 +1406,8 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,return -EFAULT ;/* Get current fd to protect against it being closed */ - f =fdget (cmd.fd); -if (!f.file) + f =fdgetr (cmd.fd,CAP_READ ,CAP_WRITE ,CAP_POLL_EVENT ); +if (IS_ERR (f.file))return -ENOENT ;/* Validate current fd and prevent destruction of id. */
drivers/infiniband/core/uverbs_cmd.c +3 -3
@@ -719,9 +719,9 @@ ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,if (cmd.fd !=-1 ) {/* search for file descriptor */ - f =fdget (cmd.fd); -if (!f.file) { - ret = -EBADF ; + f =fdgetr (cmd.fd,CAP_FSTAT ); +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file); goto err_tree_mutex_unlock; }
drivers/infiniband/core/uverbs_main.c +2 -2
@@ -566,9 +566,9 @@ struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file (int fd) {struct ib_uverbs_event_file *ev_file =NULL ; -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_LIST_END ); -if (!f.file) +if (IS_ERR (f.file))return NULL ;if (f.file->f_op != &uverbs_event_fops)
drivers/infiniband/hw/usnic/usnic_transport.c +1 -1
@@ -134,7 +134,7 @@ struct socket *usnic_transport_get_socket(int sock_fd)char buf[25 ];/* sockfd_lookup will internally do a fget */ - sock =sockfd_lookup (sock_fd, &err); + sock =sockfd_lookupr (sock_fd, &err,CAP_SOCK_SERVER );if (!sock) {usnic_err ("Unable to lookup socket for fd %d with err %d\n" , sock_fd, err);
drivers/md/md.c +5 -3
@@ -5973,12 +5973,14 @@ static int set_bitmap_file(struct mddev *mddev, int fd)struct inode *inode;if (mddev->bitmap)return -EEXIST ;/* cannot add when bitmap is present */ - mddev->bitmap_info.file =fget (fd); + mddev->bitmap_info.file =fgetr (fd,CAP_READ ); -if (mddev->bitmap_info.file ==NULL ) { +if (IS_ERR (mddev->bitmap_info.file)) { + err =PTR_ERR (mddev->bitmap_info.file); + mddev->bitmap_info.file =NULL ;printk (KERN_ERR "%s: error: failed to get bitmap file\n" ,mdname (mddev)); -return -EBADF ; +return err; } inode = mddev->bitmap_info.file->f_mapping->host;
drivers/staging/android/sync.c +1 -1
@@ -406,7 +406,7 @@ static void sync_fence_free_pts(struct sync_fence *fence)struct sync_fence *sync_fence_fdget (int fd) { -struct file *file =fget (fd); +struct file *file =fgetr (fd,CAP_IOCTL );if (file ==NULL )return NULL ;
drivers/staging/lustre/lustre/llite/file.c +3 -3
@@ -2186,9 +2186,9 @@ ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)if ((file->f_flags &O_ACCMODE ) ==0 )/* O_RDONLY */ return -EPERM ; - file2 =fget (lsl.sl_fd); -if (file2 ==NULL ) -return -EBADF ; + file2 =fgetr (lsl.sl_fd,CAP_FSTAT ); +if (IS_ERR (file2)) +return PTR_ERR (file2); rc = -EPERM ;if ((file2->f_flags &O_ACCMODE ) !=0 )/* O_WRONLY or O_RDWR */
drivers/staging/lustre/lustre/lmv/lmv_obd.c +3 -4
@@ -880,10 +880,9 @@ static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,return -ENOTCONN ;/* at least one registration done, with no failure */ - filp =fget (lk->lk_wfd); -if (filp ==NULL ) { -return -EBADF ; - } + filp =fgetr (lk->lk_wfd,CAP_READ ); +if (IS_ERR (filp)) +return PTR_ERR (filp); rc =libcfs_kkuc_group_add (filp, lk->lk_uid, lk->lk_group, lk->lk_data);if (rc !=0 && filp !=NULL )fput (filp);
drivers/staging/lustre/lustre/mdc/lproc_mdc.c +5 -2
@@ -130,9 +130,12 @@ static ssize_t mdc_kuc_write(struct file *file, const char *buffer,if (fd ==0 ) { rc =libcfs_kkuc_group_put (KUC_GRP_HSM , lh); }else { -struct file *fp =fget (fd); +struct file *fp =fgetr (fd,CAP_WRITE ); - rc =libcfs_kkuc_msg_put (fp, lh); +if (IS_ERR (fp)) + rc =PTR_ERR (fp); +else + rc =libcfs_kkuc_msg_put (fp, lh);fput (fp); }OBD_FREE (lh, len);
drivers/staging/lustre/lustre/mdc/mdc_request.c +3 -1
@@ -1606,7 +1606,9 @@ static int mdc_ioc_changelog_send(struct obd_device *obd, cs->cs_obd = obd; cs->cs_startrec = icc->icc_recno;/* matching fput in mdc_changelog_send_thread */ - cs->cs_fp =fget (icc->icc_id); + cs->cs_fp =fgetr (icc->icc_id,CAP_WRITE ); +if (IS_ERR (cs->cs_fp)) + cs->cs_fp =NULL ; cs->cs_flags = icc->icc_flags;/*
drivers/vfio/pci/vfio_pci.c +4 -3
@@ -638,9 +638,10 @@ reset_info_exit:*/ for (i =0 ; i < hdr.count; i++) {struct vfio_group *group; -struct fd f =fdget (group_fds[i]); -if (!f.file) { - ret = -EBADF ; +struct fd f =fdgetr (group_fds[i],CAP_FSTAT ); + +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file);break ; }
drivers/vfio/pci/vfio_pci_intrs.c +3 -3
@@ -149,9 +149,9 @@ static int virqfd_enable(struct vfio_pci_device *vdev,INIT_WORK (&virqfd->shutdown, virqfd_shutdown);INIT_WORK (&virqfd->inject, virqfd_inject); - irqfd =fdget (fd); -if (!irqfd.file) { - ret = -EBADF ; + irqfd =fdgetr (fd,CAP_WRITE ); +if (IS_ERR (irqfd.file)) { + ret =PTR_ERR (irqfd.file); goto err_fd; }
drivers/vfio/vfio.c +3 -3
@@ -1036,9 +1036,9 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)if (atomic_read (&group->container_users))return -EINVAL ; - f =fdget (container_fd); -if (!f.file) -return -EBADF ; + f =fdgetr (container_fd,CAP_LIST_END ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file);/* Sanity check, is this really our fd? */ if (f.file->f_op != &vfio_fops) {
drivers/vhost/net.c +3 -3
@@ -871,11 +871,11 @@ err:static struct socket *get_tap_socket (int fd) { -struct file *file =fget (fd); +struct file *file =fgetr (fd,CAP_READ ,CAP_WRITE );struct socket *sock; -if (!file) -return ERR_PTR (-EBADF ); +if (IS_ERR (file)) +return ERR_PTR (PTR_ERR (file)); sock =tun_get_socket (file);if (!IS_ERR (sock))return sock;
drivers/video/fbdev/msm/mdp.c +3 -2
@@ -257,8 +257,9 @@ int get_img(struct mdp_img *img, struct fb_info *info, struct file **filep) {int ret =0 ; -struct fd f =fdget (img->memory_id); -if (f.file ==NULL ) +struct fd f =fdgetr (img->memory_id,CAP_FSTAT ); + +if (IS_ERR (f.file))return -1 ;if (MAJOR (file_inode (f.file)->i_rdev) ==FB_MAJOR ) {
fs/aio.c +34 -3
@@ -1404,10 +1404,38 @@ rw_common:return 0 ; } +static struct capsicum_rights * +aio_opcode_rights (struct capsicum_rights *rights,int opcode) +{ +switch (opcode) { +case IOCB_CMD_PREAD : +case IOCB_CMD_PREADV : +cap_rights_init (rights,CAP_PREAD ); +break ; + +case IOCB_CMD_PWRITE : +case IOCB_CMD_PWRITEV : +cap_rights_init (rights,CAP_PWRITE ); +break ; + +case IOCB_CMD_FSYNC : +case IOCB_CMD_FDSYNC : +cap_rights_init (rights,CAP_FSYNC ); +break ; + +default : +cap_rights_init (rights,CAP_PREAD ,CAP_PWRITE ,CAP_POLL_EVENT , +CAP_FSYNC ); +break ; + } +return rights; +} +static int io_submit_one (struct kioctx *ctx,struct iocb __user *user_iocb,struct iocb *iocb,bool compat) {struct kiocb *req; +struct capsicum_rights rights;ssize_t ret;/* enforce forwards compatibility on users */
@@ -1430,9 +1458,12 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,if (unlikely (!req))return -EAGAIN ; - req->ki_filp =fget (iocb->aio_fildes); -if (unlikely (!req->ki_filp)) { - ret = -EBADF ; + req->ki_filp =fget_rights (iocb->aio_fildes, +aio_opcode_rights (&rights, + iocb->aio_lio_opcode)); +if (unlikely (IS_ERR (req->ki_filp))) { + ret =PTR_ERR (req->ki_filp); + req->ki_filp =NULL ; goto out_put_req; }
fs/autofs4/dev-ioctl.c +11 -6
@@ -371,9 +371,9 @@ static int autofs_dev_ioctl_setpipefd(struct file *fp, goto out; } - pipe =fget (pipefd); -if (!pipe) { - err = -EBADF ; + pipe =fgetr (pipefd,CAP_READ ,CAP_WRITE ,CAP_FSYNC ); +if (IS_ERR (pipe)) { + err =PTR_ERR (pipe); goto out; }if (autofs_prepare_pipe (pipe) <0 ) {
@@ -665,11 +665,16 @@ static int _autofs_dev_ioctl(unsigned int command, struct autofs_dev_ioctl __use*/ if (cmd !=AUTOFS_DEV_IOCTL_OPENMOUNT_CMD && cmd !=AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD ) { - fp =fget (param->ioctlfd); -if (!fp) { +struct capsicum_rights rights; + +cap_rights_init (&rights,CAP_IOCTL ,CAP_FSTAT ); + rights.nioctls =1 ; + rights.ioctls = &cmd; + fp =fget_rights (param->ioctlfd, &rights); +if (IS_ERR (fp)) {if (cmd ==AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD ) goto cont; - err = -EBADF ; + err =PTR_ERR (fp); goto out; }
fs/autofs4/inode.c +2 -2
@@ -305,9 +305,9 @@ int autofs4_fill_super(struct super_block *s, void *data, int silent) sbi->sub_version =AUTOFS_PROTO_SUBVERSION ;DPRINTK ("pipe fd = %d, pgrp = %u" , pipefd,pid_nr (sbi->oz_pgrp)); - pipe =fget (pipefd); + pipe =fgetr (pipefd,CAP_WRITE ,CAP_FSYNC ); -if (!pipe) { +if (IS_ERR (pipe)) {printk ("autofs: could not open pipe file descriptor\n" ); goto fail_dput; }
fs/btrfs/ioctl.c +12 -9
@@ -1666,10 +1666,12 @@ static noinline int btrfs_ioctl_snap_create_transid(struct file *file, ret =btrfs_mksubvol (&file->f_path, name, namelen,NULL , transid, readonly, inherit); }else { -struct fd src =fdget (fd); +struct fd src =fdgetr (fd,CAP_FSTAT );struct inode *src_inode; -if (!src.file) { - ret = -EINVAL ; +if (IS_ERR (src.file)) { + ret =PTR_ERR (src.file); +if (ret == -EBADF ) + ret = -EINVAL ; goto out_drop_write; }
@@ -3040,9 +3042,10 @@ static long btrfs_ioctl_file_extent_same(struct file *file,for (i =0 , info = same->info; i < count; i++, info++) {struct inode *dst; -struct fd dst_file =fdget (info->fd); -if (!dst_file.file) { - info->status = -EBADF ; +struct fd dst_file =fdgetr (info->fd,CAP_FSTAT ); + +if (IS_ERR (dst_file.file)) { + info->status =PTR_ERR (dst_file.file);continue ; } dst =file_inode (dst_file.file);
@@ -3611,9 +3614,9 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,if (ret)return ret; - src_file =fdget (srcfd); -if (!src_file.file) { - ret = -EBADF ; + src_file =fdgetr (srcfd,CAP_FSTAT ); +if (IS_ERR (src_file.file)) { + ret =PTR_ERR (src_file.file); goto out_drop_write; }
fs/btrfs/send.c +4 -3
@@ -5573,9 +5573,10 @@ long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_) sctx->flags = arg->flags; - sctx->send_filp =fget (arg->send_fd); -if (!sctx->send_filp) { - ret = -EBADF ; + sctx->send_filp =fgetr (arg->send_fd,CAP_PWRITE ); +if (IS_ERR (sctx->send_filp)) { + ret =PTR_ERR (sctx->send_filp); + sctx->send_filp =NULL ; goto out; }
fs/cifs/ioctl.c +3 -3
@@ -61,9 +61,9 @@ static long cifs_ioctl_clone(unsigned int xid, struct file *dst_file,return rc; } - src_file =fdget (srcfd); -if (!src_file.file) { - rc = -EBADF ; + src_file =fdgetr (srcfd,CAP_PREAD ); +if (IS_ERR (src_file.file)) { + rc =PTR_ERR (src_file.file); goto out_drop_write; }
fs/coda/inode.c +2 -2
@@ -128,8 +128,8 @@ static int get_device_index(struct coda_mount_data *data)return -1 ; } - f =fdget (data->fd); -if (!f.file) + f =fdgetr (data->fd,CAP_FSTAT ); +if (IS_ERR (f.file)) goto Ebadf; inode =file_inode (f.file);if (!S_ISCHR (inode->i_mode) ||imajor (inode) !=CODA_PSDEV_MAJOR ) {
fs/coda/psdev.c +1 -1
@@ -188,7 +188,7 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,struct coda_open_by_fd_out *outp = (struct coda_open_by_fd_out *)req->uc_data;if (!outp->oh.result) - outp->fh =fget (outp->fd); + outp->fh =fgetr (outp->fd,CAP_LIST_END ); }wake_up (&req->uc_sleep);
fs/compat.c +9 -9
@@ -889,14 +889,14 @@ COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd, struct compat_old_linux_dirent __user *, dirent, unsigned int, count) { int error; -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_READ );struct compat_readdir_callback buf = { .ctx.actor = compat_fillonedir, .dirent = dirent }; -if (!f.file) -return -EBADF ; +if (IS_ERR (f.file)) +return PTR_ERR (f.file); error =iterate_dir (f.file, &buf.ctx);if (buf.result)
@@ -979,9 +979,9 @@ COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,if (!access_ok (VERIFY_WRITE , dirent, count))return -EFAULT ; - f =fdget (fd); -if (!f.file) -return -EBADF ; + f =fdgetr (fd,CAP_READ ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file); error =iterate_dir (f.file, &buf.ctx);if (error >=0 )
@@ -1064,9 +1064,9 @@ COMPAT_SYSCALL_DEFINE3(getdents64, unsigned int, fd,if (!access_ok (VERIFY_WRITE , dirent, count))return -EFAULT ; - f =fdget (fd); -if (!f.file) -return -EBADF ; + f =fdgetr (fd,CAP_READ ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file); error =iterate_dir (f.file, &buf.ctx);if (error >=0 )
fs/compat_ioctl.c +11 -3
@@ -1542,10 +1542,18 @@ COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, compat_ulong_t, arg32) {unsigned long arg = arg32; -struct fd f =fdget (fd); -int error = -EBADF ; -if (!f.file) +struct capsicum_rights rights; +struct fd f; +int error; + +cap_rights_init (&rights,CAP_IOCTL ); + rights.nioctls =1 ; + rights.ioctls = &cmd; + f =fdget_rights (fd, &rights); +if (IS_ERR (f.file)) { + error =PTR_ERR (f.file); goto out; + }/* RED-PEN how should LSM module know it's handling 32bit? */ error =security_file_ioctl (f.file, cmd, arg);
fs/eventfd.c +10 -8
@@ -319,16 +319,17 @@ static const struct file_operations eventfd_fops = {* Returns a pointer to the eventfd file structure in case of success, or the * following error pointer: * -* -EBADF : Invalid @fd file descriptor. -* -EINVAL : The @fd file descriptor is not an eventfd file. +* -EBADF : Invalid @fd file descriptor. +* -ENOTCAPABLE : The @fd file descriptor does not have the required rights. +* -EINVAL : The @fd file descriptor is not an eventfd file. */ struct file *eventfd_fget (int fd) {struct file *file; - file =fget (fd); -if (!file) -return ERR_PTR (-EBADF ); + file =fgetr (fd,CAP_WRITE ); +if (IS_ERR (file)) +return file;if (file->f_op != &eventfd_fops) {fput (file);return ERR_PTR (-EINVAL );
@@ -350,9 +351,10 @@ EXPORT_SYMBOL_GPL(eventfd_fget);struct eventfd_ctx *eventfd_ctx_fdget (int fd) {struct eventfd_ctx *ctx; -struct fd f =fdget (fd); -if (!f.file) -return ERR_PTR (-EBADF ); +struct fd f =fdgetr (fd,CAP_WRITE ); + +if (IS_ERR (f.file)) +return (struct eventfd_ctx *) f.file; ctx =eventfd_ctx_fileget (f.file);fdput (f);return ctx;
fs/eventpoll.c +11 -8
@@ -1836,15 +1836,18 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, copy_from_user(&epds, event, sizeof(struct epoll_event))) goto error_return; - error = -EBADF ; - f =fdget (epfd); -if (!f.file) + f =fdgetr (epfd,CAP_EPOLL_CTL ); +if (IS_ERR (f.file)) { + error =PTR_ERR (f.file); goto error_return; + }/* Get the "struct file *" for the target file */ - tf =fdget (fd); -if (!tf.file) + tf =fdgetr (fd,CAP_POLL_EVENT ); +if (IS_ERR (tf.file)) { + error =PTR_ERR (tf.file); goto error_fput; + }/* The target file descriptor must support poll */ error = -EPERM ;
@@ -1976,9 +1979,9 @@ SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,return -EFAULT ;/* Get the "struct file *" for the eventpoll file */ - f =fdget (epfd); -if (!f.file) -return -EBADF ; + f =fdgetr (epfd,CAP_POLL_EVENT ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file);/* * We have to check that the file structure underneath the fd
fs/ext4/ioctl.c +3 -3
@@ -419,9 +419,9 @@ group_extend_out:return -EFAULT ; me.moved_len =0 ; - donor =fdget (me.donor_fd); -if (!donor.file) -return -EBADF ; + donor =fdgetr (me.donor_fd,CAP_PWRITE ,CAP_FSTAT ); +if (IS_ERR (donor.file)) +return PTR_ERR (donor.file);if (!(donor.file->f_mode &FMODE_WRITE )) { err = -EBADF ;
fs/fcntl.c +95 -6
120 lines not on this page · raw message
fs/fhandle.c +4 -3
@@ -121,9 +121,10 @@ static struct vfsmount *get_vfsmount_from_fd(int fd) mnt =mntget (fs->pwd.mnt);spin_unlock (&fs->lock); }else { -struct fd f =fdget (fd); -if (!f.file) -return ERR_PTR (-EBADF ); +struct fd f =fdgetr (fd,CAP_LOOKUP ); + +if (IS_ERR (f.file)) +return (struct vfsmount *)f.file; mnt =mntget (f.file->f_path.mnt);fdput (f); }
fs/fuse/inode.c +7 -3
@@ -1025,11 +1025,15 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent) sb->s_time_gran =1 ; sb->s_export_op = &fuse_export_operations; - file =fget (d.fd); - err = -EINVAL ; -if (!file) + file =fgetr (d.fd,CAP_READ ,CAP_WRITE ); +if (IS_ERR (file)) { + err =PTR_ERR (file); +if (err == -EBADF ) + err = -EINVAL ; goto err; + } + err = -EINVAL ;if ((file->f_op != &fuse_dev_operations) || (file->f_cred->user_ns != &init_user_ns)) goto err_fput;
fs/ioctl.c +9 -3
@@ -604,10 +604,16 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) {int error; -struct fd f =fdget (fd); +struct capsicum_rights rights; +struct fd f; -if (!f.file) -return -EBADF ; +cap_rights_init (&rights,CAP_IOCTL ); + rights.nioctls =1 ; + rights.ioctls = &cmd; + f =fdget_rights (fd, &rights); + +if (IS_ERR (f.file)) +return PTR_ERR (f.file); error =security_file_ioctl (f.file, cmd, arg);if (!error) error =do_vfs_ioctl (f.file, fd, cmd, arg);
fs/locks.c +5 -3
@@ -1828,19 +1828,21 @@ EXPORT_SYMBOL(flock_lock_file_wait);*/ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) { -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_FLOCK );struct file_lock *lock;int can_sleep, unlock;int error; - error = -EBADF ; -if (!f.file) +if (IS_ERR (f.file)) { + error =PTR_ERR (f.file); goto out; + } can_sleep = !(cmd &LOCK_NB ); cmd &= ~LOCK_NB ; unlock = (cmd ==LOCK_UN ); + error = -EBADF ;if (!unlock && !(cmd &LOCK_MAND ) && !(f.file->f_mode & (FMODE_READ |FMODE_WRITE ))) goto out_putf;
fs/notify/fanotify/fanotify_user.c +9 -7
@@ -433,11 +433,12 @@ static int fanotify_find_path(int dfd, const char __user *filename, dfd, filename, flags);if (filename ==NULL ) { -struct fd f =fdget (dfd); +struct fd f =fdgetr (dfd,CAP_FSTAT ); - ret = -EBADF ; -if (!f.file) +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file); goto out; + } ret = -ENOTDIR ; if ((flags & FAN_MARK_ONLYDIR) &&
@@ -457,7 +458,8 @@ static int fanotify_find_path(int dfd, const char __user *filename,if (flags &FAN_MARK_ONLYDIR ) lookup_flags |=LOOKUP_DIRECTORY ; - ret =user_path_at (dfd, filename, lookup_flags, path); + ret =user_path_atr (dfd, filename, lookup_flags, path, +CAP_FSTAT ,CAP_LOOKUP );if (ret) goto out; }
@@ -822,9 +824,9 @@ SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,#endif return -EINVAL ; - f =fdget (fanotify_fd); -if (unlikely (!f.file)) -return -EBADF ; + f =fdgetr (fanotify_fd,CAP_NOTIFY ); +if (unlikely (IS_ERR (f.file))) +return PTR_ERR (f.file);/* verify that this is indeed an fanotify instance */ ret = -EINVAL ;
fs/notify/inotify/inotify_user.c +6 -6
@@ -711,9 +711,9 @@ SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,if (unlikely (!(mask &ALL_INOTIFY_BITS )))return -EINVAL ; - f =fdget (fd); -if (unlikely (!f.file)) -return -EBADF ; + f =fdgetr (fd,CAP_NOTIFY ); +if (unlikely (IS_ERR (f.file))) +return PTR_ERR (f.file);/* verify that this is indeed an inotify instance */ if (unlikely (f.file->f_op != &inotify_fops)) {
@@ -749,9 +749,9 @@ SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)struct fd f;int ret =0 ; - f =fdget (fd); -if (unlikely (!f.file)) -return -EBADF ; + f =fdgetr (fd,CAP_NOTIFY ); +if (unlikely (IS_ERR (f.file))) +return PTR_ERR (f.file);/* verify that this is indeed an inotify instance */ ret = -EINVAL ;
fs/ocfs2/cluster/heartbeat.c +6 -2
@@ -1740,9 +1740,13 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,if (fd <0 || fd >=INT_MAX ) goto out; - f =fdget (fd); -if (f.file ==NULL ) + f =fdgetr (fd,CAP_FSTAT ); +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file); +if (ret == -EBADF ) + ret = -EINVAL ; goto out; + }if (reg->hr_blocks ==0 || reg->hr_start_block ==0 || reg->hr_block_bytes ==0 )
fs/open.c +25 -17
105 lines not on this page · raw message
fs/proc/namespaces.c +3 -3
@@ -272,9 +272,9 @@ struct file *proc_ns_fget(int fd) {struct file *file; - file =fget (fd); -if (!file) -return ERR_PTR (-EBADF ); + file =fgetr (fd,CAP_SETNS ); +if (IS_ERR (file)) +return file;if (file->f_op != &ns_file_operations) goto out_invalid;
fs/read_write.c +65 -44
263 lines not on this page · raw message
fs/readdir.c +9 -9
@@ -110,14 +110,14 @@ SYSCALL_DEFINE3(old_readdir, unsigned int, fd, struct old_linux_dirent __user *, dirent, unsigned int, count) { int error; -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_READ );struct readdir_callback buf = { .ctx.actor = fillonedir, .dirent = dirent }; -if (!f.file) -return -EBADF ; +if (IS_ERR (f.file)) +return PTR_ERR (f.file); error =iterate_dir (f.file, &buf.ctx);if (buf.result)
@@ -206,9 +206,9 @@ SYSCALL_DEFINE3(getdents, unsigned int, fd,if (!access_ok (VERIFY_WRITE , dirent, count))return -EFAULT ; - f =fdget (fd); -if (!f.file) -return -EBADF ; + f =fdgetr (fd,CAP_READ ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file); error =iterate_dir (f.file, &buf.ctx);if (error >=0 )
@@ -286,9 +286,9 @@ SYSCALL_DEFINE3(getdents64, unsigned int, fd,if (!access_ok (VERIFY_WRITE , dirent, count))return -EFAULT ; - f =fdget (fd); -if (!f.file) -return -EBADF ; + f =fdgetr (fd,CAP_READ ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file); error =iterate_dir (f.file, &buf.ctx);if (error >=0 )
fs/select.c +7 -4
@@ -449,8 +449,8 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)break ;if (!(bit & all_bits))continue ; - f =fdget (i); -if (f.file) { + f =fdgetr (i,CAP_POLL_EVENT ); +if (!IS_ERR (f.file)) {const struct file_operations *f_op; f_op = f.file->f_op; mask =DEFAULT_POLLMASK ;
@@ -487,6 +487,9 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time) }else if (busy_flag & mask) can_busy_loop = true; + }else if (PTR_ERR (f.file) != -EBADF ) { + retval =PTR_ERR (f.file); +break ; } }if (res_in)
@@ -757,9 +760,9 @@ static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait, mask =0 ; fd = pollfd->fd;if (fd >=0 ) { -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_POLL_EVENT ); mask =POLLNVAL ; -if (f.file) { +if (!IS_ERR (f.file)) { mask =DEFAULT_POLLMASK ;if (f.file->f_op->poll) { pwait->_key = pollfd->events|POLLERR |POLLHUP ;
fs/signalfd.c +4 -3
@@ -288,9 +288,10 @@ SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,if (ufd <0 )kfree (ctx); }else { -struct fd f =fdget (ufd); -if (!f.file) -return -EBADF ; +struct fd f =fdgetr (ufd,CAP_FSIGNAL ); + +if (IS_ERR (f.file)) +return PTR_ERR (f.file); ctx = f.file->private_data;if (f.file->f_op != &signalfd_fops) {fdput (f);
fs/splice.c +23 -12
@@ -1647,14 +1647,16 @@ SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,return 0 ; error = -EBADF ; - f =fdget (fd); -if (f.file) { + f =fdgetr (fd,CAP_WRITE ); +if (!IS_ERR (f.file)) {if (f.file->f_mode &FMODE_WRITE ) error =vmsplice_to_pipe (f.file, iov, nr_segs, flags);else if (f.file->f_mode &FMODE_READ ) error =vmsplice_to_user (f.file, iov, nr_segs, flags);fdput (f); + }else { + error =PTR_ERR (f.file); }return error;
@@ -1692,19 +1694,23 @@ SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,return 0 ; error = -EBADF ; - in =fdget (fd_in); -if (in.file) { + in =fdgetr (fd_in,CAP_PREAD ); +if (!IS_ERR (in.file)) {if (in.file->f_mode &FMODE_READ ) { - out =fdget (fd_out); -if (out.file) { + out =fdgetr (fd_out,CAP_PWRITE ); +if (!IS_ERR (out.file)) {if (out.file->f_mode &FMODE_WRITE ) error =do_splice (in.file, off_in, out.file, off_out, len, flags);fdput (out); + }else { + error =PTR_ERR (out.file); } }fdput (in); + }else { + error =PTR_ERR (in.file); }return error; }
@@ -2023,19 +2029,24 @@ SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)return 0 ; error = -EBADF ; - in =fdget (fdin); -if (in.file) { + in =fdgetr (fdin,CAP_READ ); +if (!IS_ERR (in.file)) {if (in.file->f_mode &FMODE_READ ) { -struct fd out =fdget (fdout); -if (out.file) { +struct fd out =fdgetr (fdout,CAP_WRITE ); + +if (!IS_ERR (out.file)) {if (out.file->f_mode &FMODE_WRITE ) error =do_tee (in.file, out.file, len, flags);fdput (out); + }else { + error =PTR_ERR (out.file); } } -fdput (in); - } +fdput (in); + }else { + error =PTR_ERR (in.file); + }return error; }
fs/stat.c +6 -4
@@ -76,12 +76,14 @@ EXPORT_SYMBOL(vfs_getattr);int vfs_fstat (unsigned int fd,struct kstat *stat) { -struct fd f =fdget_raw (fd); -int error = -EBADF ; +struct fd f =fdgetr_raw (fd,CAP_FSTAT ); +int error; -if (f.file) { +if (!IS_ERR (f.file)) { error =vfs_getattr (&f.file->f_path, stat);fdput (f); + }else { + error =PTR_ERR (f.file); }return error; }
@@ -103,7 +105,7 @@ int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,if (flag &AT_EMPTY_PATH ) lookup_flags |=LOOKUP_EMPTY ; retry: - error =user_path_at (dfd, filename, lookup_flags, &path); + error =user_path_atr (dfd, filename, lookup_flags, &path,CAP_FSTAT );if (error) goto out;
fs/statfs.c +6 -3
@@ -94,11 +94,14 @@ retry:int fd_statfs (int fd,struct kstatfs *st) { -struct fd f =fdget_raw (fd); -int error = -EBADF ; -if (f.file) { +struct fd f =fdgetr_raw (fd,CAP_FSTATFS ); +int error; + +if (!IS_ERR (f.file)) { error =vfs_statfs (&f.file->f_path, st);fdput (f); + }else { + error =PTR_ERR (f.file); }return error; }
fs/sync.c +12 -9
@@ -148,12 +148,12 @@ void emergency_sync(void)*/ SYSCALL_DEFINE1(syncfs, int, fd) { -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_FSYNC );struct super_block *sb;int ret; -if (!f.file) -return -EBADF ; +if (IS_ERR (f.file)) +return PTR_ERR (f.file); sb = f.file->f_dentry->d_sb;down_read (&sb->s_umount);
@@ -199,12 +199,14 @@ EXPORT_SYMBOL(vfs_fsync);static int do_fsync (unsigned int fd,int datasync) { -struct fd f =fdget (fd); -int ret = -EBADF ; +struct fd f =fdgetr (fd,CAP_FSYNC ); +int ret; -if (f.file) { +if (!IS_ERR (f.file)) { ret =vfs_fsync (f.file, datasync);fdput (f); + }else { + ret =PTR_ERR (f.file); }return ret; }
@@ -310,10 +312,11 @@ SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,else endbyte--;/* inclusive */ - ret = -EBADF ; - f =fdget (fd); -if (!f.file) + f =fdgetr (fd,CAP_FSYNC ,CAP_SEEK ); +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file); goto out; + } i_mode =file_inode (f.file)->i_mode; ret = -ESPIPE ;
fs/timerfd.c +35 -5
@@ -291,6 +291,32 @@ static const struct file_operations timerfd_fops = { .llseek = noop_llseek, }; +#ifdef CONFIG_SECURITY_CAPSICUM +#define timerfd_fgetr (f, p, ...) \ + _timerfd_fgetr((f), (p), __VA_ARGS__, 0ULL) +static int _timerfd_fgetr (int fd,struct fd *p, ...) +{ +struct capsicum_rights rights; +struct fd f; +va_list ap; + +va_start (ap, p); + f =fdget_rights (fd,cap_rights_vinit (&rights, ap)); +va_end (ap); +if (IS_ERR (f.file)) +return PTR_ERR (f.file); +if (f.file->f_op != &timerfd_fops) { +fdput (f); +return -EINVAL ; + } + *p = f; +return 0 ; +} + +#else + +#define timerfd_fgetr (f, p, ...) \ + timerfd_fget((f), (p))static int timerfd_fget (int fd,struct fd *p) {struct fd f =fdget (fd);
@@ -304,6 +330,8 @@ static int timerfd_fget(int fd, struct fd *p)return 0 ; } +#endif + SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) {int ufd;
@@ -359,7 +387,7 @@ static int do_timerfd_settime(int ufd, int flags, !timespec_valid(&new->it_interval))return -EINVAL ; - ret =timerfd_fget (ufd, &f); + ret =timerfd_fgetr (ufd, &f,CAP_WRITE , (old ?CAP_READ :0 ));if (ret)return ret; ctx = f.file->private_data;
@@ -397,8 +425,10 @@ static int do_timerfd_settime(int ufd, int flags,hrtimer_forward_now (&ctx->t.tmr, ctx->tintv); } - old->it_value =ktime_to_timespec (timerfd_get_remaining (ctx)); - old->it_interval =ktime_to_timespec (ctx->tintv); +if (old) { + old->it_value =ktime_to_timespec (timerfd_get_remaining (ctx)); + old->it_interval =ktime_to_timespec (ctx->tintv); + }/* * Re-program the timer to the new value ...
@@ -414,7 +444,7 @@ static int do_timerfd_gettime(int ufd, struct itimerspec *t) {struct fd f;struct timerfd_ctx *ctx; -int ret =timerfd_fget (ufd, &f); +int ret =timerfd_fgetr (ufd, &f,CAP_READ );if (ret)return ret; ctx = f.file->private_data;
@@ -451,7 +481,7 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,if (copy_from_user (&new, utmr,sizeof (new)))return -EFAULT ; - ret =do_timerfd_settime (ufd, flags, &new, &old); + ret =do_timerfd_settime (ufd, flags, &new, otmr ? &old :NULL );if (ret)return ret;if (otmr &©_to_user (otmr, &old,sizeof (old)))
fs/utimes.c +6 -4
@@ -152,10 +152,11 @@ long do_utimes(int dfd, const char __user *filename, struct timespec *times,if (flags &AT_SYMLINK_NOFOLLOW ) goto out; - f =fdget (dfd); - error = -EBADF ; -if (!f.file) + f =fdgetr (dfd,CAP_FUTIMES ); +if (IS_ERR (f.file)) { + error =PTR_ERR (f.file); goto out; + } error =utimes_common (&f.file->f_path, times);fdput (f);
@@ -166,7 +167,8 @@ long do_utimes(int dfd, const char __user *filename, struct timespec *times,if (!(flags &AT_SYMLINK_NOFOLLOW )) lookup_flags |=LOOKUP_FOLLOW ; retry: - error =user_path_at (dfd, filename, lookup_flags, &path); + error =user_path_atr (dfd, filename, lookup_flags, &path, +CAP_FUTIMESAT );if (error) goto out;
fs/xattr.c +13 -13
@@ -415,12 +415,12 @@ retry: SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name, const void __user *,value, size_t, size, int, flags) { -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_EXTATTR_SET );struct dentry *dentry; -int error = -EBADF ; +int error; -if (!f.file) -return error; +if (IS_ERR (f.file)) +return PTR_ERR (f.file); dentry = f.file->f_path.dentry;audit_inode (NULL , dentry,0 ); error =mnt_want_write_file (f.file);
@@ -522,11 +522,11 @@ retry: SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name, void __user *, value, size_t, size) { -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_EXTATTR_GET );ssize_t error = -EBADF ; -if (!f.file) -return error; +if (IS_ERR (f.file)) +return PTR_ERR (f.file);audit_inode (NULL , f.file->f_path.dentry,0 ); error =getxattr (f.file->f_path.dentry, name, value, size);fdput (f);
@@ -611,11 +611,11 @@ retry: SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size) { -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_EXTATTR_LIST );ssize_t error = -EBADF ; -if (!f.file) -return error; +if (IS_ERR (f.file)) +return PTR_ERR (f.file);audit_inode (NULL , f.file->f_path.dentry,0 ); error =listxattr (f.file->f_path.dentry, list, size);fdput (f);
@@ -688,12 +688,12 @@ retry: SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name) { -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_EXTATTR_DELETE );struct dentry *dentry;int error = -EBADF ; -if (!f.file) -return error; +if (IS_ERR (f.file)) +return PTR_ERR (f.file); dentry = f.file->f_path.dentry;audit_inode (NULL , dentry,0 ); error =mnt_want_write_file (f.file);
fs/xfs/xfs_ioctl.c +7 -7
@@ -76,9 +76,9 @@ xfs_find_handle(struct xfs_inode *ip;if (cmd ==XFS_IOC_FD_TO_HANDLE ) { - f =fdget (hreq->fd); -if (!f.file) -return -EBADF ; + f =fdgetr (hreq->fd,CAP_FSTAT ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file); inode =file_inode (f.file); }else { error =user_lpath ((const char __user *)hreq->path, &path);
@@ -1443,8 +1443,8 @@ xfs_ioc_swapext(int error =0 ;/* Pull information for the target fd */ - f =fdget ((int )sxp->sx_fdtarget); -if (!f.file) { + f =fdgetr ((int )sxp->sx_fdtarget,CAP_READ ,CAP_WRITE ,CAP_FSTAT ); +if (IS_ERR (f.file)) { error =XFS_ERROR (EINVAL ); goto out; }
@@ -1456,8 +1456,8 @@ xfs_ioc_swapext( goto out_put_file; } - tmp =fdget ((int )sxp->sx_fdtmp); -if (!tmp.file) { + tmp =fdgetr ((int )sxp->sx_fdtmp,CAP_READ ,CAP_WRITE ,CAP_FSTAT ); +if (IS_ERR (tmp.file)) { error =XFS_ERROR (EINVAL ); goto out_put_file; }
ipc/mqueue.c +15 -15
@@ -978,9 +978,9 @@ SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,audit_mq_sendrecv (mqdes, msg_len, msg_prio, timeout ? &ts :NULL ); - f =fdget (mqdes); -if (unlikely (!f.file)) { - ret = -EBADF ; + f =fdgetr (mqdes,CAP_WRITE ); +if (unlikely (IS_ERR (f.file))) { + ret =PTR_ERR (f.file); goto out; }
@@ -1094,9 +1094,9 @@ SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,audit_mq_sendrecv (mqdes, msg_len,0 , timeout ? &ts :NULL ); - f =fdget (mqdes); -if (unlikely (!f.file)) { - ret = -EBADF ; + f =fdgetr (mqdes,CAP_READ ); +if (unlikely (IS_ERR (f.file))) { + ret =PTR_ERR (f.file); goto out; }
@@ -1229,9 +1229,9 @@ SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,skb_put (nc,NOTIFY_COOKIE_LEN );/* and attach it to the socket */ retry: - f =fdget (notification.sigev_signo); -if (!f.file) { - ret = -EBADF ; + f =fdgetr (notification.sigev_signo,CAP_POLL_EVENT ); +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file); goto out; } sock =netlink_getsockbyfilp (f.file);
@@ -1254,9 +1254,9 @@ retry: } } - f =fdget (mqdes); -if (!f.file) { - ret = -EBADF ; + f =fdgetr (mqdes,CAP_POLL_EVENT ); +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file); goto out; }
@@ -1328,9 +1328,9 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,return -EINVAL ; } - f =fdget (mqdes); -if (!f.file) { - ret = -EBADF ; + f =fdgetr (mqdes,CAP_POLL_EVENT ); +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file); goto out; }
kernel/events/core.c +8 -7
@@ -603,11 +603,11 @@ static inline int perf_cgroup_connect(int fd, struct perf_event *event, {struct perf_cgroup *cgrp;struct cgroup_subsys_state *css; -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_FSTAT );int ret =0 ; -if (!f.file) -return -EBADF ; +if (IS_ERR (f.file)) +return PTR_ERR (f.file); css =css_tryget_online_from_dir (f.file->f_dentry, &perf_event_cgrp_subsys);
@@ -3636,9 +3636,10 @@ static const struct file_operations perf_fops;static inline int perf_fget_light (int fd,struct fd *p) { -struct fd f =fdget (fd); -if (!f.file) -return -EBADF ; +struct fd f =fdgetr (fd,CAP_WRITE ); + +if (IS_ERR (f.file)) +return PTR_ERR (f.file);if (f.file->f_op != &perf_fops) {fdput (f);
@@ -3689,7 +3690,7 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)int ret;if (arg !=-1 ) {struct perf_event *output_event; -struct fd output; +struct fd output = { .file =NULL }; ret =perf_fget_light (arg, &output);if (ret)return ret;
kernel/module.c +7 -3
@@ -2513,14 +2513,18 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,/* Sets info->hdr and info->len. */ static int copy_module_from_fd (int fd,struct load_info *info) { -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_FEXECVE );int err;struct kstat stat;loff_t pos;ssize_t bytes =0 ; -if (!f.file) -return -ENOEXEC ; +if (IS_ERR (f.file)) { + err =PTR_ERR (f.file); +if (err == -EBADF ) + err = -ENOEXEC ; +return err; + } err =security_kernel_module_from_file (f.file);if (err)
kernel/sys.c +3 -3
@@ -1634,9 +1634,9 @@ static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)struct inode *inode;int err; - exe =fdget (fd); -if (!exe.file) -return -EBADF ; + exe =fdgetr (fd,CAP_FEXECVE ); +if (IS_ERR (exe.file)) +return PTR_ERR (exe.file); inode =file_inode (exe.file);
kernel/taskstats.c +2 -2
@@ -437,8 +437,8 @@ static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)return -EINVAL ; fd =nla_get_u32 (info->attrs[CGROUPSTATS_CMD_ATTR_FD ]); - f =fdget (fd); -if (!f.file) + f =fdgetr (fd,CAP_FSTAT ); +if (IS_ERR (f.file))return 0 ; size =nla_total_size (sizeof (struct cgroupstats ));
kernel/time/posix-clock.c +16 -11
@@ -246,13 +246,18 @@ struct posix_clock_desc {struct posix_clock *clk; }; -static int get_clock_desc (const clockid_t id,struct posix_clock_desc *cd) +static int get_clock_desc (const clockid_t id,struct posix_clock_desc *cd, +u64 right) { -struct file *fp =fget (CLOCKID_TO_FD (id)); +struct file *fp =fgetr (CLOCKID_TO_FD (id), right);int err = -EINVAL ; -if (!fp) +if (IS_ERR (fp)) { + err =PTR_ERR (fp); +if (err == -EBADF ) + err = -EINVAL ;return err; + }if (fp->f_op->open != posix_clock_open || !fp->private_data) goto out;
@@ -278,7 +283,7 @@ static int pc_clock_adjtime(clockid_t id, struct timex *tx)struct posix_clock_desc cd;int err; - err =get_clock_desc (id, &cd); + err =get_clock_desc (id, &cd,CAP_WRITE );if (err)return err;
@@ -302,7 +307,7 @@ static int pc_clock_gettime(clockid_t id, struct timespec *ts)struct posix_clock_desc cd;int err; - err =get_clock_desc (id, &cd); + err =get_clock_desc (id, &cd,CAP_READ );if (err)return err;
@@ -321,7 +326,7 @@ static int pc_clock_getres(clockid_t id, struct timespec *ts)struct posix_clock_desc cd;int err; - err =get_clock_desc (id, &cd); + err =get_clock_desc (id, &cd,CAP_READ );if (err)return err;
@@ -340,7 +345,7 @@ static int pc_clock_settime(clockid_t id, const struct timespec *ts)struct posix_clock_desc cd;int err; - err =get_clock_desc (id, &cd); + err =get_clock_desc (id, &cd,CAP_WRITE );if (err)return err;
@@ -365,7 +370,7 @@ static int pc_timer_create(struct k_itimer *kit)struct posix_clock_desc cd;int err; - err =get_clock_desc (id, &cd); + err =get_clock_desc (id, &cd,CAP_WRITE );if (err)return err;
@@ -385,7 +390,7 @@ static int pc_timer_delete(struct k_itimer *kit)struct posix_clock_desc cd;int err; - err =get_clock_desc (id, &cd); + err =get_clock_desc (id, &cd,CAP_WRITE );if (err)return err;
@@ -404,7 +409,7 @@ static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)clockid_t id = kit->it_clock;struct posix_clock_desc cd; -if (get_clock_desc (id, &cd)) +if (get_clock_desc (id, &cd,CAP_READ ))return ;if (cd.clk->ops.timer_gettime)
@@ -420,7 +425,7 @@ static int pc_timer_settime(struct k_itimer *kit, int flags,struct posix_clock_desc cd;int err; - err =get_clock_desc (id, &cd); + err =get_clock_desc (id, &cd,CAP_WRITE );if (err)return err;
mm/fadvise.c +4 -3
@@ -27,7 +27,7 @@*/ SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice) { -struct fd f =fdget (fd); +struct fd f;struct address_space *mapping;struct backing_dev_info *bdi;loff_t endbyte;/* inclusive */
@@ -36,8 +36,9 @@ SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)unsigned long nrpages;int ret =0 ; -if (!f.file) -return -EBADF ; + f =fdgetr (fd,CAP_LIST_END ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file);if (S_ISFIFO (file_inode (f.file)->i_mode)) { ret = -ESPIPE ;
mm/internal.h +19 -0
@@ -13,6 +13,8 @@#include <linux/fs.h> #include <linux/mm.h> +#include <linux/mman.h> +#include <linux/capsicum.h> void free_pgtables (struct mmu_gather *tlb,struct vm_area_struct *start_vma,unsigned long floor,unsigned long ceiling);
@@ -91,6 +93,23 @@ static inline void get_page_foll(struct page *page) } } +static inline struct capsicum_rights * +mmap_rights (struct capsicum_rights *rights, +unsigned long prot, +unsigned long flags) +{ +#ifdef CONFIG_SECURITY_CAPSICUM +cap_rights_init (rights,CAP_MMAP ); +if (prot &PROT_READ ) +cap_rights_set (rights,CAP_MMAP_R ); +if ((flags &MAP_SHARED ) && (prot &PROT_WRITE )) +cap_rights_set (rights,CAP_MMAP_W ); +if (prot &PROT_EXEC ) +cap_rights_set (rights,CAP_MMAP_X ); +#endif +return rights; +} +extern unsigned long highest_memmap_pfn;/*
mm/memcontrol.c +6 -6
@@ -5868,9 +5868,9 @@ static ssize_t memcg_write_event_control(struct kernfs_open_file *of,init_waitqueue_func_entry (&event->wait, memcg_event_wake);INIT_WORK (&event->remove, memcg_event_remove); - efile =fdget (efd); -if (!efile.file) { - ret = -EBADF ; + efile =fdgetr (efd,CAP_WRITE ); +if (IS_ERR (efile.file)) { + ret =PTR_ERR (efile.file); goto out_kfree; }
@@ -5880,9 +5880,9 @@ static ssize_t memcg_write_event_control(struct kernfs_open_file *of, goto out_put_efile; } - cfile =fdget (cfd); -if (!cfile.file) { - ret = -EBADF ; + cfile =fdgetr (cfd,CAP_READ ); +if (IS_ERR (cfile.file)) { + ret =PTR_ERR (cfile.file); goto out_put_eventfd; }
mm/mmap.c +5 -2
@@ -1381,10 +1381,13 @@ SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,unsigned long retval = -EBADF ;if (!(flags &MAP_ANONYMOUS )) { +struct capsicum_rights rights;audit_mmap_fd (fd, flags); - file =fget (fd); -if (!file) + file =fget_rights (fd,mmap_rights (&rights, prot, flags)); +if (IS_ERR (file)) { + retval =PTR_ERR (file); goto out; + }if (is_file_hugepages (file)) len =ALIGN (len,huge_page_size (hstate_file (file))); retval = -EINVAL ;
mm/nommu.c +7 -3
@@ -1496,13 +1496,17 @@ SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, unsigned long, fd, unsigned long, pgoff) { struct file *file = NULL; -unsigned long retval = -EBADF ; +unsigned long retval;audit_mmap_fd (fd, flags);if (!(flags &MAP_ANONYMOUS )) { - file =fget (fd); -if (!file) +struct capsicum_rights rights; + + file =fget_rights (fd,mmap_rights (&rights, prot, flags)); +if (IS_ERR (file)) { + retval =PTR_ERR (file); goto out; + } } flags &= ~(MAP_EXECUTABLE |MAP_DENYWRITE );
mm/readahead.c +4 -2
@@ -566,8 +566,8 @@ SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count)struct fd f; ret = -EBADF ; - f =fdget (fd); -if (f.file) { + f =fdgetr (fd,CAP_PREAD ); +if (!IS_ERR (f.file)) {if (f.file->f_mode &FMODE_READ ) {struct address_space *mapping = f.file->f_mapping;pgoff_t start = offset >>PAGE_CACHE_SHIFT ;
@@ -576,6 +576,8 @@ SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count) ret =do_readahead (mapping, f.file, start, len); }fdput (f); + }else { + ret =PTR_ERR (f.file); }return ret; }
net/9p/trans_fd.c +5 -5
@@ -789,12 +789,12 @@ static int p9_fd_open(struct p9_client *client, int rfd, int wfd)if (!ts)return -ENOMEM ; - ts->rd =fget (rfd); - ts->wr =fget (wfd); -if (!ts->rd || !ts->wr) { -if (ts->rd) + ts->rd =fgetr (rfd,CAP_READ ,CAP_POLL_EVENT ); + ts->wr =fgetr (wfd,CAP_WRITE ,CAP_POLL_EVENT ); +if (IS_ERR (ts->rd) ||IS_ERR (ts->wr)) { +if (!IS_ERR (ts->rd))fput (ts->rd); -if (ts->wr) +if (!IS_ERR (ts->wr))fput (ts->wr);kfree (ts);return -EIO ;
sound/core/pcm_native.c +7 -3
@@ -1611,10 +1611,14 @@ static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)struct snd_pcm_file *pcm_file;struct snd_pcm_substream *substream1;struct snd_pcm_group *group; -struct fd f =fdget (fd); +struct fd f =fdgetr (fd,CAP_LIST_END ); -if (!f.file) -return -EBADFD ; +if (IS_ERR (f.file)) { + res =PTR_ERR (f.file); +if (res == -EBADF ) +return -EBADFD ; +return res; + }if (!is_pcm_file (f.file)) { res = -EBADFD ; goto _badf;
virt/kvm/eventfd.c +3 -3
@@ -311,9 +311,9 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)INIT_WORK (&irqfd->inject, irqfd_inject);INIT_WORK (&irqfd->shutdown, irqfd_shutdown); - f =fdget (args->fd); -if (!f.file) { - ret = -EBADF ; + f =fdgetr (args->fd,CAP_WRITE ); +if (IS_ERR (f.file)) { + ret =PTR_ERR (f.file); goto out; }
virt/kvm/vfio.c +6 -6
@@ -124,9 +124,9 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)if (get_user (fd, argp))return -EFAULT ; - f =fdget (fd); -if (!f.file) -return -EBADF ; + f =fdgetr (fd,CAP_FSTAT ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file); vfio_group =kvm_vfio_group_get_external_user (f.file);fdput (f);
@@ -164,9 +164,9 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)if (get_user (fd, argp))return -EFAULT ; - f =fdget (fd); -if (!f.file) -return -EBADF ; + f =fdgetr (fd,CAP_FSTAT ); +if (IS_ERR (f.file)) +return PTR_ERR (f.file); vfio_group =kvm_vfio_group_get_external_user (f.file);fdput (f);
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 6/11 capsicum: implement sockfd_lookupr()
In reply to David DrysdaleAdd variants of sockfd_lookup() and related functions where the caller indicates the operations that will be performed on the socket.
If CONFIG_SECURITY_CAPSICUM is defined, these variants use the fgetr()-style functions to retrieve the struct file from the file descriptor.
If CONFIG_SECURITY_CAPSICUM is not defined, these variants use the normal fget() functions.
--- include/linux/net.h | 16 +++++++ net/socket.c | 120 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 119 insertions(+), 17 deletions(-)
include/linux/net.h +16 -0
@@ -24,6 +24,7 @@#include <linux/fcntl.h> /* For O_CLOEXEC and O_NONBLOCK */ #include <linux/kmemcheck.h> #include <linux/rcupdate.h> +#include <linux/capsicum.h> #include <linux/jump_label.h> #include <uapi/linux/net.h>
@@ -222,6 +223,21 @@ struct socket *sock_from_file(struct file *file, int *err);#define sockfd_put (sock) fput(sock->file)int net_ratelimit (void ); +#ifdef CONFIG_SECURITY_CAPSICUM +struct socket *sockfd_lookup_rights (int fd,int *err, +struct capsicum_rights *rights); +struct socket *_sockfd_lookupr (int fd,int *err, ...); +#define sockfd_lookupr (fd, err, ...) \ + _sockfd_lookupr((fd), (err), __VA_ARGS__, 0ULL) +#else +static inline struct socket * +sockfd_lookup_rights (int fd,int *err,struct capsicum_rights *rights) +{ +return sockfd_lookup (fd, err); +} +#define sockfd_lookupr (fd, err, ...) sockfd_lookup((fd), (err)) +#endif +#define net_ratelimited_function (function, ...) \ do { \ if (net_ratelimit()) \
net/socket.c +103 -17
138 lines not on this page · raw message
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 7/11 capsicum: convert callers to use sockfd_lookupr() etc
In reply to David DrysdaleConvert places that use sockfd_lookup() functions to use the equivalent sockfd_lookupr() variant instead.
Annotate each such call with an indication of what operations will be performed on the retrieved socket, to allow future policing of rights associated with file descriptors.
--- drivers/block/nbd.c | 3 +- drivers/scsi/iscsi_tcp.c | 2 +- drivers/staging/usbip/stub_dev.c | 2 +- drivers/staging/usbip/vhci_sysfs.c | 2 +- drivers/vhost/net.c | 2 +- fs/ncpfs/inode.c | 5 +- net/bluetooth/bnep/sock.c | 2 +- net/bluetooth/cmtp/sock.c | 2 +- net/bluetooth/hidp/sock.c | 4 +- net/compat.c | 4 +- net/l2tp/l2tp_core.c | 11 ++-- net/l2tp/l2tp_core.h | 2 + net/sched/sch_atm.c | 2 +- net/socket.c | 119 +++++++++++++++++++++++-------------- net/sunrpc/svcsock.c | 4 +- 15 files changed, 100 insertions(+), 66 deletions(-)
drivers/block/nbd.c +2 -1
@@ -643,7 +643,8 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,int err;if (nbd->sock)return -EBUSY ; - sock =sockfd_lookup (arg, &err); + sock =sockfd_lookupr (arg, &err, +CAP_READ ,CAP_WRITE ,CAP_SHUTDOWN );if (sock) { nbd->sock = sock;if (max_part >0 )
drivers/scsi/iscsi_tcp.c +1 -1
@@ -652,7 +652,7 @@ iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,int err;/* lookup for existing socket */ - sock =sockfd_lookup ((int )transport_eph, &err); + sock =sockfd_lookupr ((int )transport_eph, &err,CAP_SOCK_SERVER );if (!sock) {iscsi_conn_printk (KERN_ERR , conn,"sockfd_lookup failed %d\n" , err);
drivers/staging/usbip/stub_dev.c +1 -1
@@ -109,7 +109,7 @@ static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr, goto err; } - socket =sockfd_lookup (sockfd, &err); + socket =sockfd_lookupr (sockfd, &err,CAP_LIST_END );if (!socket) goto err;
drivers/staging/usbip/vhci_sysfs.c +1 -1
@@ -195,7 +195,7 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr,return -EINVAL ;/* Extract socket from fd. */ - socket =sockfd_lookup (sockfd, &err); + socket =sockfd_lookupr (sockfd, &err,CAP_LIST_END );if (!socket)return -EINVAL ;
drivers/vhost/net.c +1 -1
@@ -843,7 +843,7 @@ static struct socket *get_raw_socket(int fd)char buf[MAX_ADDR_LEN ]; } uaddr;int uaddr_len =sizeof uaddr, r; -struct socket *sock =sockfd_lookup (fd, &r); +struct socket *sock =sockfd_lookupr (fd, &r,CAP_READ ,CAP_WRITE );if (!sock)return ERR_PTR (-ENOTSOCK );
fs/ncpfs/inode.c +3 -2
@@ -539,7 +539,7 @@ static int ncp_fill_super(struct super_block *sb, void *raw_data, int silent)if (!uid_valid (data.mounted_uid) || !uid_valid (data.uid) || !gid_valid (data.gid)) goto out; - sock =sockfd_lookup (data.ncp_fd, &error); + sock =sockfd_lookupr (data.ncp_fd, &error,CAP_WRITE ,CAP_FSTAT );if (!sock) goto out;
@@ -567,7 +567,8 @@ static int ncp_fill_super(struct super_block *sb, void *raw_data, int silent) server->ncp_sock = sock;if (data.info_fd !=-1 ) { -struct socket *info_sock =sockfd_lookup (data.info_fd, &error); +struct socket *info_sock =sockfd_lookupr (data.info_fd, &error, +CAP_WRITE ,CAP_FSTAT );if (!info_sock) goto out_bdi; server->info_sock = info_sock;
net/bluetooth/bnep/sock.c +1 -1
@@ -69,7 +69,7 @@ static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned longif (copy_from_user (&ca, argp,sizeof (ca)))return -EFAULT ; - nsock =sockfd_lookup (ca.sock, &err); + nsock =sockfd_lookupr (ca.sock, &err,CAP_READ ,CAP_WRITE );if (!nsock)return err;
net/bluetooth/cmtp/sock.c +1 -1
@@ -83,7 +83,7 @@ static int cmtp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned longif (copy_from_user (&ca, argp,sizeof (ca)))return -EFAULT ; - nsock =sockfd_lookup (ca.sock, &err); + nsock =sockfd_lookupr (ca.sock, &err,CAP_READ ,CAP_WRITE );if (!nsock)return err;
net/bluetooth/hidp/sock.c +2 -2
@@ -67,11 +67,11 @@ static int hidp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned longif (copy_from_user (&ca, argp,sizeof (ca)))return -EFAULT ; - csock =sockfd_lookup (ca.ctrl_sock, &err); + csock =sockfd_lookupr (ca.ctrl_sock, &err,CAP_READ ,CAP_WRITE );if (!csock)return err; - isock =sockfd_lookup (ca.intr_sock, &err); + isock =sockfd_lookupr (ca.intr_sock, &err,CAP_READ ,CAP_WRITE );if (!isock) {sockfd_put (csock);return err;
net/compat.c +2 -2
@@ -388,7 +388,7 @@ COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, char __user *, optval, unsigned int, optlen) { int err; -struct socket *sock =sockfd_lookup (fd, &err); +struct socket *sock =sockfd_lookupr (fd, &err,CAP_SETSOCKOPT );if (sock) { err =security_socket_setsockopt (sock, level, optname);
@@ -508,7 +508,7 @@ COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname, char __user *, optval, int __user *, optlen) { int err; -struct socket *sock =sockfd_lookup (fd, &err); +struct socket *sock =sockfd_lookupr (fd, &err,CAP_GETSOCKOPT );if (sock) { err =security_socket_getsockopt (sock, level, optname);
net/l2tp/l2tp_core.c +7 -4
@@ -175,7 +175,8 @@ l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)* owned by userspace. A struct sock returned from this function must be * released using l2tp_tunnel_sock_put once you're done with it. */ -static struct sock *l2tp_tunnel_sock_lookup (struct l2tp_tunnel *tunnel) +static struct sock *l2tp_tunnel_sock_lookup (struct l2tp_tunnel *tunnel, +struct capsicum_rights *rights) {int err =0 ;struct socket *sock =NULL ;
@@ -189,7 +190,7 @@ static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)* of closing it. Look the socket up using the fd to ensure * consistency. */ - sock =sockfd_lookup (tunnel->fd, &err); + sock =sockfd_lookup_rights (tunnel->fd, &err, rights);if (sock) sk = sock->sk; }else {
@@ -1314,9 +1315,11 @@ static void l2tp_tunnel_del_work(struct work_struct *work)struct l2tp_tunnel *tunnel =NULL ;struct socket *sock =NULL ;struct sock *sk =NULL ; +struct capsicum_rights rights; tunnel = container_of(work, struct l2tp_tunnel, del_work); - sk =l2tp_tunnel_sock_lookup (tunnel); + sk =l2tp_tunnel_sock_lookup (tunnel, +cap_rights_init (&rights,CAP_SHUTDOWN ));if (!sk)return ;
@@ -1522,7 +1525,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32if (err <0 ) goto err; }else { - sock =sockfd_lookup (fd, &err); + sock =sockfd_lookupr (fd, &err,CAP_READ ,CAP_WRITE );if (!sock) {pr_err ("tunl %u: sockfd_lookup(fd=%d) returned %d\n" , tunnel_id, fd, err);
net/l2tp/l2tp_core.h +2 -0
@@ -11,6 +11,8 @@#ifndef _L2TP_CORE_H_#define _L2TP_CORE_H_ +#include <linux/capsicum.h> +/* Just some random numbers */ #define L2TP_TUNNEL_MAGIC 0x42114DDA#define L2TP_SESSION_MAGIC 0x0C04EB7D
net/sched/sch_atm.c +1 -1
@@ -238,7 +238,7 @@ static int atm_tc_change(struct Qdisc *sch, u32 classid, u32 parent, }pr_debug ("atm_tc_change: type %d, payload %d, hdr_len %d\n" , opt->nla_type,nla_len (opt), hdr_len); - sock =sockfd_lookup (fd, &error); + sock =sockfd_lookupr (fd, &error,CAP_GETSOCKNAME );if (!sock)return error;/* f_count++ */ pr_debug ("atm_tc_change: f_count %ld\n" ,file_count (sock->file));
net/socket.c +73 -46
266 lines not on this page · raw message
net/sunrpc/svcsock.c +2 -2
@@ -1413,7 +1413,7 @@ static struct svc_sock *svc_setup_socket(struct svc_serv *serv,bool svc_alien_sock (struct net *net,int fd) {int err; -struct socket *sock =sockfd_lookup (fd, &err); +struct socket *sock =sockfd_lookupr (fd, &err,CAP_LIST_END );bool ret = false;if (!sock)
@@ -1441,7 +1441,7 @@ int svc_addsock(struct svc_serv *serv, const int fd, char *name_return, const size_t len) {int err =0 ; -struct socket *so =sockfd_lookup (fd, &err); +struct socket *so =sockfd_lookupr (fd, &err,CAP_LISTEN );struct svc_sock *svsk =NULL ;struct sockaddr_storage addr;struct sockaddr *sin = (struct sockaddr *)&addr;
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 8/11 capsicum: invoke Capsicum on FD/file conversion
In reply to David DrysdaleAdd calls to Capsicum to intercept FD/file conversion in the following situations:
- Any place where a file descriptor is converted to a struct file.
- Any place where a new file descriptor that is derived from an existing file descriptor is installed into the fdtable.
For the first of these, Capsicum checks for a Capsicum capability wrapper file and unwraps it to the underlying file, provided the appropriate rights are available (otherwise, ERR_PTR(-ENOTCAPABLE) is returned).
For the second, Capsicum checks whether the original file has restricted rights associated with it. If it does, it is replaced with a Capsicum capability wrapper file before installation into the fdtable. This propagates the rights from the original FD to the new FD, and particularly affects accept(2) and openat(2).
For openat(2) rights propagation in particular, the rights associated with the dfd need to make their way through the code in fs/namei.c to allow this.
The path walking code in fs/namei.c is also modified to enable the O_BENEATH flag if the dfd is a Capsicum capability.
--- arch/powerpc/platforms/cell/spufs/coredump.c | 2 + fs/file.c | 7 +- fs/locks.c | 3 + fs/namei.c | 223 +++++++++++++++++++------ fs/notify/dnotify/dnotify.c | 2 + fs/proc/fd.c | 18 +- include/linux/capsicum.h | 22 +++ include/uapi/asm-generic/errno.h | 3 + net/socket.c | 10 +- security/Makefile | 2 +- security/capsicum.c | 238 +++++++++++++++++++++++++++ 11 files changed, 468 insertions(+), 62 deletions(-) create mode 100644 security/capsicum.c
arch/powerpc/platforms/cell/spufs/coredump.c +2 -0
@@ -29,6 +29,7 @@#include <linux/syscalls.h> #include <linux/coredump.h> #include <linux/binfmts.h> +#include <linux/capsicum.h> #include <asm/uaccess.h>
@@ -101,6 +102,7 @@ static struct spu_context *coredump_next_context(int *fd)return NULL ; *fd = n -1 ; file =fcheck (*fd); + file =capsicum_file_lookup (file,NULL ,NULL );return SPUFS_I (file_inode (file))->i_ctx; }
fs/file.c +4 -3
@@ -14,6 +14,7 @@#include <linux/time.h> #include <linux/sched.h> #include <linux/security.h> +#include <linux/capsicum.h> #include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/file.h>
@@ -720,8 +721,8 @@ unsigned long __fdget_pos(unsigned int fd)#ifdef CONFIG_SECURITY_CAPSICUM /* -* We might want to change the return value of fget() and friends. This -* function is called with the intended return value, and fget() will /actually/ +* Capsicum might want to change the return value of fget() and friends. This +* function is called with the intended return value, and fget() will actually * return whatever is returned from here. We adjust the reference counter if * necessary. */
@@ -736,7 +737,7 @@ static struct file *unwrap_file(struct file *orig,return ERR_PTR (-EBADF );if (IS_ERR (orig))return orig; - f = orig;/* TODO: change the value of f here */ + f =capsicum_file_lookup (orig, required_rights, actual_rights);if (f != orig && update_refcnt) {/* We're not returning the original, and the calling code * has already incremented the refcount on it, we need to
fs/locks.c +3 -0
@@ -121,6 +121,7 @@#include <linux/init.h> #include <linux/module.h> #include <linux/security.h> +#include <linux/capsicum.h> #include <linux/slab.h> #include <linux/syscalls.h> #include <linux/time.h>
@@ -2133,6 +2134,7 @@ again:*/ spin_lock (¤t->files->file_lock); f =fcheck (fd); + f =capsicum_file_lookup (f,NULL ,NULL );spin_unlock (¤t->files->file_lock);if (!error && f != filp && flock.l_type !=F_UNLCK ) { flock.l_type =F_UNLCK ;
@@ -2267,6 +2269,7 @@ again:*/ spin_lock (¤t->files->file_lock); f =fcheck (fd); + f =capsicum_file_lookup (f,NULL ,NULL );spin_unlock (¤t->files->file_lock);if (!error && f != filp && flock.l_type !=F_UNLCK ) { flock.l_type =F_UNLCK ;
fs/namei.c +172 -51
503 lines not on this page · raw message
fs/notify/dnotify/dnotify.c +2 -0
@@ -25,6 +25,7 @@#include <linux/slab.h> #include <linux/fdtable.h> #include <linux/fsnotify_backend.h> +#include <linux/capsicum.h> int dir_notify_enable__read_mostly =1 ;
@@ -327,6 +328,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)rcu_read_lock (); f =fcheck (fd); + f =capsicum_file_lookup (f,NULL ,NULL );rcu_read_unlock ();/* if (f != filp) means that we lost a race and another task/thread
fs/proc/fd.c +13 -5
@@ -6,6 +6,7 @@#include <linux/namei.h> #include <linux/pid.h> #include <linux/security.h> +#include <linux/capsicum.h> #include <linux/file.h> #include <linux/seq_file.h>
@@ -20,6 +21,7 @@ static int seq_show(struct seq_file *m, void *v)struct files_struct *files =NULL ;int f_flags =0 , ret = -ENOENT ;struct file *file =NULL ; +struct file *underlying =NULL ;struct task_struct *task; task =get_proc_task (m->private);
@@ -36,12 +38,13 @@ static int seq_show(struct seq_file *m, void *v) file =fcheck_files (files, fd);if (file) {struct fdtable *fdt =files_fdtable (files); - - f_flags = file->f_flags; + underlying =capsicum_file_lookup (file,NULL ,NULL ); + f_flags = underlying->f_flags;if (close_on_exec (fd, fdt)) f_flags |=O_CLOEXEC ;get_file (file); +get_file (underlying); ret =0 ; }spin_unlock (&files->file_lock);
@@ -50,10 +53,11 @@ static int seq_show(struct seq_file *m, void *v)if (!ret) {seq_printf (m,"pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n" , - (long long )file->f_pos, f_flags, -real_mount (file->f_path.mnt)->mnt_id); + (long long )underlying->f_pos, f_flags, +real_mount (underlying->f_path.mnt)->mnt_id);if (file->f_op->show_fdinfo) ret = file->f_op->show_fdinfo (m, file); +fput (underlying);fput (file); }
@@ -95,7 +99,10 @@ static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)rcu_read_lock (); file =fcheck_files (files, fd);if (file) { -unsigned f_mode = file->f_mode; +unsigned f_mode; + + file =capsicum_file_lookup (file,NULL ,NULL ); + f_mode = file->f_mode;rcu_read_unlock ();put_files_struct (files);
@@ -158,6 +165,7 @@ static int proc_fd_link(struct dentry *dentry, struct path *path)spin_lock (&files->file_lock); fd_file =fcheck_files (files, fd);if (fd_file) { + fd_file =capsicum_file_lookup (fd_file,NULL ,NULL ); *path = fd_file->f_path;path_get (&fd_file->f_path); ret =0 ;
include/linux/capsicum.h +22 -0
@@ -16,6 +16,13 @@ struct capsicum_rights {#define CAP_LIST_END 0ULL#ifdef CONFIG_SECURITY_CAPSICUM +/* FD->struct file interception functions */ +struct file *capsicum_file_lookup (struct file *file, +const struct capsicum_rights *required_rights, +const struct capsicum_rights **actual_rights); +struct file *capsicum_file_install (const struct capsicum_rights *base_rights, +struct file *file); +/* Rights manipulation functions */ #define cap_rights_init (rights, ...) \ _cap_rights_init((rights), __VA_ARGS__, CAP_LIST_END)
@@ -32,6 +39,21 @@ bool cap_rights_is_all(const struct capsicum_rights *rights);#else +static inline struct file * +capsicum_file_lookup (struct file *file, +const struct capsicum_rights *required_rights, +const struct capsicum_rights **actual_rights) +{ +return file; +} + +static inline struct file * +capsicum_file_install (const const struct capsicum_rights *base_rights, +struct file *file) +{ +return file; +} +#define cap_rights_init (rights, ...) _cap_rights_noop(rights)#define cap_rights_set (rights, ...) _cap_rights_noop(rights)#define cap_rights_set_all (rights) _cap_rights_noop(rights)
include/uapi/asm-generic/errno.h +3 -0
@@ -110,4 +110,7 @@#define EHWPOISON 133/* Memory page has hardware error */ +#define ECAPMODE 134/* Not permitted in capability mode */ +#define ENOTCAPABLE 135/* Capability FD rights insufficient */ +#endif
net/socket.c +8 -2
@@ -1671,6 +1671,7 @@ SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr, {struct socket *sock, *newsock;struct file *newfile; +struct file *installfile;int err, len, newfd, fput_needed;struct sockaddr_storage address;struct capsicum_rights rights;
@@ -1738,7 +1739,12 @@ SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,/* File flags are not inherited via accept() unlike another OSes. */ -fd_install (newfd, newfile); + installfile =capsicum_file_install (listen_rights, newfile); +if (IS_ERR (installfile)) { + err =PTR_ERR (installfile); + goto out_fd; + } +fd_install (newfd, installfile); err = newfd; out_put:
@@ -2117,7 +2123,7 @@ static int ___sys_sendmsg(struct socket *sock_noaddr, struct socket *sock_addr, } sock = (msg_sys->msg_name ? sock_addr : sock_noaddr);if (!sock) -return -EBADF ; +return -ENOTCAPABLE ;if (msg_sys->msg_iovlen >UIO_FASTIOV ) { err = -EMSGSIZE ;
security/Makefile +1 -1
@@ -14,7 +14,7 @@ obj-y += commoncap.o obj-$(CONFIG_MMU) += min_addr.o # Object file lists -obj-$(CONFIG_SECURITY) += security.o capability.o capsicum-rights.o +obj-$(CONFIG_SECURITY) += security.o capability.o capsicum.o capsicum-rights.o obj-$(CONFIG_SECURITYFS) += inode.o obj-$(CONFIG_SECURITY_SELINUX) += selinux/ obj-$(CONFIG_SECURITY_SMACK) += smack/
security/capsicum.c new file +238 -0
238 lines not on this page · raw message
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 9/11 capsicum: add syscalls to limit FD rights
In reply to David DrysdaleAdd the cap_rights_get(2) and cap_rights_set(2) syscalls to allow retrieval and modification of the rights associated with a file descriptor.
When a normal file descriptor has its rights restricted in any way, it becomes a Capsicum capability file descriptor. This is a wrapper struct file that is installed in the fdtable in place of the original file. From this point on, when the FD is converted to a struct file by fget() (or equivalent), the wrapper is checked for the appropriate rights and the wrapped inner normal file is returned.
When a Capsicum capability file descriptor has its rights restricted further (they cannot be expanded), a new wrapper is created with the restricted rights, also wrapping the same inner normal file. In other words, the .underlying field in a struct capsicum_capability is always a normal file, never another Capsicum capability file.
These syscalls specify the different components of the compound rights structure separately, allowing components to be unspecified for no change.
Note that in FreeBSD 10.x the function of this pair of syscalls is implemented as 3 distinct pairs of syscalls, one pair for each component of the compound rights (primary/fcntl/ioctl).
--- arch/x86/syscalls/syscall_64.tbl | 2 + include/linux/syscalls.h | 12 ++++ kernel/sys_ni.c | 4 ++ security/capsicum.c | 146 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+)
arch/x86/syscalls/syscall_64.tbl +2 -0
@@ -323,6 +323,8 @@ 314 common sched_setattr sys_sched_setattr 315 common sched_getattr sys_sched_getattr 316 common renameat2 sys_renameat2 +318 common cap_rights_limit sys_cap_rights_limit +319 common cap_rights_get sys_cap_rights_get # # x32-specific system call numbers start at 512 to avoid cache impact
include/linux/syscalls.h +12 -0
@@ -65,6 +65,7 @@ struct old_linux_dirent;struct perf_event_attr ;struct file_handle ;struct sigaltstack ; +struct cap_rights ;#include <linux/types.h> #include <linux/aio_abi.h>
@@ -866,4 +867,15 @@ asmlinkage long sys_process_vm_writev(pid_t pid,asmlinkage long sys_kcmp (pid_t pid1,pid_t pid2,int type,unsigned long idx1,unsigned long idx2);asmlinkage long sys_finit_module (int fd,const char __user *uargs,int flags); +asmlinkage long sys_cap_rights_limit (unsigned int orig_fd, +const struct cap_rights __user *new_rights, +unsigned int fcntls, +int nioctls, +unsigned int __user *ioctls); +asmlinkage long sys_cap_rights_get (unsigned int fd, +struct cap_rights __user *rightsp, +unsigned int __user *fcntls, +int __user *nioctls, +unsigned int __user *ioctls); +#endif
kernel/sys_ni.c +4 -0
@@ -213,3 +213,7 @@ cond_syscall(compat_sys_open_by_handle_at);/* compare kernel pointers */ cond_syscall (sys_kcmp); + +/* capsicum object capabilities */ +cond_syscall (sys_cap_rights_get); +cond_syscall (sys_cap_rights_limit);
security/capsicum.c +146 -0
152 lines not on this page · raw message
Signature · 1 line
2.0.0.526.g5318336
PATCH v2 10/11 capsicum: prctl(2) to force use of O_BENEATH
In reply to David DrysdaleAdd a per-task flag that indicates all openat(2) operations should implicitly have the O_BENEATH flag set.
Add a prctl(2) command to set this flag (irrevocably). Include an option to force the flag set to be synchronized across all tasks in the thread group.
--- fs/namei.c | 3 +++ include/linux/sched.h | 3 +++ include/uapi/linux/prctl.h | 14 ++++++++++++++ kernel/sys.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+)
fs/namei.c +3 -0
@@ -1849,6 +1849,9 @@ static int path_init(int dfd, const char *name, unsigned int *flags, nd->flags = (*flags) |LOOKUP_PARENT |LOOKUP_JUMPED ; nd->depth =0 ; +if (current->openat_beneath) + *flags |=LOOKUP_BENEATH ; +if ((*flags) &LOOKUP_ROOT ) {struct dentry *root = nd->root.dentry;struct inode *inode = root->d_inode;
include/linux/sched.h +3 -0
@@ -1303,6 +1303,9 @@ struct task_struct {/* Used for emulating ABI behavior of previous Linux versions */ unsigned int personality; +/* Indicate that openat(2) operations implictly have O_BENEATH */ + unsigned openat_beneath:1; + unsigned in_execve:1; /* Tell the LSMs that the process is doing an* execve */ unsigned in_iowait:1;
include/uapi/linux/prctl.h +14 -0
@@ -152,4 +152,18 @@#define PR_SET_THP_DISABLE 41#define PR_GET_THP_DISABLE 42 +/* +* If openat_beneath is set for a task, then all openat(2) operations will +* implicitly have the O_BENEATH flag set for them. Once set, this flag cannot +* be cleared. +*/ +#define PR_SET_OPENAT_BENEATH 44 +#define PR_GET_OPENAT_BENEATH 45 + +/* +Indicate that the openat_beneath flag should be synchronized across all +* threads in the process. +*/ +#define PR_SET_OPENAT_BENEATH_TSYNC 0x01 +#endif /* _LINUX_PRCTL_H */
kernel/sys.c +28 -0
@@ -1821,6 +1821,23 @@ out:return error; } +static int prctl_set_openat_beneath (struct task_struct *me,unsigned long flags) +{ + me->openat_beneath =1 ; +if (flags &PR_SET_OPENAT_BENEATH_TSYNC ) { +struct task_struct *thread, *caller; +unsigned long tflags; + +write_lock_irqsave (&tasklist_lock, tflags); + thread = caller = me; + while_each_thread(caller, thread) { + thread->openat_beneath =1 ; + } +write_unlock_irqrestore (&tasklist_lock, tflags); + } +return 0 ; +} +#ifdef CONFIG_CHECKPOINT_RESTORE static int prctl_get_tid_address (struct task_struct *me,int __user **tid_addr) {
@@ -1996,6 +2013,17 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;return current->no_new_privs ?1 :0 ; +case PR_SET_OPENAT_BENEATH : +if (arg2 !=1 || arg4 || arg5) +return -EINVAL ; +if ((arg3 & ~(PR_SET_OPENAT_BENEATH_TSYNC )) !=0 ) +return -EINVAL ; + error =prctl_set_openat_beneath (me, arg3); +break ; +case PR_GET_OPENAT_BENEATH : +if (arg2 || arg3 || arg4 || arg5) +return -EINVAL ; +return me->openat_beneath;case PR_GET_THP_DISABLE :if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;
Signature · 1 line
2.0.0.526.g5318336
Paolo Bonzini
maintainerRe: [PATCH 10/11] capsicum: prctl(2) to force use of O_BENEATH
Il 25/07/2014 15:47, David Drysdale ha scritto:
Quoted 9 lines
kernel/sys.c +4
-0@@ -1996,6 +2013,17 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;return current->no_new_privs ?1 :0 ; +case PR_SET_OPENAT_BENEATH : +if (arg2 !=1 || arg4 || arg5) +return -EINVAL ; +if ((arg3 & ~(PR_SET_OPENAT_BENEATH_TSYNC )) !=0 )
kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2029
Quoted 10 lines
kernel/sys.c +7
-0+return -EINVAL ; + error =prctl_set_openat_beneath (me, arg3); +break ; +case PR_GET_OPENAT_BENEATH : +if (arg2 || arg3 || arg4 || arg5) +return -EINVAL ; +return me->openat_beneath;case PR_GET_THP_DISABLE :if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;
Why are you always forbidding a change of prctl from 1 to 0? It should be safe if current->no_new_privs is clear.
Do new threads inherit from the parent?
Also, I wonder if you need something like this check:
/*
* Installing a seccomp filter requires that the task has
* CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
* This avoids scenarios where unprivileged tasks can affect the
* behavior of privileged children.
*/
if (!current->no_new_privs &&
security_capable_noaudit(current_cred(), current_user_ns(),
CAP_SYS_ADMIN) != 0)
return -EACCES;Paolo
Andy Lutomirski
reviewerRe: [PATCH 10/11] capsicum: prctl(2) to force use of O_BENEATH
Quoted 16 lines · Paolo Bonzini
Il 25/07/2014 15:47, David Drysdale ha scritto:
Quoted 14 lines
kernel/sys.c +10
-0@@ -1996,6 +2013,17 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;return current->no_new_privs ?1 :0 ; +case PR_SET_OPENAT_BENEATH : +if (arg2 !=1 || arg4 || arg5) +return -EINVAL ; +if ((arg3 & ~(PR_SET_OPENAT_BENEATH_TSYNC )) !=0 ) +return -EINVAL ; + error =prctl_set_openat_beneath (me, arg3); +break ; +case PR_GET_OPENAT_BENEATH : +if (arg2 || arg3 || arg4 || arg5) +return -EINVAL ;
Quoted 8 lines
kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2029
Quoted 5 lines
kernel/sys.c +1
-0+return me->openat_beneath;case PR_GET_THP_DISABLE :if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;Why are you always forbidding a change of prctl from 1 to 0? It should be safe if
current->no_new_privsis clear.
I don't immediately see why you're forbidding unsettling it at all. If you need it to be sticky, then use seccomp or Capsicum to make it sticky.
Also, the way implementation is dangerously racy -- if anyone pokes at adjacent bitfields without the lock, they can get corrupted. Try basing on Kees' seccomp tree or security-next and using the new atomic flags field.
--Andy
Quoted 17 lines
Do new threads inherit from the parent?
Also, I wonder if you need something like this check:
/* * Installing a seccomp filter requires that the task has * CAP_SYS_ADMIN in its namespace or be running with no_new_privs. * This avoids scenarios where unprivileged tasks can affect the * behavior of privileged children. */ if (!current->no_new_privs && security_capable_noaudit(current_cred(), current_user_ns(), CAP_SYS_ADMIN) != 0) return -EACCES;Paolo
Re: [PATCH 10/11] capsicum: prctl(2) to force use of O_BENEATH
Quoted 27 lines · Andy Lutomirski
Quoted 16 lines · Paolo Bonzini
Il 25/07/2014 15:47, David Drysdale ha scritto:
Quoted 14 lines
kernel/sys.c +10
-0@@ -1996,6 +2013,17 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;return current->no_new_privs ?1 :0 ; +case PR_SET_OPENAT_BENEATH : +if (arg2 !=1 || arg4 || arg5) +return -EINVAL ; +if ((arg3 & ~(PR_SET_OPENAT_BENEATH_TSYNC )) !=0 ) +return -EINVAL ; + error =prctl_set_openat_beneath (me, arg3); +break ; +case PR_GET_OPENAT_BENEATH : +if (arg2 || arg3 || arg4 || arg5) +return -EINVAL ;Quoted 8 lines
kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2029
Quoted 5 lines
kernel/sys.c +1
-0+return me->openat_beneath;case PR_GET_THP_DISABLE :if (arg2 || arg3 || arg4 || arg5)return -EINVAL ;Why are you always forbidding a change of prctl from 1 to 0? It should be safe if
current->no_new_privsis clear.
Quoted 3 lines
I don't immediately see why you're forbidding unsettling it at all. If you need it to be sticky, then use seccomp or Capsicum to make it sticky.
Good point, that would make the function more generic -- needing to latch is specific to Capsicum's use of it.
Quoted 5 lines
Also, the way implementation is dangerously racy -- if anyone pokes at adjacent bitfields without the lock, they can get corrupted. Try basing on Kees' seccomp tree or security-next and using the new atomic flags field.
Ah yes, sorry -- I hadn't yet shifted the implementation to line up with the work you and Kees have put into the seccomp stuff.
Quoted 21 lines
--Andy
Quoted 17 lines
Do new threads inherit from the parent?
Also, I wonder if you need something like this check:
/* * Installing a seccomp filter requires that the task has * CAP_SYS_ADMIN in its namespace or be running with no_new_privs. * This avoids scenarios where unprivileged tasks can affect the * behavior of privileged children. */ if (!current->no_new_privs && security_capable_noaudit(current_cred(), current_user_ns(), CAP_SYS_ADMIN) != 0) return -EACCES;Paolo
Yes, new threads inherit the flag from the parent so the NNP||CAP_SYS_ADMIN check is probably needed.
PATCH v2 11/11 seccomp: Add tgid and tid into seccomp_data
In reply to David DrysdaleAdd the current thread and thread group IDs into the data available for seccomp-bpf programs to work on. This allows installation of filters that police syscalls based on thread or process ID, e.g. tgkill(2)/kill(2)/prctl(2).
--- include/uapi/linux/seccomp.h | 10 ++++++++++ kernel/seccomp.c | 2 ++ 2 files changed, 12 insertions(+)
include/uapi/linux/seccomp.h +10 -0
@@ -36,12 +36,22 @@* @instruction_pointer: at the time of the system call. * @args: up to 6 system call arguments always stored as 64-bit values * regardless of the architecture. +* @tgid: thread group ID of the thread executing the BPF program. +* @tid: thread ID of the thread executing the BPF program. +* The SECCOMP_DATA_TID_PRESENT macro indicates the presence of the +* tgid and tid fields; user programs may use this macro to conditionally +* compile code against older versions of the kernel. Note also that +* BPF programs should cope with the absence of these fields by testing +* the length of data available. */ struct seccomp_data {int nr;__u32 arch;__u64 instruction_pointer;__u64 args[6 ]; +__u32 tgid; +__u32 tid; }; +#define SECCOMP_DATA_TID_PRESENT 1#endif /* _UAPI_LINUX_SECCOMP_H */
kernel/seccomp.c +2 -0
@@ -80,6 +80,8 @@ static void populate_seccomp_data(struct seccomp_data *sd) sd->args[4 ] = args[4 ]; sd->args[5 ] = args[5 ]; sd->instruction_pointer =KSTK_EIP (task); + sd->tgid =task_tgid_vnr (current); + sd->tid =task_pid_vnr (current); }/**
Signature · 1 line
2.0.0.526.g5318336
Andy Lutomirski
reviewerRe: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 44 lines · David Drysdale
Add the current thread and thread group IDs into the data available for seccomp-bpf programs to work on. This allows installation of filters that police syscalls based on thread or process ID, e.g. tgkill(2)/kill(2)/prctl(2).
Signed-off-by David Drysdale <[email protected]>--- include/uapi/linux/seccomp.h | 10 ++++++++++ kernel/seccomp.c | 2 ++ 2 files changed, 12 insertions(+)include/uapi/linux/seccomp.h +10
-0@@ -36,12 +36,22 @@* @instruction_pointer: at the time of the system call. * @args: up to 6 system call arguments always stored as 64-bit values * regardless of the architecture. +* @tgid: thread group ID of the thread executing the BPF program. +* @tid: thread ID of the thread executing the BPF program. +* The SECCOMP_DATA_TID_PRESENT macro indicates the presence of the +* tgid and tid fields; user programs may use this macro to conditionally +* compile code against older versions of the kernel. Note also that +* BPF programs should cope with the absence of these fields by testing +* the length of data available. */ struct seccomp_data {int nr;__u32 arch;__u64 instruction_pointer;__u64 args[6 ]; +__u32 tgid; +__u32 tid; }; +#define SECCOMP_DATA_TID_PRESENT 1#endif /* _UAPI_LINUX_SECCOMP_H */ kernel/seccomp.c +0
-0
kernel/seccomp.c static void populate_seccomp_data(struct seccomp_data *sd) · line 85
Quoted 6 lines
kernel/seccomp.c +2
-0@@ -80,6 +80,8 @@ static void populate_seccomp_data(struct seccomp_data *sd) sd->args[4 ] = args[4 ]; sd->args[5 ] = args[5 ]; sd->instruction_pointer =KSTK_EIP (task); + sd->tgid =task_tgid_vnr (current); + sd->tid =task_pid_vnr (current); }
This is, IMO, problematic. These should probably be relative to the filter creator, not the filtered task. This will also hurt performance.
What's the use case? Can it be better achieved with a new eBPF function?
--Andy
Quoted 5 lines
kernel/seccomp.c +0
-1/** -- 2.0.0.526.g5318336
Re: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 39 lines · Andy Lutomirski
Quoted 38 lines · David Drysdale
Add the current thread and thread group IDs into the data available for seccomp-bpf programs to work on. This allows installation of filters that police syscalls based on thread or process ID, e.g. tgkill(2)/kill(2)/prctl(2).
Signed-off-by David Drysdale <[email protected]>--- include/uapi/linux/seccomp.h | 10 ++++++++++ kernel/seccomp.c | 2 ++ 2 files changed, 12 insertions(+)include/uapi/linux/seccomp.h +10
-0@@ -36,12 +36,22 @@* @instruction_pointer: at the time of the system call. * @args: up to 6 system call arguments always stored as 64-bit values * regardless of the architecture. +* @tgid: thread group ID of the thread executing the BPF program. +* @tid: thread ID of the thread executing the BPF program. +* The SECCOMP_DATA_TID_PRESENT macro indicates the presence of the +* tgid and tid fields; user programs may use this macro to conditionally +* compile code against older versions of the kernel. Note also that +* BPF programs should cope with the absence of these fields by testing +* the length of data available. */ struct seccomp_data {int nr;__u32 arch;__u64 instruction_pointer;__u64 args[6 ]; +__u32 tgid; +__u32 tid; }; +#define SECCOMP_DATA_TID_PRESENT 1
Quoted 16 lines
kernel/seccomp.c static void populate_seccomp_data(struct seccomp_data *sd) · line 85
Quoted 12 lines
include/uapi/linux/seccomp.h +0
-0#endif /* _UAPI_LINUX_SECCOMP_H */ kernel/seccomp.c +2
-0@@ -80,6 +80,8 @@ static void populate_seccomp_data(struct seccomp_data *sd) sd->args[4 ] = args[4 ]; sd->args[5 ] = args[5 ]; sd->instruction_pointer =KSTK_EIP (task); + sd->tgid =task_tgid_vnr (current); + sd->tid =task_pid_vnr (current); }This is, IMO, problematic. These should probably be relative to the filter creator, not the filtered task. This will also hurt performance.
Yeah, we can't change the seccomp_data structure without a lot of care, and tgid/tid really should be encoded in the filter. However, it is tricky in the forking case.
Quoted 2 lines
What's the use case? Can it be better achieved with a new eBPF function?
Julien had been wanting something like this too (though he'd suggested it via prctl): limit the signal functions to "self" only. I wonder if adding a prctl like done for O_BENEATH could work for signal sending?
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Andy Lutomirski
reviewerRe: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
[cc: Eric Biederman]
Quoted 62 lines · Kees Cook
Quoted 39 lines · Andy Lutomirski
Quoted 38 lines · David Drysdale
Add the current thread and thread group IDs into the data available for seccomp-bpf programs to work on. This allows installation of filters that police syscalls based on thread or process ID, e.g. tgkill(2)/kill(2)/prctl(2).
Signed-off-by David Drysdale <[email protected]>--- include/uapi/linux/seccomp.h | 10 ++++++++++ kernel/seccomp.c | 2 ++ 2 files changed, 12 insertions(+)include/uapi/linux/seccomp.h +10
-0@@ -36,12 +36,22 @@* @instruction_pointer: at the time of the system call. * @args: up to 6 system call arguments always stored as 64-bit values * regardless of the architecture. +* @tgid: thread group ID of the thread executing the BPF program. +* @tid: thread ID of the thread executing the BPF program. +* The SECCOMP_DATA_TID_PRESENT macro indicates the presence of the +* tgid and tid fields; user programs may use this macro to conditionally +* compile code against older versions of the kernel. Note also that +* BPF programs should cope with the absence of these fields by testing +* the length of data available. */ struct seccomp_data {int nr;__u32 arch;__u64 instruction_pointer;__u64 args[6 ]; +__u32 tgid; +__u32 tid; }; +#define SECCOMP_DATA_TID_PRESENT 1Quoted 16 lines
kernel/seccomp.c static void populate_seccomp_data(struct seccomp_data *sd) · line 85
Quoted 12 lines
include/uapi/linux/seccomp.h +0
-0#endif /* _UAPI_LINUX_SECCOMP_H */ kernel/seccomp.c +2
-0@@ -80,6 +80,8 @@ static void populate_seccomp_data(struct seccomp_data *sd) sd->args[4 ] = args[4 ]; sd->args[5 ] = args[5 ]; sd->instruction_pointer =KSTK_EIP (task); + sd->tgid =task_tgid_vnr (current); + sd->tid =task_pid_vnr (current); }This is, IMO, problematic. These should probably be relative to the filter creator, not the filtered task. This will also hurt performance.
Yeah, we can't change the
seccomp_datastructure without a lot of care, and tgid/tid really should be encoded in the filter. However, it is tricky in the forking case.Quoted 1 line
Quoted 6 lines
Quoted 1 line
What's the use case? Can it be better achieved with a new eBPF function?
Julien had been wanting something like this too (though he'd suggested it via prctl): limit the signal functions to "self" only. I wonder if adding a prctl like done for
O_BENEATHcould work for signal sending?
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that find_task_by_vpid works).
It's far from just being signals. There's access_process_vm, ptrace, all the signal functions, clock_gettime (see CPUCLOCK_PID -- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.
--Andy
Quoted 5 lines
-Kees
--
Kees Cook
Chrome OS Security
Signature · 2 lines
Andy Lutomirski AMA Capital Management, LLC
Re: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 73 lines · Andy Lutomirski
[cc: Eric Biederman]
Quoted 62 lines · Kees Cook
Quoted 39 lines · Andy Lutomirski
Quoted 38 lines · David Drysdale
Add the current thread and thread group IDs into the data available for seccomp-bpf programs to work on. This allows installation of filters that police syscalls based on thread or process ID, e.g. tgkill(2)/kill(2)/prctl(2).
Signed-off-by David Drysdale <[email protected]>--- include/uapi/linux/seccomp.h | 10 ++++++++++ kernel/seccomp.c | 2 ++ 2 files changed, 12 insertions(+)include/uapi/linux/seccomp.h +10
-0@@ -36,12 +36,22 @@* @instruction_pointer: at the time of the system call. * @args: up to 6 system call arguments always stored as 64-bit values * regardless of the architecture. +* @tgid: thread group ID of the thread executing the BPF program. +* @tid: thread ID of the thread executing the BPF program. +* The SECCOMP_DATA_TID_PRESENT macro indicates the presence of the +* tgid and tid fields; user programs may use this macro to conditionally +* compile code against older versions of the kernel. Note also that +* BPF programs should cope with the absence of these fields by testing +* the length of data available. */ struct seccomp_data {int nr;__u32 arch;__u64 instruction_pointer;__u64 args[6 ]; +__u32 tgid; +__u32 tid; }; +#define SECCOMP_DATA_TID_PRESENT 1Quoted 16 lines
kernel/seccomp.c static void populate_seccomp_data(struct seccomp_data *sd) · line 85
Quoted 12 lines
include/uapi/linux/seccomp.h +0
-0#endif /* _UAPI_LINUX_SECCOMP_H */ kernel/seccomp.c +2
-0@@ -80,6 +80,8 @@ static void populate_seccomp_data(struct seccomp_data *sd) sd->args[4 ] = args[4 ]; sd->args[5 ] = args[5 ]; sd->instruction_pointer =KSTK_EIP (task); + sd->tgid =task_tgid_vnr (current); + sd->tid =task_pid_vnr (current); }This is, IMO, problematic. These should probably be relative to the filter creator, not the filtered task. This will also hurt performance.
Yeah, we can't change the
seccomp_datastructure without a lot of care, and tgid/tid really should be encoded in the filter. However, it is tricky in the forking case.Quoted 1 line
Quoted 6 lines
Quoted 1 line
What's the use case? Can it be better achieved with a new eBPF function?
Julien had been wanting something like this too (though he'd suggested it via prctl): limit the signal functions to "self" only. I wonder if adding a prctl like done for
O_BENEATHcould work for signal sending?
Quoted 3 lines
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).
Ooh, that would be extremely interesting, yes. Kind of an extreme form of pid namespace without actually being a namespace.
Quoted 4 lines
It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.
Yeah, that would be very interesting.
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Re: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 18 lines · Kees Cook
Quoted 10 lines · Andy Lutomirski
[cc: Eric Biederman]
Quoted 4 lines · Kees Cook
Julien had been wanting something like this too (though he'd suggested it via prctl): limit the signal functions to "self" only. I wonder if adding a prctl like done for
O_BENEATHcould work for signal sending?Quoted 3 lines
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).Ooh, that would be extremely interesting, yes. Kind of an extreme form of pid namespace without actually being a namespace.
Quoted 6 lines
Quoted 4 lines
It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.Yeah, that would be very interesting.
Yes, this would be incredibly useful.
1. For Chromium [1], I dislike relying on seccomp purely for "access-control" (to other processes or files). Because it's really hard to think about everything (things like CPUCLOCK_PID bite, seehttps://crbug.com/374479).
Se we have a first layer of sandboxing (using PID + NET namespaces and chroot) for "access-control" and a second layer for kernel attack surface reduction and a few other things using seccomp-bpf.
The first layer isn't currently very good; it's heavyweight and complex (you need an init(1) per namespace and that init cannot be multi-purposed as a useful process because pid = 1 can never receive signals). One PID namespace per process isn't something that scales well. (Also before USER_NS it required a setuid root program).
2. Even with a safe pure seccomp-bpf sandbox that prevents sending signals to other process / ptrace() et al and that restrict clock_gettime(2) properly, things become quickly very tedious because as far as the kernel is concerned, the process under this BPF program can still pass ptrace_may_access() to other processes. This means for instance that no matter what you do, a model where open() is allowed can't work if /proc is available. We need a mode that says "ptrace_may_access()" will never pass.
So yes, I really would like:
- a prctl that says: "I'm dropping privileges and I now can't interact with other thread groups (via signals, ptrace, etc..)".
- Something to drop access to the file system. It could be an unprivileged way to chroot() to an empty directory (unprivileged namespaces work for that, - except if you're already in a chroot -). This is a little tricky without allowing chroot escapes, so I suspect we would want to express it in terms of mount namespace, or something else, rather than chroot.
Then we have the primitives we need to build sandboxes in a simple way and we can add seccomp-bpf on top to do things such as open() hooking (via SECCOMP_RET_TRAP) and to restrict the kernel attack surface.
Julien
A message that is not in the archive (sent off-list or not yet received): CAKyRK=j-f92xHTL3+TNr9WOv_y47dkZR=WZkpY_a5YW3Q8HfaQ@mail.gmail.com
Andy Lutomirski
reviewerRe: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 30 lines · Julien Tinnes
Quoted 21 lines · Kees Cook
Quoted 11 lines · Andy Lutomirski
[cc: Eric Biederman]
Quoted 4 lines · Kees Cook
Julien had been wanting something like this too (though he'd suggested it via prctl): limit the signal functions to "self" only. I wonder if adding a prctl like done for
O_BENEATHcould work for signal sending?Quoted 3 lines
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).Ooh, that would be extremely interesting, yes. Kind of an extreme form of pid namespace without actually being a namespace.
Quoted 6 lines
Quoted 4 lines
It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.Yeah, that would be very interesting.
Quoted 6 lines
Yes, this would be incredibly useful.
1. For Chromium [1], I dislike relying on seccomp purely for "access-control" (to other processes or files). Because it's really hard to think about everything (things like
CPUCLOCK_PIDbite, see
https://crbug.com/374479).
Not public :(
Quoted 18 lines
Se we have a first layer of sandboxing (using PID + NET namespaces and chroot) for "access-control" and a second layer for kernel attack surface reduction and a few other things using seccomp-bpf.
The first layer isn't currently very good; it's heavyweight and complex (you need an init(1) per namespace and that init cannot be multi-purposed as a useful process because pid = 1 can never receive signals). One PID namespace per process isn't something that scales well. (Also before
USER_NSit required a setuid root program).2. Even with a safe pure seccomp-bpf sandbox that prevents sending signals to other process /
ptrace()et al and that restrictclock_gettime(2)properly, things become quickly very tedious because as far as the kernel is concerned, the process under this BPF program can still passptrace_may_access()to other processes. This means for instance that no matter what you do, a model whereopen()is allowed can't work if /proc is available. We need a mode that says "ptrace_may_access()" will never pass.
Quoted 8 lines
So yes, I really would like:
- a prctl that says: "I'm dropping privileges and I now can't interact with other thread groups (via signals, ptrace, etc..)".
- Something to drop access to the file system. It could be an unprivileged way tochroot()to an empty directory (unprivileged namespaces work for that, - except if you're already in a chroot -). This is a little tricky without allowing chroot escapes, so I suspect we would want to express it in terms of mount namespace, or something else, rather than chroot.
Capsicum will give you this.
See the other thread for a more concrete proposal. prctl is getting out of hand.
--Andy
Re: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 67 lines · Andy Lutomirski
Quoted 30 lines · Julien Tinnes
Quoted 21 lines · Kees Cook
Quoted 11 lines · Andy Lutomirski
[cc: Eric Biederman]
Quoted 4 lines · Kees Cook
Julien had been wanting something like this too (though he'd suggested it via prctl): limit the signal functions to "self" only. I wonder if adding a prctl like done for
O_BENEATHcould work for signal sending?Quoted 3 lines
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).Ooh, that would be extremely interesting, yes. Kind of an extreme form of pid namespace without actually being a namespace.
Quoted 6 lines
Quoted 4 lines
It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.Yeah, that would be very interesting.
Quoted 6 lines
Yes, this would be incredibly useful.
1. For Chromium [1], I dislike relying on seccomp purely for "access-control" (to other processes or files). Because it's really hard to think about everything (things like
CPUCLOCK_PIDbite, see
https://crbug.com/374479).Not public :(
Quoted 18 lines
Se we have a first layer of sandboxing (using PID + NET namespaces and chroot) for "access-control" and a second layer for kernel attack surface reduction and a few other things using seccomp-bpf.
The first layer isn't currently very good; it's heavyweight and complex (you need an init(1) per namespace and that init cannot be multi-purposed as a useful process because pid = 1 can never receive signals). One PID namespace per process isn't something that scales well. (Also before
USER_NSit required a setuid root program).2. Even with a safe pure seccomp-bpf sandbox that prevents sending signals to other process /
ptrace()et al and that restrictclock_gettime(2)properly, things become quickly very tedious because as far as the kernel is concerned, the process under this BPF program can still passptrace_may_access()to other processes. This means for instance that no matter what you do, a model whereopen()is allowed can't work if /proc is available. We need a mode that says "ptrace_may_access()" will never pass.Quoted 8 lines
So yes, I really would like:
- a prctl that says: "I'm dropping privileges and I now can't interact with other thread groups (via signals, ptrace, etc..)".
- Something to drop access to the file system. It could be an unprivileged way tochroot()to an empty directory (unprivileged namespaces work for that, - except if you're already in a chroot -). This is a little tricky without allowing chroot escapes, so I suspect we would want to express it in terms of mount namespace, or something else, rather than chroot.
Quoted 1 line
Capsicum will give you this.
Yep, that's the idea. As long as there aren't any open DFDs for "/proc" on entry to capability mode, there shouldn't be a way to access it later -- but it is still possible to openat(2) new files (relative to a pre-opened DFD).
Quoted 4 lines
See the other thread for a more concrete proposal. prctl is getting out of hand.
--Andy
Re: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
In reply to Andy LutomirskiQuoted 66 lines · Andy Lutomirski
[cc: Eric Biederman]
Quoted 63 lines · Kees Cook
Quoted 39 lines · Andy Lutomirski
Quoted 38 lines · David Drysdale
Add the current thread and thread group IDs into the data available for seccomp-bpf programs to work on. This allows installation of filters that police syscalls based on thread or process ID, e.g. tgkill(2)/kill(2)/prctl(2).
Signed-off-by David Drysdale <[email protected]>--- include/uapi/linux/seccomp.h | 10 ++++++++++ kernel/seccomp.c | 2 ++ 2 files changed, 12 insertions(+)include/uapi/linux/seccomp.h +10
-0@@ -36,12 +36,22 @@* @instruction_pointer: at the time of the system call. * @args: up to 6 system call arguments always stored as 64-bit values * regardless of the architecture. +* @tgid: thread group ID of the thread executing the BPF program. +* @tid: thread ID of the thread executing the BPF program. +* The SECCOMP_DATA_TID_PRESENT macro indicates the presence of the +* tgid and tid fields; user programs may use this macro to conditionally +* compile code against older versions of the kernel. Note also that +* BPF programs should cope with the absence of these fields by testing +* the length of data available. */ struct seccomp_data {int nr;__u32 arch;__u64 instruction_pointer;__u64 args[6 ]; +__u32 tgid; +__u32 tid; }; +#define SECCOMP_DATA_TID_PRESENT 1Quoted 16 lines
kernel/seccomp.c static void populate_seccomp_data(struct seccomp_data *sd) · line 85
Quoted 12 lines
include/uapi/linux/seccomp.h +0
-0#endif /* _UAPI_LINUX_SECCOMP_H */ kernel/seccomp.c +2
-0@@ -80,6 +80,8 @@ static void populate_seccomp_data(struct seccomp_data *sd) sd->args[4 ] = args[4 ]; sd->args[5 ] = args[5 ]; sd->instruction_pointer =KSTK_EIP (task); + sd->tgid =task_tgid_vnr (current); + sd->tid =task_pid_vnr (current); }This is, IMO, problematic. These should probably be relative to the filter creator, not the filtered task. This will also hurt performance.
Yeah, we can't change the
seccomp_datastructure without a lot of care, and tgid/tid really should be encoded in the filter. However, it is tricky in the forking case.Quoted 2 lines
What's the use case? Can it be better achieved with a new eBPF function?
The specific use case is to be able to write a filter that allows kill(2) or tgkill(2) to self, where the filter still works after forking. Capsicum capability mode in general locks down system calls that access PIDs (as they're a global namespace), but allows kill(self) as a pragmatic compromise to make it easier to migrate applications to use Capsicum.
Quoted 9 lines
Quoted 4 lines
Julien had been wanting something like this too (though he'd suggested it via prctl): limit the signal functions to "self" only. I wonder if adding a prctl like done for
O_BENEATHcould work for signal sending?Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).
That sounds like a good idea, as long as it's possible for non-CAP_SYS_ADMIN processes to do....
Quoted 4 lines
It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.
For the Capsicum case in particular, most of these are restricted by the capability mode filter anyhow (although I need to fix it for CPUCLOCK_PID -- thanks for pointing that out); the kill(2) case was a special case to make migrations easier. But a more general mechanism seems sensible.
Quoted 13 lines
--Andy
Quoted 5 lines
-Kees
--
Kees Cook
Chrome OS Security--
Andy Lutomirski
AMA Capital Management, LLC
Re: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
In reply to Andy LutomirskiQuoted 11 lines · Andy Lutomirski
[cc: Eric Biederman]
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.
So here is the practical question.
Are these processes that only can send signals to their thread group allowed to call fork()?
If fork is allowed and all pid lookups are restricted to their own thread group that wait, waitpid, and all of the rest of the wait family will never return the pids of their children, and zombies will accumulate. Aka the semantics are fundamentally broken.
If fork is not allowed pid namespaces already solve this problem.
Eric
Andy Lutomirski
reviewerRe: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 16 lines · Eric W. Biederman
Quoted 11 lines · Andy Lutomirski
[cc: Eric Biederman]
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.So here is the practical question.
Quoted 8 lines
Are these processes that only can send signals to their thread group allowed to call
fork()?If fork is allowed and all pid lookups are restricted to their own thread group that wait, waitpid, and all of the rest of the wait family will never return the pids of their children, and zombies will accumulate. Aka the semantics are fundamentally broken.
Good point.
I can imagine at least three ways that fork() could continue working, though:
1. Allow lookups of immediate children, too. (I don't love this one.)
2. Allow non-self pids to be translated in but not out. This way P_ALL will continue working.
3. Have the kernel treat any PID-restricted process as though it were NOCLDWAIT.
I think I like #3. Thoughts?
Quoted 2 lines
If fork is not allowed pid namespaces already solve this problem.
PID namespaces are fairly heavyweight. Julien pointed out that using PID namespaces requires a bunch of dummy PID 1 processes.
--Andy
Re: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 36 lines · Andy Lutomirski
Quoted 16 lines · Eric W. Biederman
Quoted 11 lines · Andy Lutomirski
[cc: Eric Biederman]
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.So here is the practical question.
Quoted 8 lines
Are these processes that only can send signals to their thread group allowed to call
fork()?If fork is allowed and all pid lookups are restricted to their own thread group that wait, waitpid, and all of the rest of the wait family will never return the pids of their children, and zombies will accumulate. Aka the semantics are fundamentally broken.
Good point.
I can imagine at least three ways that
fork()could continue working, though:1. Allow lookups of immediate children, too. (I don't love this one.)
2. Allow non-self pids to be translated in but not out. This wayP_ALLwill continue working.
3. Have the kernel treat any PID-restricted process as though it were NOCLDWAIT.
Quoted 7 lines
I think I like #3. Thoughts?
Quoted 2 lines
If fork is not allowed pid namespaces already solve this problem.
PID namespaces are fairly heavyweight. Julien pointed out that using PID namespaces requires a bunch of dummy PID 1 processes.
Only if you can't tolerate init exiting. The reasoning with respect to signals and signals being ignored was wrong. And if you only have one process you care about and no children to worry about neither the difference in signal handling nor the world dies whe init exits applies.
Therefore given what I have read described pid namespaces are a trivial solution to this problem space.
Eric
Andy Lutomirski
reviewerRe: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 43 lines · Eric W. Biederman
Quoted 41 lines · Andy Lutomirski
Quoted 16 lines · Eric W. Biederman
Quoted 11 lines · Andy Lutomirski
[cc: Eric Biederman]
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.So here is the practical question.
Quoted 8 lines
Are these processes that only can send signals to their thread group allowed to call
fork()?If fork is allowed and all pid lookups are restricted to their own thread group that wait, waitpid, and all of the rest of the wait family will never return the pids of their children, and zombies will accumulate. Aka the semantics are fundamentally broken.
Good point.
I can imagine at least three ways that
fork()could continue working, though:1. Allow lookups of immediate children, too. (I don't love this one.)
2. Allow non-self pids to be translated in but not out. This wayP_ALLwill continue working.
3. Have the kernel treat any PID-restricted process as though it were NOCLDWAIT.I think I like #3. Thoughts?
Quoted 2 lines
If fork is not allowed pid namespaces already solve this problem.
Quoted 7 lines
Quoted 2 lines
PID namespaces are fairly heavyweight. Julien pointed out that using PID namespaces requires a bunch of dummy PID 1 processes.
Only if you can't tolerate init exiting. The reasoning with respect to signals and signals being ignored was wrong. And if you only have one process you care about and no children to worry about neither the difference in signal handling nor the world dies whe init exits applies.
Can you elaborate? It seems entirely plausible to me that there are programs that won't work right as PID 1 without considerable adaptation.
Quoted 3 lines
Therefore given what I have read described pid namespaces are a trivial solution to this problem space.
pid namespaces also won't work in the context of Capsicum unless you want every single Capsicum process to be its own pid namespace. Also, pid namespaces don't offer any way to protect children from parents.
--Andy
A message that is not in the archive (sent off-list or not yet received): [email protected]
Andy Lutomirski
reviewerRe: [PATCH 11/11] seccomp: Add tgid and tid into seccomp_data
Quoted 78 lines · Eric W. Biederman
Quoted 48 lines · Andy Lutomirski
Quoted 46 lines · Eric W. Biederman
Quoted 43 lines · Andy Lutomirski
Quoted 16 lines · Eric W. Biederman
Quoted 11 lines · Andy Lutomirski
[cc: Eric Biederman]
Can we do one better and add a flag to prevent any non-self pid lookups? This might actually be easy on top of the pid namespace work (e.g. we could change the way that
find_task_by_vpidworks).It's far from just being signals. There's
access_process_vm, ptrace, all the signal functions,clock_gettime(seeCPUCLOCK_PID-- yes, this is ridiculous), and probably some others that I've forgotten about or never noticed in the first place.So here is the practical question.
Quoted 8 lines
Are these processes that only can send signals to their thread group allowed to call
fork()?If fork is allowed and all pid lookups are restricted to their own thread group that wait, waitpid, and all of the rest of the wait family will never return the pids of their children, and zombies will accumulate. Aka the semantics are fundamentally broken.
Good point.
I can imagine at least three ways that
fork()could continue working, though:1. Allow lookups of immediate children, too. (I don't love this one.)
2. Allow non-self pids to be translated in but not out. This wayP_ALLwill continue working.
3. Have the kernel treat any PID-restricted process as though it were NOCLDWAIT.I think I like #3. Thoughts?
Quoted 2 lines
If fork is not allowed pid namespaces already solve this problem.
PID namespaces are fairly heavyweight. Julien pointed out that using PID namespaces requires a bunch of dummy PID 1 processes.
Quoted 8 lines
Quoted 4 lines
Only if you can't tolerate init exiting. The reasoning with respect to signals and signals being ignored was wrong. And if you only have one process you care about and no children to worry about neither the difference in signal handling nor the world dies whe init exits applies.
Can you elaborate? It seems entirely plausible to me that there are programs that won't work right as PID 1 without considerable adaptation.
The only funny things about pid 1 of a pid namespace are:
- children can't send signals to pid 1 unless a signal handler has been established.
- All children die when the parent dies.
- Grand children become zombies of the parent when the children die.
- The pid is 1.That is almost everything is the same and it takes almost no adaptation (really) to run as the initial pid in a pid namespace.
Not being able to receive signals (which is the argument I read against them) is bogus. You just have to set your signal handler to something besides
SIG_DFL.So I have my question: What is the use case people are trying to solve by filtering signals and pid lookups. If children are not part of the goal a pid namespace will work just fine.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Therefore given what I have read described pid namespaces are a trivial solution to this problem space.
pid namespaces also won't work in the context of Capsicum unless you want every single Capsicum process to be its own pid namespace.
For a tightly bound process I don't see why each process could not be it's own pid namespace.
Two main reasons: You can't put yourself in a pid namespace, so you need to fork into your sandbox, and you can't prevent yourself from seeing your children (although, as noted, my approach has issues here, too, but I think this is more easily solved outside the context of namespaces).
Quoted 7 lines
Quoted 3 lines
Also,
pid namespaces don't offer any way to protect children from parents.And my presumption was that there were not any children because the semantics suggested so far do not properly support children.
I'd like to try to fix that.
Another approach: let waiting for zombies that are immediate children be an exception.
--Andy
Quoted 1 line
Eric
PATCH 1/6 open.2: describe O_BENEATH flag
In reply to David Drysdale--- man2/open.2 | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-)
man2/open.2 +31 -2
@@ -705,7 +705,7 @@ in a fully formed state (using as described above). .RE .IP -.B O_TMPFILE +.B O_TMPFILE " (since Linux 3.??)" requires support by the underlying filesystem; only a subset of Linux filesystems provide that support. In the initial implementation, support was provided in
@@ -715,6 +715,31 @@ XFS support was added .\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe in Linux 3.15. .TP +.B O_BENEATH +Ensure that the +.I pathname +is beneath the current working directory (for +.BR open (2)) +or the +.I dirfd +(for +.BR openat (2)). +If the +.I pathname +is absolute or contains a path component of "..", the +.BR open () +fails with the error +.BR EACCES. +This occurs even if ".." path component would not actually +escape the original directory; for example, a +.I pathname +of "subdir/../filename" would be rejected. +Path components that are symbolic links to absolute paths, or that are +relative paths containing a ".." component, will also cause the +.BR open () +operation to fail with the error +.BR EACCES. +.TP .B O_TRUNC If the file already exists and is a regular file and the access mode allows writing (i.e., is
@@ -791,7 +816,11 @@ The requested access to the file is not allowed, or search permission is denied for one of the directories in the path prefix of .IR pathname , or the file did not exist yet and write access to the parent directory -is not allowed. +is not allowed, or the +.B O_BENEATH +flag was specified and the +.I pathname +was not beneath the relevant directory. (See also .BR path_resolution (7).) .TP
--
2.0.0.526.g5318336
PATCH 2/6 capsicum.7: describe Capsicum capability framework
In reply to David Drysdale--- man7/capsicum.7 | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 man7/capsicum.7
man7/capsicum.7 new file +97 -0
@@ -0,0 +1,97 @@ +.\" +.\" Copyright (c) 2014 Google, Inc. +.\" Copyright (c) 2011, 2013 Robert N. M. Watson +.\" Copyright (c) 2011 Jonathan Anderson +.\" All rights reserved. +.\" +.\" %%%LICENSE_START(BSD_2_CLAUSE) +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" %%%LICENSE_END +.\" +.TH CAPSICUM 7 2014-05-07 "Linux" "Linux Programmer's Manual" +.SH NAME +capsicum \- lightweight OS capability and sandbox framework +.SH SYNOPSIS +.B #include <sys/capsicum.h> +.SH DESCRIPTION +Capsicum is a lightweight OS capability and sandbox framework implementing a hybrid +capability system model. +Capsicum can be used for application and library compartmentalisation, the +decomposition of larger bodies of software into isolated (sandboxed) +components in order to implement security policies and limit the impact of +software vulnerabilities. +.PP +Capsicum provides three core kernel mechanisms, +.IR "Capsicum capabilities", +.I "capability mode" +and +.IR "process descriptors", +each described below. + +.SS Capsicum Capabilities +A +.I Capsicum capability +is a file descriptor that has been limited so that only +certain operations can be performed on it. +For example, a file descriptor returned by +.BR open (2) +may be refined using +.BR cap_rights_limit (2) +so that only +.BR read (2) +and +.BR write (2) +can be called on it, but not +.BR fchmod (2). +The complete list of the capability rights can be found in the +.BR rights (7) +manual page. + +.SS Capability Mode +Capsicum capability mode is a process mode, entered by invoking +.BR cap_enter (3), +in which access to global OS namespaces (such as the file system and PID +namespaces) is restricted; only explicitly delegated rights, referenced by +memory mappings or file descriptors, may be used. +Once set, the flag is inherited by future children processes, and may not be +cleared. + +.SS Process Descriptors +.I Process descriptors +are file descriptors representing processes, allowing parent processes to manage +child processes without requiring access to the PID namespace, and are described in +greater detail in +.BR procdesc (7). +.SH VERSIONS +Capsicum support is available in the kernel since version 3.???. +.SH SEE ALSO +.BR cap_enter (3), +.BR cap_getmode (3) , +.BR cap_rights_get (2), +.BR cap_rights_limit (2) , +.BR pdfork (2), +.BR pdgetpid (2), +.BR pdkill (2), +.BR pdwait4 (2), +.BR procdesc (7), +.BR rights (7) +
--
2.0.0.526.g5318336
PATCH 3/6 rights.7: Describe Capsicum primary rights
In reply to David Drysdale--- man7/rights.7 | 525 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 525 insertions(+) create mode 100644 man7/rights.7
man7/rights.7 new file +525 -0
@@ -0,0 +1,525 @@ +.\" +.\" Copyright (c) 2014 Google, Inc. +.\" Copyright (c) 2012-2013 The FreeBSD Foundation +.\" Copyright (c) 2008-2010 Robert N. M. Watson +.\" All rights reserved. +.\" +.\" This software was developed at the University of Cambridge Computer +.\" Laboratory with support from a grant from Google, Inc. +.\" +.\" %%%LICENSE_START(BSD_2_CLAUSE) +.\" Portions of this documentation were written by Pawel Jakub Dawidek +.\" under sponsorship from the FreeBSD Foundation. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" %%%LICENSE_END +.\" +.TH RIGHTS 7 2014-05-07 "Linux" "Linux Programmer's Manual" +.SH NAME +Capsicum capability rights for file descriptors +.SH SYNOPSIS +.B #include <linux/capsicum.h> +.SH DESCRIPTION +When a file descriptor is created by a function such as +.BR accept (2), +.BR accept4 (2), +.BR creat (2), +.BR epoll_create (2), +.BR eventfd (2), +.BR mq_open (2), +.BR open (2), +.BR openat (2), +.BR pdfork (2), +.BR pipe (2), +.BR pipe2 (2), +.BR signalfd (2), +.BR socket (2), +.BR socketpair (2) +or +.BR timerfd_create (2), +it implicitly has all Capsicum capability rights. +Those rights can be reduced (but never expanded) by using the +.BR cap_rights_limit (2) +system call. +Once capability rights are reduced, operations on the file descriptor will be +limited to those permitted by the associated rights. +.Pp +The list of primary capability rights is provided below. In addition, +.BR ioctl (2) +and +.BR fcntl (2) +can also be restricted to only allow specific commands. +.PP +The +.I "struct cap_rights" +type is used to store a list of primary capability rights; the +.BR cap_rights_init (3) +family of functions should be used to manage the structure. +.SH RIGHTS +The following rights may be specified in a rights mask: +.TP +.B CAP_ACCEPT +Permit +.BR accept (2) +and +.BR accept4 (2). +.TP +.B CAP_BIND +Permit +.BR bind (2). +Note that sockets can also become bound implicitly as a result of +.BR connect (2) +or +.BR send (2), +and that socket options set with +.BR setsockopt (2) +may also affect binding behavior. +.TP +.B CAP_CONNECT +Permit +.BR connect (2); +also required for +.BR sendto (2) +with a non-NULL destination address. +.TP +.B CAP_CREATE +Permit +.BR openat (2) +with the +.B O_CREAT +flag. +.TP +.B CAP_EVENT +Permit +.BR select (2), +.BR poll (2), +and +.BR epoll (7) +to be used in monitoring the file descriptor for events. +.TP +.B CAP_EXTATTR_DELETE +Permit +.BR fremovexattr (2). +.TP +.B CAP_EXTATTR_GET +Permit +.BR fgetxattr (2). +.TP +.B CAP_EXTATTR_LIST +Permit +.BR flistxattr (2). +.TP +.B CAP_EXTATTR_SET +Permit +.BR fsetxattr (2). +.TP +.B CAP_FCHDIR +Permit +.BR fchdir (2). +.TP +.B CAP_FCHMOD +Permit +.BR fchmod (2) +and +.BR fchmodat (2) +if the +.B CAP_LOOKUP +right is also present. +.TP +.B CAP_FCHMODAT +An alias to +.B CAP_FCHMOD +and +.BR CAP_LOOKUP . +.TP +.B CAP_FCHOWN +Permit +.BR fchown (2) +and +.BR fchownat (2) +if the +.B CAP_LOOKUP +right is also present. +.TP +.B CAP_FCHOWNAT +An alias to +.B CAP_FCHOWN +and +.BR CAP_LOOKUP . +.TP +.B CAP_FCNTL +Permit +.BR fcntl (2). +Note that only the +.BR F_GETFL , +.BR F_SETFL , +.B F_GETOWN , +.B F_SETOWN , +.B F_GETOWN_EX +and +.B F_SETOWN_EX +commands require this capability right. +Also note that the list of permitted commands can be further limited with the +.BR cap_rights_limit (2) +system call. +.TP +.B CAP_FEXECVE +Permit +.BR execveat (2) +and +.BR openat (2) +with the +.B O_EXEC +flag; +.B CAP_READ +is also required. +.TP +.B CAP_FLOCK +Permit +.BR flock (2) +and +.BR fcntl (2) +(with +.BR F_GETLK , +.BR F_SETLK +or +.B F_SETLKW +flag). +.TP +.B CAP_FSTAT +Permit +.BR fstat (2). +.TP +.B CAP_FSTATFS +Permit +.BR fstatfs (2). +.TP +.B CAP_FSYNC +Permit +.BR fsync (2) +and +.BR openat (2) +with the +.B O_SYNC +flag. +.TP +.B CAP_FTRUNCATE +Permit +.BR ftruncate (2) +and +.BR openat (2) +with the +.B O_TRUNC +flag. +.TP +.B CAP_FUTIMES +Permit +.BR futimesat (2) +if the +.B CAP_LOOKUP +right is also present. +.TP +.B CAP_FUTIMESAT +An alias to +.B CAP_FUTIMES +and +.BR CAP_LOOKUP . +.TP +.B CAP_GETPEERNAME +Permit +.BR getpeername (2). +.TP +.B CAP_GETSOCKNAME +Permit +.BR getsockname (2). +.TP +.B CAP_GETSOCKOPT +Permit +.BR getsockopt (2). +.TP +.B CAP_IOCTL +Permit +.BR ioctl (2). +Be aware that this system call has enormous scope, including potentially +global scope for some objects. +The list of permitted ioctl commands can be further limited with the +.BR cap_rights_limit (2) +system call. +.TP +.B CAP_LINKAT +Permit +.BR linkat (2) +and +.BR renameat (2) +on the destination directory descriptor. +This right includes the +.B CAP_LOOKUP +right. +.TP +.B CAP_LISTEN +Permit +.BR listen (2); +not much use (generally) without +.BR CAP_BIND . +.TP +.B CAP_LOOKUP +Permit the file descriptor to be used as a starting directory for calls such as +.BR linkat (2), +.BR openat (2), +and +.BR unlinkat (2). +.TP +.B CAP_MKDIRAT +Permit +.BR mkdirat (2). +This right includes the +.B CAP_LOOKUP +right. +.TP +.B CAP_MKFIFOAT +Permit +.BR mkfifoat (2). +This right includes the +.B CAP_LOOKUP +right. +.TP +.B CAP_MKNODAT +Permit +.BR mknodat (2). +This right includes the +.B CAP_LOOKUP +right. +.TP +.B CAP_MMAP +Permit +.BR mmap (2) +with the +.B PROT_NONE +protection. +.TP +.B CAP_MMAP_R +Permit +.BR mmap (2) +with the +.B PROT_READ +protection. +This right includes the +.B CAP_READ +and +.B CAP_SEEK +rights. +.TP +.B CAP_MMAP_RW +An alias to +.B CAP_MMAP_R +and +.BR CAP_MMAP_W . +.TP +.B CAP_MMAP_RWX +An alias to +.BR CAP_MMAP_R , +.B CAP_MMAP_W +and +.BR CAP_MMAP_X . +.TP +.B CAP_MMAP_RX +An alias to +.B CAP_MMAP_R +and +.BR CAP_MMAP_X . +.TP +.B CAP_MMAP_W +Permit +.BR mmap (2) +with the +.B PROT_WRITE +protection. +This right includes the +.B CAP_WRITE +and +.B CAP_SEEK +rights. +.TP +.B CAP_MMAP_WX +An alias to +.B CAP_MMAP_W +and +.BR CAP_MMAP_X . +.TP +.B CAP_MMAP_X +Permit +.BR mmap (2) +with the +.B PROT_EXEC +protection. +This right includes the +.B CAP_SEEK +right. +.TP +.B CAP_PDGETPID +Permit +.BR pdgetpid (2). +.TP +.B CAP_PDKILL +Permit +.BR pdkill (2). +.TP +.B CAP_PDWAIT +Permit +.BR pdwait4 (2). +.TP +.B CAP_PEELOFF +Permit +.BR sctp_peeloff (3). +.TP +.B CAP_PREAD +An alias to +.B CAP_READ +and +.BR CAP_SEEK . +.TP +.B CAP_PWRITE +An alias to +.B CAP_SEEK +and +.BR CAP_WRITE . +.TP +.B CAP_READ +Permit +.BR openat (2) +with the +.BR O_RDONLY flag, +.BR read (2), +.BR readv (2), +.BR recv (2), +.BR recvfrom (2), +.BR recvmsg (2), +.BR pread (2) +( +.B CAP_SEEK +is also required), +.BR preadv (2) +( +.B CAP_SEEK +is also required) and related system calls. +.TP +.B CAP_RECV +An alias to +.BR CAP_READ . +.TP +.B CAP_RENAMEAT +Permit +.BR renameat (2). +This right is required on the source directory descriptor. +This right includes the +.B CAP_LOOKUP +right. +.TP +.B CAP_SEEK +Permit operations that seek on the file descriptor, such as +.BR lseek (2), +but also required for I/O system calls that can read or write at any position +in the file, such as +.BR pread (2) +and +.BR pwrite (2). +.TP +.B CAP_SEND +An alias to +.BR CAP_WRITE . +.TP +.B CAP_SETSOCKOPT +Permit +.BR setsockopt (2); +this controls various aspects of socket behavior and may affect binding, +connecting, and other behaviors with global scope. +.TP +.B CAP_SHUTDOWN +Permit explicit +.BR shutdown (2); +closing the socket will also generally shut down any connections on it. +.TP +.B CAP_SYMLINKAT +Permit +.BR symlinkat (2). +This right includes the +.B CAP_LOOKUP +right. +.TP +.B CAP_UNLINKAT +Permit +.BR unlinkat (2) +and +.BR renameat (2). +This right is only required for +.BR renameat (2) +on the destination directory descriptor if the destination object already +exists and will be removed by the rename. +This right includes the +.B CAP_LOOKUP +right. +.TP +.B CAP_WRITE +Allow +.BR openat (2) +with +.B O_WRONLY +and +.B O_APPEND +flags set, +.BR send (2), +.BR sendmsg (2), +.BR sendto (2), +.BR write (2), +.BR writev (2), +.BR pwrite (2), +.BR pwritev (2) +and related system calls. +For +.BR sendto (2) +with a non-NULL connection address, +.B CAP_CONNECT +is also required. +For +.BR openat (2) +with the +.B O_WRONLY +flag, but without the +.B O_APPEND +flag, +.B CAP_SEEK +is also required. +For +.BR pwrite (2) +and +.BR pwritev (2) +.B CAP_SEEK +is also required. +.SH VERSIONS +Capsicum support was originally added to the kernel in version 3.???. +.SH SEE ALSO +.BR cap_enter (3), +.BR cap_fcntls_limit (3), +.BR cap_ioctls_limit (3), +.BR cap_rights_limit (2), +.BR cap_rights_limit (3), +.BR capsicum (7)
--
2.0.0.526.g5318336
PATCH 4/6 cap_rights_limit.2: limit FD rights for Capsicum
In reply to David Drysdale--- man2/cap_rights_limit.2 | 241 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 man2/cap_rights_limit.2
man2/cap_rights_limit.2 new file +241 -0
@@ -0,0 +1,241 @@ +.\" +.\" Copyright (c) 2008-2010 Robert N. M. Watson +.\" Copyright (c) 2012-2013 The FreeBSD Foundation +.\" Copyright (c) 2013-2014 Google, Inc. +.\" All rights reserved. +.\" +.\" %%%LICENSE_START(BSD_2_CLAUSE) +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" %%%LICENSE_END +.\" +.TH CAP_RIGHTS_LIMIT 2 2014-05-07 "Linux" "Linux Programmer's Manual" +.SH NAME +cap_rights_limit \- limit Capsicum capability rights +.SH SYNOPSIS +.nf +.B #include <sys/capsicum.h> +.sp +.BI "int cap_rights_limit(int " fd ", const struct cap_rights *" rights , +.BI " unsigned int " fcntls , +.BI " int " nioctls ", unsigned int *" ioctls ); +.SH DESCRIPTION +When a file descriptor is created by a function such as +.BR accept (2), +.BR accept4 (2), +.BR creat (2), +.BR epoll_create (2), +.BR eventfd (2), +.BR mq_open (2), +.BR open (2), +.BR openat (2), +.BR pdfork (2), +.BR pipe (2), +.BR pipe2 (2), +.BR signalfd (2), +.BR socket (2), +.BR socketpair (2) +or +.BR timerfd_create (2), +it implicitly has all Capsicum capability rights. +Those rights can be reduced (but never expanded) by using the +.BR cap_rights_limit () +system call. +Once Capsicum capability rights are reduced, operations on the file descriptor +.I fd +will be limited to those permitted by the remainder of the arguments. +.PP +The +.I rights +argument describes the primary rights for the file descriptor, as a +.I cap_rights +structure: +.in +4n +.nf + +#define CAP_RIGHTS_VERSION_00 0 +#define CAP_RIGHTS_VERSION_01 1 +#define CAP_RIGHTS_VERSION_02 2 +#define CAP_RIGHTS_VERSION_03 3 +#define CAP_RIGHTS_VERSION CAP_RIGHTS_VERSION_00 +struct cap_rights { + __u64 cr_rights[CAP_RIGHTS_VERSION + 2]; +}; +.fi +.in +.PP +The contents of the +.I cr_rights +array are encoded as follows. +.IP \(bu +The top 2 bits of +.I cr_rights[0] +hold the array size minus 2, allowing for an array size between 2 and 5 inclusive. +.IP \(bu +The top 2 bits of the other +.I cr_rights +entries are zero. +.IP \(bu +The following 5 bits of each array entry indicate its position in the array, +from 0b00001 for entry [0], 0b00010 for entry [1], up to 0b10000 for entry [4]. +.IP \(bu +The remaining 57 bits of each array entry identify a particular primary +right. +.PP +This encoding allows for future expansion in the number of distinct rights; +for example, a +.I cap_rights +structure holding the +.B CAP_READ +and +.B CAP_BINDAT +rights would have contents of +.in +4n +.nf +0x0200000000000001 0x0400000000001000 +.fi +.in +as a version 0 array, but would be encoded in a larger version 1 array as +.in +4n +.nf +0x4200000000000001 0x0400000000001000 0x0800000000000000. +.fi +.in +.PP +User programs should prepare the contents of the +.I cap_rights +structure with the +.BR cap_rights_init (3) +family of functions. +The complete list of primary rights can be found in the +.BR rights (7) +manual page. +.PP +If a file descriptor is granted the +.B CAP_FCNTL +primary capability right, some specific +.BR fcntl (2) +commands can be selectively disallowed with the +.I fcntls +argument. The following flags may be specified in the +.I fcntls +argument for this: +.TP +.B CAP_FCNTL_GETFL +Permit +.B F_GETFL +command. +.TP +.B CAP_FCNTL_SETFL +Permit +.B F_SETFL +command. +.TP +.B CAP_FCNTL_GETOWN +Permit +.B F_GETOWN +command. +.TP +.B CAP_FCNTL_SETOWN +Permit +.B F_SETOWN +command. +.PP +A value of +.B CAP_FCNTL_ALL +for the +.I fcntls +argument leaves the set of allowed +.BR fcntl (2) +commands unchanged. +.PP +However, note that +.BR fcntl (2) +commands that are analogous to other system calls +(such as +.B F_DUPFD +or +.BR F_GETPIPE_SZ ) +are not controlled by the +.B CAP_FCNTL +right and so do not have corresponding values in the +.I fcntls +argument. +.PP +If a file descriptor is granted the +.B CAP_IOCTL +capability right, the list of allowed +.BR ioctl (2) +commands can be selectively reduced (but never expanded) using the +.I nioctls +and +.I ioctls +arguments. +The +.I ioctls +argument is an array of +.BR ioctl (2) +command values and the +.I nioctls +argument specifies the number of elements in the array. +.PP +If the +.I nioctls +argument is -1 or 0, the +.I ioctls +argument is ignored, and either all +.BR ioctl (2) +operations or no +.BR ioctl (2) +operations (respectively) will be allowed. +.PP +Capsicum capability rights assigned to a file descriptor can be obtained with the +.BR cap_rights_get (2) +system call. +.SH RETURN VALUE +.BR cap_rights_limit () +returns zero on success. On error, -1 is returned and +.I errno +is set appropriately. +.SH ERRORS +.TP +.B EBADF +.I fd +isn't a valid open file descriptor. +.TP +.B EINVAL +An invalid set of rights has been requested in +.IR rights . +.TP +.B ENOMEM +Out of memory. +.TP +.B ENOTCAPABLE +The arguments contain capability rights not present for the given file descriptor (Capsicum +capability rights list can only be reduced, never expanded). +.SH VERSION +Capsicum support was added to the kernel in version 3.???. +.SH SEE ALSO +.BR cap_enter (2), +.BR cap_rights_get (2), +.BR cap_rights_init (3), +.BR capsicum (7), +.BR rights (7)
--
2.0.0.526.g5318336
PATCH 5/6 cap_rights_get.2: retrieve Capsicum fd rights
In reply to David Drysdale--- man2/cap_rights_get.2 | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 man2/cap_rights_get.2
man2/cap_rights_get.2 new file +134 -0
@@ -0,0 +1,134 @@ +.\" +.\" Copyright (c) 2008-2010 Robert N. M. Watson +.\" Copyright (c) 2012-2013 The FreeBSD Foundation +.\" Copyright (c) 2013-2014 Google, Inc. +.\" All rights reserved. +.\" +.\" %%%LICENSE_START(BSD_2_CLAUSE) +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" %%%LICENSE_END +.\" +.TH CAP_RIGHTS_GET 2 2014-05-07 "Linux" "Linux Programmer's Manual" +.SH NAME +cap_rights_get \- retrieve Capsicum capability rights +.SH SYNOPSIS +.nf +.B #include <sys/capsicum.h> +.sp +.BI "int cap_rights_get(int " fd ", struct cap_rights *" rights , +.BI " unsigned int *" fcntls , +.BI " int *" nioctls ", unsigned int *" ioctls ); +.SH DESCRIPTION +Obtain the current Capsicum capability rights for a file descriptor. +.PP +The function will fill the +.I rights +argument (if non-NULL) with the primary capability rights of the +.I fd +descriptor. The +.I rights +field is a +.I cap_rights +structure, as described in +.BR cap_rights_limit (2), +and can be examined with the +.BR cap_rights_is_set (3) +family of functions. The complete list of primary rights can be found in the +.BR rights (7) +manual page. +.PP +If the +.I fcntls +argument is non-NULL, it will be filled in with a bitmask of allowed +.BR fcntl (2) +commands; see +.BR cap_rights_limit (2) +for values. If the file descriptor does not have the +.B CAP_FCNTL +primary right, the returned +.I fcntls +value will be zero. +.PP +If the +.I nioctls +argument is non-NULL, it will be filled in with the number of allowed +.BR ioctl (2) +commands, or with the value CAP_IOCTLS_ALL to indicate that all +.BR ioctl (2) +commands are allowed. If the file descriptor does not have the +.B CAP_IOCTL +primary right, the returned +.I nioctls +value will be zero. +.PP +If the +.I ioctls +argument is non-NULL, the caller should specify the size of the +provided buffer as the initial value of the +.I nioctls +argument (as a count of the number of +.BR ioctl (2) +command values the buffer can hold). +On successful completion of the system call, the +.I ioctls +buffer is filled with the +.BR ioctl (2) +command values, up to maximum of the initial value of +.BR nioctls . +.PP +If all +.BR ioctl (2) +commands are allowed (the +.B CAP_IOCTL +primary capability right is assigned to the file descriptor and the +set of allowed +.BR ioctl (2) +commands was never limited for this file descriptor), the +system call will not modify the buffer pointed to by the +.I ioctls +argument. +.PP +Capsicum capability rights assigned to a file descriptor can be reduced with the +.BR cap_rights_limit (2) +system call. +.SH RETURN VALUE +.BR cap_rights_get () +returns zero on success. On error, -1 is returned and +.I errno +is set appropriately. +.SH ERRORS +.TP +.B EBADF +.I fd +isn't a valid open file descriptor. +.TP +.B EFAULT +Invalid pointer argument. +.SH VERSION +Capsicum support was added to the kernel in version 3.???. +.SH SEE ALSO +.BR cap_enter (2), +.BR cap_rights_limit (2), +.BR cap_rights_init (3), +.BR capsicum (7), +.BR rights (7) +
--
2.0.0.526.g5318336
PATCH 6/6 prctl.2: describe PR_SET_OPENAT_BENEATH/PR_GET_OPENAT_BENEATH
In reply to David Drysdale--- man2/prctl.2 | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
man2/prctl.2 +35 -0
@@ -295,6 +295,41 @@ A value of 1 indicates .BR execve (2) will operate in the privilege-restricting mode described above. .TP +.BR PR_SET_OPENAT_BENEATH " (since Linux 3.??)" +Set the calling process's +.I openat_beneath +bit to the value in +.IR arg2 . +With +.I openat_beneath +set to 1, all +.BR openat (2) +and +.BR open (2) +operations act as though the +.B O_BENEATH +flag is set. +Once set, this bit cannot be unset. +The setting of this bit is inherited by children created by +.BR fork (2) +and +.BR clone (2), +and preserved across +.BR execve (2). +.TP +.BR PR_GET_OPENAT_BENEATH " (since Linux 3.??)" +Return (as the function result) the value of the +.I openat_beneath +bit for the current process. +A value of 0 indicates the regular behavior. +A value of 1 indicates that +.BR openat (2) +and +.BR open (2) +will operate in the implicit +.B O_BENEATH +mode described above. +.TP .BR PR_SET_PDEATHSIG " (since Linux 2.1.57)" Set the parent process death signal of the calling process to \fIarg2\fP (either a signal value
Signature · 1 line
2.0.0.526.g5318336
Quoted 39 lines · David Drysdale
The last couple of versions of FreeBSD (9.x/10.x) have included the Capsicum security framework [1], which allows security-aware applications to sandbox themselves in a very fine-grained way. For example, OpenSSH now (>= 6.5) uses Capsicum in its FreeBSD version to restrict sshd's credentials checking process, to reduce the chances of credential leakage.
It would be good to have equivalent functionality in Linux, so I've been working on getting the Capsicum framework running in the kernel, and I'd appreciate some feedback/opinions on the general approach.
I'm attaching a corresponding draft patchset for reference, but hopefully this cover email can cover the significant features to save everyone having to look through the code details. (It does mean this is a long email though -- apologies for that.)
1) Capsicum Capabilities
------------------------The most significant aspect of Capsicum is associating *rights* with (some) file descriptors, so that the kernel only allows operations on an FD if the rights permit it. This allows userspace applications to sandbox themselves by tightly constraining what's allowed with both input and outputs; for example, tcpdump might restrict itself so it can only read from the network FD, and only write to stdout.
The kernel thus needs to police the rights checks for these file descriptors (referred to as 'Capsicum capabilities', completely different than POSIX.1e capabilities), and the best place to do this is at the points where a file descriptor from userspace is converted to a struct file * within the kernel.
[Policing the rights checks anywhere else, for example at the system call boundary, isn't a good idea because it opens up the possibility of time-of-check/time-of-use (TOCTOU) attacks [2] where FDs are changed (as openat/close/dup2 are allowed in capability mode) between the 'check' at syscall entry and the 'use' at
fget()invocation.]
Quoted 6 lines
However, this does lead to quite an invasive change to the kernel -- every invocation of
fget()or similar functions (fdget(),sockfd_lookup(),user_path_at(),...) needs to be annotated with the rights associated with the specific operations that will be performed on the struct file. There are ~100 such invocations that need annotation.
And it is silly. Roughly you just need a locking version of fcntl(F_SETFL).
That is make the restriction in the struct file not in the fd to file lookup.
Files in unix have been capabilities for more than 20 years. That is what file descriptor passing in unix domain sockets are all about. We don't need an additional ``capability rights'' layer on top of file descriptors.
In fact internal to linux with FMODE_READ and friends we already have restrictions on which methods are allowed on linux file descriptors. So this whole entire abstraction layer you are adding seems just plain broken.
Going farther one huge thing your work and the capsicum work in general is missing is an implementation of revoke. With a little care a good implementation of bits reporting and controlling which methods are available on which file descriptors should be a good start on a revoke implementation for linux as well.
Quoted 9 lines
2) Capsicum Capabilities Data Structure
---------------------------------------Internally, the rights associated with a Capsicum capability FD are stored in a special struct file wrapper. For a normal file, the rights check inside
fget()falls through, but for a capability wrapper the rights in the wrapper are checked and (if capable) the underlying wrapped struct file is returned.
Quoted 5 lines
[This is approximately the implementation that was present in FreeBSD 9.x. For FreeBSD 10.x, the wrapper file was removed and the rights associated with a file descriptor are now stored in the fdtable. As that impacts memory use for all processes, whether Capsicum users or not, I've stuck with the FreeBSD 9.x approach.]
I have already mentioned that this is an insane choice of semantics right? Adding an extra layer on top of a data structure that is a perfectly good restrictor of rights.
If you can't add the restriction on struct file itself I would argue that the semantics of capsicum are fundamentally broken.
I can not imagine why in the world you would want and extra layer of indirection, complication, and maintenance.
Quoted 13 lines
3) Allowing Capability Mode
---------------------------Capsicum also includes 'capability mode', which locks down the available syscalls so the rights restrictions can't just be bypassed by opening new file descriptors. More precisely, capability mode prevents access to syscalls that access global namespaces, such as the filesystem or the IP:port space.
The existing seccomp-bpf functionality of the kernel is a good mechanism for implementing capability mode, but there are a few additional details that also need to be addressed.
Quoted 5 lines
a) The capability mode filter program needs to apply process-wide, not just to the current thread.
b) In capability mode, new files can still be opened with openat(2) but only if they are beneath an existing directory file descriptor.
Which raises the question is that worth it?
Quoted 2 lines
c) In capability mode it should still be possible for a process to send signals to itself with kill(2)/tgkill(2).
Again is it worth it?
I would think you would want capability mode to default to the minium set of system calls you could get away with (to keep kernel code auditing to a minium) and only add things if the performance gain of using the syscall exceeds the pain.
If you look at a kernel like sel4 it succeeds in with an object capability model with many fewer system calls than you are proposing to export. Roughly just read, write and close.
Consider the fact if you really want a kernel layer you can completely trust and rely on someone needs to write a formal proof of that layer. Short of that someone certainly needs to audit the kernel code very closely so simplicity of semantics and simplicity of implementation are very important.
Quoted 28 lines
This v2 patchset copes with these as follows:
a) Kees Cook's incoming seccomp(2) patchset covers thread synchronization of filters.
b) A new
prctl(PR_SET_OPENAT_BENEATH)operation implicitly sets theO_BENEATHflag (see below) for all file-open operations for all threads of the current process, by adding a newopenat_beneathflag intask_struct.c) An extension to the
seccomp_datastructure that includes the current task's tid and tgid values allows for BPF programs that check a kill(2)/tgkill(2) argument against the current thread, in a manner that is robust against fork(2)/clone(2).The combination of these features with the existing seccomp-bpf functionality gives the tools needed to implement capability mode.
4) New System Calls
-------------------To allow userspace applications to access the Capsicum capability functionality, I'm proposing two new system calls:
cap_rights_limit(2)andcap_rights_get(2). I guess these could potentially be implemented elsewhere (e.g. as fcntl(2) operations?) but the changes seem significant enough that new syscalls are warranted.
Quoted 4 lines
[FreeBSD 10.x actually includes six new syscalls for manipulating the rights associated with a Capsicum capability -- the capability rights can police that only specific fcntl(2) or ioctl(2) commands are allowed, and FreeBSD sets these with distinct syscalls.]
ioctls? In a sandbox? Ick.
Quoted 16 lines
5) New openat(2)
O_BENEATHFlag
-------------------------------For Capsicum capabilities that are directory file descriptors, the Capsicum framework only allows
openat(cap_dfd, path, ...)operations to work for files that are beneath the specified directory (and even that only when the directory FD has theCAP_LOOKUPright), rejecting paths that start with "/" or include "..". The same restriction applies process-wide for a process in capability mode.As this seemed like functionality that might be more generally useful, I've implemented it independently as a new
O_BENEATHflag for openat(2). The Capsicum code then always triggers the use of that flag when the dfd is a Capsicum capability, or when the prctl(2) command described above is in play.
Quoted 3 lines
[FreeBSD has the openat(2) relative-only behaviour for capability DFDs and processes in capability mode, but does not include the
O_BENEATHflag.]
If you are going to allow open I would think the simple solution here is to just create a mount namespace that only has available the files you would like to export to the process, and force all of the file descriptors into that mount namespace.
Eric
Paolo Bonzini
maintainerIl 26/07/2014 23:04, Eric W. Biederman ha scritto:
Quoted 26 lines
Quoted 19 lines
The most significant aspect of Capsicum is associating *rights* with (some) file descriptors, so that the kernel only allows operations on an FD if the rights permit it. This allows userspace applications to sandbox themselves by tightly constraining what's allowed with both input and outputs; for example, tcpdump might restrict itself so it can only read from the network FD, and only write to stdout.
The kernel thus needs to police the rights checks for these file descriptors (referred to as 'Capsicum capabilities', completely different than POSIX.1e capabilities), and the best place to do this is at the points where a file descriptor from userspace is converted to a struct file * within the kernel.
[Policing the rights checks anywhere else, for example at the system call boundary, isn't a good idea because it opens up the possibility of time-of-check/time-of-use (TOCTOU) attacks [2] where FDs are changed (as openat/close/dup2 are allowed in capability mode) between the 'check' at syscall entry and the 'use' at
fget()invocation.]Quoted 6 lines
However, this does lead to quite an invasive change to the kernel -- every invocation of
fget()or similar functions (fdget(),sockfd_lookup(),user_path_at(),...) needs to be annotated with the rights associated with the specific operations that will be performed on the struct file. There are ~100 such invocations that need annotation.
Quoted 5 lines
And it is silly. Roughly you just need a locking version of
fcntl(F_SETFL).That is make the restriction in the struct file not in the fd to file lookup.
No, they have to be in the file descriptor. The same file descriptor can be dup'ed and passed with different capabilities to different processes.
Say you pass an eventfd to a process with SCM_RIGHTS, and you want to only allow the process to write to it.
Quoted 9 lines
Quoted 9 lines
4) New System Calls
-------------------To allow userspace applications to access the Capsicum capability functionality, I'm proposing two new system calls:
cap_rights_limit(2)andcap_rights_get(2). I guess these could potentially be implemented elsewhere (e.g. as fcntl(2) operations?) but the changes seem significant enough that new syscalls are warranted.
Quoted 6 lines
Quoted 4 lines
[FreeBSD 10.x actually includes six new syscalls for manipulating the rights associated with a Capsicum capability -- the capability rights can police that only specific fcntl(2) or ioctl(2) commands are allowed, and FreeBSD sets these with distinct syscalls.]
ioctls? In a sandbox? Ick.
KVM? X11? Both of them use loads of ioctls. I'm less sure of the benefit of picking which fcntls to allow.
Paolo
Quoted 48 lines · Eric W. Biederman
Quoted 39 lines · David Drysdale
The last couple of versions of FreeBSD (9.x/10.x) have included the Capsicum security framework [1], which allows security-aware applications to sandbox themselves in a very fine-grained way. For example, OpenSSH now (>= 6.5) uses Capsicum in its FreeBSD version to restrict sshd's credentials checking process, to reduce the chances of credential leakage.
It would be good to have equivalent functionality in Linux, so I've been working on getting the Capsicum framework running in the kernel, and I'd appreciate some feedback/opinions on the general approach.
I'm attaching a corresponding draft patchset for reference, but hopefully this cover email can cover the significant features to save everyone having to look through the code details. (It does mean this is a long email though -- apologies for that.)
1) Capsicum Capabilities
------------------------The most significant aspect of Capsicum is associating *rights* with (some) file descriptors, so that the kernel only allows operations on an FD if the rights permit it. This allows userspace applications to sandbox themselves by tightly constraining what's allowed with both input and outputs; for example, tcpdump might restrict itself so it can only read from the network FD, and only write to stdout.
The kernel thus needs to police the rights checks for these file descriptors (referred to as 'Capsicum capabilities', completely different than POSIX.1e capabilities), and the best place to do this is at the points where a file descriptor from userspace is converted to a struct file * within the kernel.
[Policing the rights checks anywhere else, for example at the system call boundary, isn't a good idea because it opens up the possibility of time-of-check/time-of-use (TOCTOU) attacks [2] where FDs are changed (as openat/close/dup2 are allowed in capability mode) between the 'check' at syscall entry and the 'use' at
fget()invocation.]Quoted 6 lines
However, this does lead to quite an invasive change to the kernel -- every invocation of
fget()or similar functions (fdget(),sockfd_lookup(),user_path_at(),...) needs to be annotated with the rights associated with the specific operations that will be performed on the struct file. There are ~100 such invocations that need annotation.
Quoted 5 lines
And it is silly. Roughly you just need a locking version of
fcntl(F_SETFL).That is make the restriction in the struct file not in the fd to file lookup.
The file status flags are far too coarse -- for example, O_RDONLY doesn't prevent fchmod(2).
Also, because they're associated with the struct file there is no way to pass a file descriptor with only a subset of rights across a UNIX socket (how do I send a read-only FD corresponding to my O_RDWR file?).
Quoted 4 lines
Files in unix have been capabilities for more than 20 years. That is what file descriptor passing in unix domain sockets are all about. We don't need an additional ``capability rights'' layer on top of file descriptors.
True, the ability to pass FDs across UNIX sockets is one of the key things that makes them analogous to object-capabilities and suitable as the substratum for Capsicum. But the coarseness of the existing rights, and the lack of coherent policing of those rights, means that an (opt-in) extension like Capsicum is useful.
Quoted 10 lines
In fact internal to linux with
FMODE_READand friends we already have restrictions on which methods are allowed on linux file descriptors. So this whole entire abstraction layer you are adding seems just plain broken.Going farther one huge thing your work and the capsicum work in general is missing is an implementation of revoke. With a little care a good implementation of bits reporting and controlling which methods are available on which file descriptors should be a good start on a revoke implementation for linux as well.
Yeah, revocation is interesting. There was been some discussion of it for Capsicum a while ago:
https://lists.cam.ac.uk/pipermail/cl-capsicum-discuss/2011-January/msg00002.html
but I don't think a firm conclusion was reached. As I understand it, there's also a theoretical argument that revocation can be constructed on top of the capability system, by handing out capabilities to proxy objects that can then change their behaviour so they no longer pass operations through.
Quoted 19 lines
Quoted 9 lines
2) Capsicum Capabilities Data Structure
---------------------------------------Internally, the rights associated with a Capsicum capability FD are stored in a special struct file wrapper. For a normal file, the rights check inside
fget()falls through, but for a capability wrapper the rights in the wrapper are checked and (if capable) the underlying wrapped struct file is returned.Quoted 5 lines
[This is approximately the implementation that was present in FreeBSD 9.x. For FreeBSD 10.x, the wrapper file was removed and the rights associated with a file descriptor are now stored in the fdtable. As that impacts memory use for all processes, whether Capsicum users or not, I've stuck with the FreeBSD 9.x approach.]
I have already mentioned that this is an insane choice of semantics right? Adding an extra layer on top of a data structure that is a perfectly good restrictor of rights.
Quoted 5 lines
If you can't add the restriction on struct file itself I would argue that the semantics of capsicum are fundamentally broken.
I can not imagine why in the world you would want and extra layer of indirection, complication, and maintenance.
See above.
Quoted 16 lines
Quoted 16 lines
3) Allowing Capability Mode
---------------------------Capsicum also includes 'capability mode', which locks down the available syscalls so the rights restrictions can't just be bypassed by opening new file descriptors. More precisely, capability mode prevents access to syscalls that access global namespaces, such as the filesystem or the IP:port space.
The existing seccomp-bpf functionality of the kernel is a good mechanism for implementing capability mode, but there are a few additional details that also need to be addressed.
a) The capability mode filter program needs to apply process-wide, not just to the current thread.
Quoted 6 lines
Quoted 2 lines
b) In capability mode, new files can still be opened with openat(2) but only if they are beneath an existing directory file descriptor.
Which raises the question is that worth it?
From a practical point of view, still allowing (directory-relative) filesystem
access means it's much easier for an application to adapt to Capsicum.
A simple example is something like unzip/tar -xf, where locking it down is conceptually as simple as:
- apply a CAP_READ restriction to the FD for the input file
- apply a CAP_WRITE+CAP_LOOKUP restriction to the DFD for the output directory
- enter capability mode.
Then the worst a malicious input file can do is to write files under the output directory -- which it could do anyway.
Similarly, migrating an application that writes temporary files out to some tempdir is much more straightforward if openat(dfd,...) is still allowed.
Quoted 9 lines
Quoted 2 lines
c) In capability mode it should still be possible for a process to send signals to itself with kill(2)/tgkill(2).
Again is it worth it?
I would think you would want capability mode to default to the minium set of system calls you could get away with (to keep kernel code auditing to a minium) and only add things if the performance gain of using the syscall exceeds the pain.
The set of syscalls allowed in capability mode is based more on the principle of restricting access to global namespaces, to prevent (in particular) the confused deputy problem. Allowing kill(self) is a pragmatic compromise to make it easier to migrate existing applications to use Capsicum.
Quoted 9 lines
If you look at a kernel like sel4 it succeeds in with an object capability model with many fewer system calls than you are proposing to export. Roughly just read, write and close.
Consider the fact if you really want a kernel layer you can completely trust and rely on someone needs to write a formal proof of that layer. Short of that someone certainly needs to audit the kernel code very closely so simplicity of semantics and simplicity of implementation are very important.
seL4 may have a much simpler (and more formally-correct) model, but the aim of Capsicum is to get some of the benefits of that well-analysed model without the need for a (massive) migration effort -- and in a way that allows co-existence of migrated and unmigrated code.
Quoted 55 lines
Quoted 28 lines
This v2 patchset copes with these as follows:
a) Kees Cook's incoming seccomp(2) patchset covers thread synchronization of filters.
b) A new
prctl(PR_SET_OPENAT_BENEATH)operation implicitly sets theO_BENEATHflag (see below) for all file-open operations for all threads of the current process, by adding a newopenat_beneathflag intask_struct.c) An extension to the
seccomp_datastructure that includes the current task's tid and tgid values allows for BPF programs that check a kill(2)/tgkill(2) argument against the current thread, in a manner that is robust against fork(2)/clone(2).The combination of these features with the existing seccomp-bpf functionality gives the tools needed to implement capability mode.
4) New System Calls
-------------------To allow userspace applications to access the Capsicum capability functionality, I'm proposing two new system calls:
cap_rights_limit(2)andcap_rights_get(2). I guess these could potentially be implemented elsewhere (e.g. as fcntl(2) operations?) but the changes seem significant enough that new syscalls are warranted.Quoted 4 lines
[FreeBSD 10.x actually includes six new syscalls for manipulating the rights associated with a Capsicum capability -- the capability rights can police that only specific fcntl(2) or ioctl(2) commands are allowed, and FreeBSD sets these with distinct syscalls.]
ioctls? In a sandbox? Ick.
Quoted 16 lines
5) New openat(2)
O_BENEATHFlag
-------------------------------For Capsicum capabilities that are directory file descriptors, the Capsicum framework only allows
openat(cap_dfd, path, ...)operations to work for files that are beneath the specified directory (and even that only when the directory FD has theCAP_LOOKUPright), rejecting paths that start with "/" or include "..". The same restriction applies process-wide for a process in capability mode.As this seemed like functionality that might be more generally useful, I've implemented it independently as a new
O_BENEATHflag for openat(2). The Capsicum code then always triggers the use of that flag when the dfd is a Capsicum capability, or when the prctl(2) command described above is in play.Quoted 3 lines
[FreeBSD has the openat(2) relative-only behaviour for capability DFDs and processes in capability mode, but does not include the
O_BENEATHflag.]
Quoted 6 lines
If you are going to allow open I would think the simple solution here is to just create a mount namespace that only has available the files you would like to export to the process, and force all of the file descriptors into that mount namespace.
Eric
I think that's a lot harder for application writers to do -- they would need to have CAP_SYS_ADMIN to set up the namespace & mounts before dropping privileges. And the net result would again be much less fine-grained (e.g. for the unzip example above, the specific capability rights prevent reading of any files that already exist in the output directory).
Quoted 39 lines · David Drysdale
Quoted 31 lines · Eric W. Biederman
Quoted 22 lines · David Drysdale
1) Capsicum Capabilities
------------------------The most significant aspect of Capsicum is associating *rights* with (some) file descriptors, so that the kernel only allows operations on an FD if the rights permit it. This allows userspace applications to sandbox themselves by tightly constraining what's allowed with both input and outputs; for example, tcpdump might restrict itself so it can only read from the network FD, and only write to stdout.
The kernel thus needs to police the rights checks for these file descriptors (referred to as 'Capsicum capabilities', completely different than POSIX.1e capabilities), and the best place to do this is at the points where a file descriptor from userspace is converted to a struct file * within the kernel.
[Policing the rights checks anywhere else, for example at the system call boundary, isn't a good idea because it opens up the possibility of time-of-check/time-of-use (TOCTOU) attacks [2] where FDs are changed (as openat/close/dup2 are allowed in capability mode) between the 'check' at syscall entry and the 'use' at
fget()invocation.]Quoted 6 lines
However, this does lead to quite an invasive change to the kernel -- every invocation of
fget()or similar functions (fdget(),sockfd_lookup(),user_path_at(),...) needs to be annotated with the rights associated with the specific operations that will be performed on the struct file. There are ~100 such invocations that need annotation.Quoted 5 lines
And it is silly. Roughly you just need a locking version of
fcntl(F_SETFL).That is make the restriction in the struct file not in the fd to file lookup.
Quoted 7 lines
The file status flags are far too coarse -- for example,
O_RDONLYdoesn't prevent fchmod(2).Also, because they're associated with the struct file there is no way to pass a file descriptor with only a subset of rights across a UNIX socket (how do I send a read-only FD corresponding to my
O_RDWRfile?).
You can't. Unix domain sockets pass struct file references. That is passing a file descriptor with a unix domain sockets is the equivlanet of dup().
You absolutely must make your struct file read-only before passing it, otherwise the the receiver will have a read-write instance.
This notion that a shared structure should have different semantics depending on who is looking at it, sounds like a maintenance nightmare to me.
Quoted 10 lines
Quoted 4 lines
Files in unix have been capabilities for more than 20 years. That is what file descriptor passing in unix domain sockets are all about. We don't need an additional ``capability rights'' layer on top of file descriptors.
True, the ability to pass FDs across UNIX sockets is one of the key things that makes them analogous to object-capabilities and suitable as the substratum for Capsicum. But the coarseness of the existing rights, and the lack of coherent policing of those rights, means that an (opt-in) extension like Capsicum is useful.
Finer grained restrictions are perfectly sensible.
I don't see how it make sense to place those restrictions in struct fdtable instead of in struct file.
I see two sensible implementations:
- Add a seccomp bpf filter to struct file.
- Add permission bits to struct file.
The bpf filter would allow for a simple code extension that would allow any arbitrary policy to be applied.
Adding permisison bits to struct file for gating access to file_operations and inode_operations would result in the fastest possible implementation and something very easy to audit and understand. But there like ioctl or read/write on IB control files where finer grained permissions might be desirable.
For a lot of operations we alread have bits like FMODE_READ and FMODE_CAN_READ on struct file for reasons such as performance so that only a single cache line needs to be touched.
It might be that it would be worth having both. Something cheap and genrally accessible and something fast.
Quoted 6 lines
Quoted 5 lines
Going farther one huge thing your work and the capsicum work in general is missing is an implementation of revoke. With a little care a good implementation of bits reporting and controlling which methods are available on which file descriptors should be a good start on a revoke implementation for linux as well.
Quoted 8 lines
Yeah, revocation is interesting. There was been some discussion of it for Capsicum a while ago:
https://lists.cam.ac.uk/pipermail/cl-capsicum-discuss/2011-January/msg00002.html
but I don't think a firm conclusion was reached. As I understand it, there's also a theoretical argument that revocation can be constructed on top of the capability system, by handing out capabilities to proxy objects that can then change their behaviour so they no longer pass operations through.
I believe the capsicum approach is so far from allowing general proxy objects that, that is not a compelling case.
Quoted 38 lines
Quoted 13 lines
Quoted 13 lines
3) Allowing Capability Mode
---------------------------Capsicum also includes 'capability mode', which locks down the available syscalls so the rights restrictions can't just be bypassed by opening new file descriptors. More precisely, capability mode prevents access to syscalls that access global namespaces, such as the filesystem or the IP:port space.
The existing seccomp-bpf functionality of the kernel is a good mechanism for implementing capability mode, but there are a few additional details that also need to be addressed.
Quoted 7 lines
Quoted 5 lines
a) The capability mode filter program needs to apply process-wide, not just to the current thread.
b) In capability mode, new files can still be opened with openat(2) but only if they are beneath an existing directory file descriptor.
Which raises the question is that worth it?
From a practical point of view, still allowing (directory-relative) filesystem access means it's much easier for an application to adapt to Capsicum.
A simple example is something like unzip/tar -xf, where locking it down is conceptually as simple as:
- apply aCAP_READrestriction to the FD for the input file
- apply a CAP_WRITE+CAP_LOOKUP restriction to the DFD for the output directory
- enter capability mode.
Then the worst a malicious input file can do is to write files under the output directory -- which it could do anyway.Similarly, migrating an application that writes temporary files out to some tempdir is much more straightforward if
openat(dfd,...)is still allowed.
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
c) In capability mode it should still be possible for a process to send signals to itself with kill(2)/tgkill(2).
seL4 may have a much simpler (and more formally-correct) model, but the aim of Capsicum is to get some of the benefits of that well-analysed model without the need for a (massive) migration effort -- and in a way that allows co-existence of migrated and unmigrated code.
Alright. About the same target as seccomp then. Aiming at something that is simple to adopt sounds like a reasonable goal.
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
5) New openat(2)
O_BENEATHFlag
-------------------------------For Capsicum capabilities that are directory file descriptors, the Capsicum framework only allows
openat(cap_dfd, path, ...)operations to work for files that are beneath the specified directory (and even that only when the directory FD has theCAP_LOOKUPright), rejecting paths that start with "/" or include "..". The same restriction applies process-wide for a process in capability mode.As this seemed like functionality that might be more generally useful, I've implemented it independently as a new
O_BENEATHflag for openat(2). The Capsicum code then always triggers the use of that flag when the dfd is a Capsicum capability, or when the prctl(2) command described above is in play.[FreeBSD has the openat(2) relative-only behaviour for capability DFDs and processes in capability mode, but does not include the
O_BENEATHflag.]
Quoted 8 lines
Quoted 4 lines
If you are going to allow open I would think the simple solution here is to just create a mount namespace that only has available the files you would like to export to the process, and force all of the file descriptors into that mount namespace.
I think that's a lot harder for application writers to do -- they would need to have
CAP_SYS_ADMINto set up the namespace & mounts before dropping privileges.
They only need CAP_SYS_ADMIN in their local user namespace, which anyone is allowed to create.
Quoted 4 lines
And the net result would again be much less
fine-grained (e.g. for the unzip example above, the specific capability rights prevent reading of any files that already exist in the output directory).
Nope. What you can implement today if you want fine grained limitations like this is to create a mount namespace with exactly the subdirectory tree you want to allow access to and to return a file descriptor that points into that mount namespace. (When complete the only user of that mount namespace would be your file descriptor).
In fact that solution is sufficiently performant and simple that even if you came up with a better user space interface for it that is how we would want to implement it.
And frankly that already exists today so I think a fairly large burden of proof needs to be met to suggest that we need to add additional functionality to the kernel.
Furthermore I think it makes sense for application authors to use as otherwise they will have to wait a year or so for the debates to be finished and your new functionality to be merged.
Additionally unless there is a process wide restriction to relative paths I can trivially escape your relative path implementation by simply doing open(.) and getting a struct file without any of those restrictions.
Eric
Paolo Bonzini
maintainerIl 28/07/2014 23:13, Eric W. Biederman ha scritto:
Quoted 3 lines
This notion that a shared structure should have different semantics depending on who is looking at it, sounds like a maintenance nightmare to me.
Isn't that already with seccomp BPF filters? You can have the parent process set a BPF filter that forbids read on that file descriptor or write on that file descriptor.
Effectively, this patchset provides the functionality to "hotplug" BPF filters on a running process as more file descriptors are passed via SCM_RIGHTS. Except it doesn't use BPF filters, and instead uses a separate set of discrete capabilities.
Quoted 2 lines
I see two sensible implementations:
- Add a seccomp bpf filter to struct file.
I proposed something like this, but it has some additional implementation complications. See the v1 thread.
But it does have to be in a file descriptor rather than a struct file. It's part of the model that two processes can have different views of the file descriptor (again my toy example is that of an eventfd that an unprivileged child process can only write to).
Quoted 4 lines
Additionally unless there is a process wide restriction to relative paths I can trivially escape your relative path implementation by simply doing open(.) and getting a struct file without any of those restrictions.
Yes, there is a prctl for that.
Paolo
Quoted 44 lines · Eric W. Biederman
Quoted 42 lines · David Drysdale
Quoted 31 lines · Eric W. Biederman
Quoted 22 lines · David Drysdale
1) Capsicum Capabilities
------------------------The most significant aspect of Capsicum is associating *rights* with (some) file descriptors, so that the kernel only allows operations on an FD if the rights permit it. This allows userspace applications to sandbox themselves by tightly constraining what's allowed with both input and outputs; for example, tcpdump might restrict itself so it can only read from the network FD, and only write to stdout.
The kernel thus needs to police the rights checks for these file descriptors (referred to as 'Capsicum capabilities', completely different than POSIX.1e capabilities), and the best place to do this is at the points where a file descriptor from userspace is converted to a struct file * within the kernel.
[Policing the rights checks anywhere else, for example at the system call boundary, isn't a good idea because it opens up the possibility of time-of-check/time-of-use (TOCTOU) attacks [2] where FDs are changed (as openat/close/dup2 are allowed in capability mode) between the 'check' at syscall entry and the 'use' at
fget()invocation.]Quoted 6 lines
However, this does lead to quite an invasive change to the kernel -- every invocation of
fget()or similar functions (fdget(),sockfd_lookup(),user_path_at(),...) needs to be annotated with the rights associated with the specific operations that will be performed on the struct file. There are ~100 such invocations that need annotation.Quoted 5 lines
And it is silly. Roughly you just need a locking version of
fcntl(F_SETFL).That is make the restriction in the struct file not in the fd to file lookup.
The file status flags are far too coarse -- for example,
O_RDONLYdoesn't prevent fchmod(2).
Quoted 8 lines
Quoted 4 lines
Also, because they're associated with the struct file there is no way to pass a file descriptor with only a subset of rights across a UNIX socket (how do I send a read-only FD corresponding to my
O_RDWRfile?).You can't. Unix domain sockets pass struct file references. That is passing a file descriptor with a unix domain sockets is the equivlanet of
dup().
That's my point -- if the rights are associated with the struct file there is no way of subsetting them. By associating rights with the file descriptor, you can pass around different views of the same underlying object with different rights.
Quoted 2 lines
You absolutely must make your struct file read-only before passing it, otherwise the the receiver will have a read-write instance.
Even if the struct file is read-only, the recipient will still be able to do lots of things (e.g. fchmod as mentioned previously)
Quoted 3 lines
This notion that a shared structure should have different semantics depending on who is looking at it, sounds like a maintenance nightmare to me.
I don't think it's that bad -- it's not completely different semantics, just a strict subset of allowed operations. Also, having a distinct ENOTCAPABLE errno value makes it easy to spot mismatches
of required & provided rights.
Quoted 16 lines
Quoted 10 lines
Quoted 4 lines
Files in unix have been capabilities for more than 20 years. That is what file descriptor passing in unix domain sockets are all about. We don't need an additional ``capability rights'' layer on top of file descriptors.
True, the ability to pass FDs across UNIX sockets is one of the key things that makes them analogous to object-capabilities and suitable as the substratum for Capsicum. But the coarseness of the existing rights, and the lack of coherent policing of those rights, means that an (opt-in) extension like Capsicum is useful.
Finer grained restrictions are perfectly sensible.
I don't see how it make sense to place those restrictions in struct fdtable instead of in struct file.
Quoted 6 lines
I see two sensible implementations:
- Add a seccomp bpf filter to struct file.
- Add permission bits to struct file.The bpf filter would allow for a simple code extension that would allow any arbitrary policy to be applied.
Paolo Bonzini suggested this on the previous iteration, but I think the associated complications are too overwhelming. Have a look at: https://lkml.org/lkml/2014/7/7/165
Quoted 28 lines
Adding permisison bits to struct file for gating access to
file_operationsandinode_operationswould result in the fastest possible implementation and something very easy to audit and understand. But there like ioctl or read/write on IB control files where finer grained permissions might be desirable.For a lot of operations we alread have bits like
FMODE_READandFMODE_CAN_READon struct file for reasons such as performance so that only a single cache line needs to be touched.It might be that it would be worth having both. Something cheap and genrally accessible and something fast.
Quoted 6 lines
Quoted 5 lines
Going farther one huge thing your work and the capsicum work in general is missing is an implementation of revoke. With a little care a good implementation of bits reporting and controlling which methods are available on which file descriptors should be a good start on a revoke implementation for linux as well.
Quoted 8 lines
Yeah, revocation is interesting. There was been some discussion of it for Capsicum a while ago:
https://lists.cam.ac.uk/pipermail/cl-capsicum-discuss/2011-January/msg00002.html
but I don't think a firm conclusion was reached. As I understand it, there's also a theoretical argument that revocation can be constructed on top of the capability system, by handing out capabilities to proxy objects that can then change their behaviour so they no longer pass operations through.
Quoted 2 lines
I believe the capsicum approach is so far from allowing general proxy objects that, that is not a compelling case.
Yeah, it is probably more of a theoretical argument than a practical one. But then, the requirement for revocation might fall into the same bucket -- more desirable in theory than in practice, particularly for the sort of application self-sandboxing and compartmentalization that Capsicum is particularly good for.
Quoted 87 lines
Quoted 38 lines
Quoted 13 lines
Quoted 13 lines
3) Allowing Capability Mode
---------------------------Capsicum also includes 'capability mode', which locks down the available syscalls so the rights restrictions can't just be bypassed by opening new file descriptors. More precisely, capability mode prevents access to syscalls that access global namespaces, such as the filesystem or the IP:port space.
The existing seccomp-bpf functionality of the kernel is a good mechanism for implementing capability mode, but there are a few additional details that also need to be addressed.
Quoted 7 lines
Quoted 5 lines
a) The capability mode filter program needs to apply process-wide, not just to the current thread.
b) In capability mode, new files can still be opened with openat(2) but only if they are beneath an existing directory file descriptor.
Which raises the question is that worth it?
From a practical point of view, still allowing (directory-relative) filesystem access means it's much easier for an application to adapt to Capsicum.
A simple example is something like unzip/tar -xf, where locking it down is conceptually as simple as:
- apply aCAP_READrestriction to the FD for the input file
- apply a CAP_WRITE+CAP_LOOKUP restriction to the DFD for the output directory
- enter capability mode.
Then the worst a malicious input file can do is to write files under the output directory -- which it could do anyway.Similarly, migrating an application that writes temporary files out to some tempdir is much more straightforward if
openat(dfd,...)is still allowed.Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
c) In capability mode it should still be possible for a process to send signals to itself with kill(2)/tgkill(2).
seL4 may have a much simpler (and more formally-correct) model, but the aim of Capsicum is to get some of the benefits of that well-analysed model without the need for a (massive) migration effort -- and in a way that allows co-existence of migrated and unmigrated code.
Alright. About the same target as seccomp then. Aiming at something that is simple to adopt sounds like a reasonable goal.
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
5) New openat(2)
O_BENEATHFlag
-------------------------------For Capsicum capabilities that are directory file descriptors, the Capsicum framework only allows
openat(cap_dfd, path, ...)operations to work for files that are beneath the specified directory (and even that only when the directory FD has theCAP_LOOKUPright), rejecting paths that start with "/" or include "..". The same restriction applies process-wide for a process in capability mode.As this seemed like functionality that might be more generally useful, I've implemented it independently as a new
O_BENEATHflag for openat(2). The Capsicum code then always triggers the use of that flag when the dfd is a Capsicum capability, or when the prctl(2) command described above is in play.[FreeBSD has the openat(2) relative-only behaviour for capability DFDs and processes in capability mode, but does not include the
O_BENEATHflag.]Quoted 8 lines
Quoted 4 lines
If you are going to allow open I would think the simple solution here is to just create a mount namespace that only has available the files you would like to export to the process, and force all of the file descriptors into that mount namespace.
I think that's a lot harder for application writers to do -- they would need to have
CAP_SYS_ADMINto set up the namespace & mounts before dropping privileges.They only need
CAP_SYS_ADMINin their local user namespace, which anyone is allowed to create.Quoted 4 lines
And the net result would again be much less
fine-grained (e.g. for the unzip example above, the specific capability rights prevent reading of any files that already exist in the output directory).
Quoted 5 lines
Nope. What you can implement today if you want fine grained limitations like this is to create a mount namespace with exactly the subdirectory tree you want to allow access to and to return a file descriptor that points into that mount namespace. (When complete the only user of that mount namespace would be your file descriptor).
How does that solve the particular example I mentioned? The DFD within the mount namespace will still allow any operation on any file that's already in the subdirectory -- or am I misunderstanding something?
Quoted 12 lines
In fact that solution is sufficiently performant and simple that even if you came up with a better user space interface for it that is how we would want to implement it.
And frankly that already exists today so I think a fairly large burden of proof needs to be met to suggest that we need to add additional functionality to the kernel.
Furthermore I think it makes sense for application authors to use as otherwise they will have to wait a year or so for the debates to be finished and your new functionality to be merged.
Quoted 6 lines
Additionally unless there is a process wide restriction to relative paths I can trivially escape your relative path implementation by simply doing open(.) and getting a struct file without any of those restrictions.
Eric
Capability mode would impose a process wide restriction to relative paths -- see sections 3) and 5) of the original mail.
I have cut this down to just focus on O_BENEATH openat case.
Quoted 9 lines · David Drysdale
Quoted 5 lines · Eric W. Biederman
Nope. What you can implement today if you want fine grained limitations like this is to create a mount namespace with exactly the subdirectory tree you want to allow access to and to return a file descriptor that points into that mount namespace. (When complete the only user of that mount namespace would be your file descriptor).
Quoted 4 lines
How does that solve the particular example I mentioned? The DFD within the mount namespace will still allow any operation on any file that's already in the subdirectory -- or am I misunderstanding something?
The goal was to bound the DFD to the directory and all of it's subdirectories such that openat(dfd, "../../..") would open the dfd, and that further opens of other directories would also not allow you to escape.
Since the mount namespace only contains the choosen directory and it's subdirectories that works easily and trivially.
So while you can indeed perform any file operation on that dfd who cares because none of those operations can get you anywhere you aren't supposed to be.
My point was that you can as granular as you would like by binding a dfd to a mount namespace instead of binding a process to a mount namespace, and the code already exists and is being maintained.
So while things are not packaged in the form that has been requested it looks to me as if the functionality for directories already exists within the Linux kernel.
Eric
Andy Lutomirski
reviewerQuoted 32 lines · Eric W. Biederman
I have cut this down to just focus on
O_BENEATHopenat case.Quoted 9 lines · David Drysdale
Quoted 5 lines · Eric W. Biederman
Nope. What you can implement today if you want fine grained limitations like this is to create a mount namespace with exactly the subdirectory tree you want to allow access to and to return a file descriptor that points into that mount namespace. (When complete the only user of that mount namespace would be your file descriptor).
Quoted 4 lines
How does that solve the particular example I mentioned? The DFD within the mount namespace will still allow any operation on any file that's already in the subdirectory -- or am I misunderstanding something?
The goal was to bound the DFD to the directory and all of it's subdirectories such that
openat(dfd, "../../..")would open the dfd, and that further opens of other directories would also not allow you to escape.Since the mount namespace only contains the choosen directory and it's subdirectories that works easily and trivially.
So while you can indeed perform any file operation on that dfd who cares because none of those operations can get you anywhere you aren't supposed to be.
Quoted 7 lines
My point was that you can as granular as you would like by binding a dfd to a mount namespace instead of binding a process to a mount namespace, and the code already exists and is being maintained.
So while things are not packaged in the form that has been requested it looks to me as if the functionality for directories already exists within the Linux kernel.
I think this would be amazingly expensive -- every constrained fd would need to carry an entire mount namespace with it. That namespace might need to have shared recursive mounts under it. And dfds created for subdirectories would need yet another mount namespace. And all these mount namespaces would probably need user namespaces to go along with them.
It would also have odd semantics. If you have a dfd pointing to /foo, and /foo/link is a symlink to "../bar", then looking up "link" relative to /foo should fail; it should not try to resolve /foo/bar.
IOW I think this is impractical.
Quoted 2 lines
Eric