Skip to content

Commit b537900

Browse files
Daniel Colascionepcmoore
authored andcommitted
userfaultfd: use secure anon inodes for userfaultfd
This change gives userfaultfd file descriptors a real security context, allowing policy to act on them. Signed-off-by: Daniel Colascione <[email protected]> [LG: Remove owner inode from userfaultfd_ctx] [LG: Use anon_inode_getfd_secure() in userfaultfd syscall] [LG: Use inode of file in userfaultfd_read() in resolve_userfault_fork()] Signed-off-by: Lokesh Gidra <[email protected]> Reviewed-by: Eric Biggers <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 29cd659 commit b537900

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

fs/userfaultfd.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -979,14 +979,14 @@ static __poll_t userfaultfd_poll(struct file *file, poll_table *wait)
979979

980980
static const struct file_operations userfaultfd_fops;
981981

982-
static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
983-
struct userfaultfd_ctx *new,
982+
static int resolve_userfault_fork(struct userfaultfd_ctx *new,
983+
struct inode *inode,
984984
struct uffd_msg *msg)
985985
{
986986
int fd;
987987

988-
fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new,
989-
O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS));
988+
fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, new,
989+
O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS), inode);
990990
if (fd < 0)
991991
return fd;
992992

@@ -996,7 +996,7 @@ static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
996996
}
997997

998998
static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
999-
struct uffd_msg *msg)
999+
struct uffd_msg *msg, struct inode *inode)
10001000
{
10011001
ssize_t ret;
10021002
DECLARE_WAITQUEUE(wait, current);
@@ -1107,7 +1107,7 @@ static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
11071107
spin_unlock_irq(&ctx->fd_wqh.lock);
11081108

11091109
if (!ret && msg->event == UFFD_EVENT_FORK) {
1110-
ret = resolve_userfault_fork(ctx, fork_nctx, msg);
1110+
ret = resolve_userfault_fork(fork_nctx, inode, msg);
11111111
spin_lock_irq(&ctx->event_wqh.lock);
11121112
if (!list_empty(&fork_event)) {
11131113
/*
@@ -1167,14 +1167,15 @@ static ssize_t userfaultfd_read(struct file *file, char __user *buf,
11671167
ssize_t _ret, ret = 0;
11681168
struct uffd_msg msg;
11691169
int no_wait = file->f_flags & O_NONBLOCK;
1170+
struct inode *inode = file_inode(file);
11701171

11711172
if (ctx->state == UFFD_STATE_WAIT_API)
11721173
return -EINVAL;
11731174

11741175
for (;;) {
11751176
if (count < sizeof(msg))
11761177
return ret ? ret : -EINVAL;
1177-
_ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
1178+
_ret = userfaultfd_ctx_read(ctx, no_wait, &msg, inode);
11781179
if (_ret < 0)
11791180
return ret ? ret : _ret;
11801181
if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
@@ -1999,8 +2000,8 @@ SYSCALL_DEFINE1(userfaultfd, int, flags)
19992000
/* prevent the mm struct to be freed */
20002001
mmgrab(ctx->mm);
20012002

2002-
fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx,
2003-
O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
2003+
fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, ctx,
2004+
O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS), NULL);
20042005
if (fd < 0) {
20052006
mmdrop(ctx->mm);
20062007
kmem_cache_free(userfaultfd_ctx_cachep, ctx);

0 commit comments

Comments
 (0)