Skip to content

Commit 6e52a9f

Browse files
legionusebiederm
authored andcommitted
Reimplement RLIMIT_MSGQUEUE on top of ucounts
The rlimit counter is tied to uid in the user_namespace. This allows rlimit values to be specified in userns even if they are already globally exceeded by the user. However, the value of the previous user_namespaces cannot be exceeded. Signed-off-by: Alexey Gladkov <[email protected]> Link: https://lkml.kernel.org/r/2531f42f7884bbfee56a978040b3e0d25cdf6cde.1619094428.git.legion@kernel.org Signed-off-by: Eric W. Biederman <[email protected]>
1 parent 21d1c5e commit 6e52a9f

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

include/linux/sched/user.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ struct user_struct {
1818
#endif
1919
#ifdef CONFIG_EPOLL
2020
atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
21-
#endif
22-
#ifdef CONFIG_POSIX_MQUEUE
23-
/* protected by mq_lock */
24-
unsigned long mq_bytes; /* How many bytes can be allocated to mqueue? */
2521
#endif
2622
unsigned long locked_shm; /* How many pages of mlocked shm ? */
2723
unsigned long unix_inflight; /* How many files in flight in unix sockets */

include/linux/user_namespace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ enum ucount_type {
5151
UCOUNT_INOTIFY_WATCHES,
5252
#endif
5353
UCOUNT_RLIMIT_NPROC,
54+
UCOUNT_RLIMIT_MSGQUEUE,
5455
UCOUNT_COUNTS,
5556
};
5657

ipc/mqueue.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct mqueue_inode_info {
144144
struct pid *notify_owner;
145145
u32 notify_self_exec_id;
146146
struct user_namespace *notify_user_ns;
147-
struct user_struct *user; /* user who created, for accounting */
147+
struct ucounts *ucounts; /* user who created, for accounting */
148148
struct sock *notify_sock;
149149
struct sk_buff *notify_cookie;
150150

@@ -292,7 +292,6 @@ static struct inode *mqueue_get_inode(struct super_block *sb,
292292
struct ipc_namespace *ipc_ns, umode_t mode,
293293
struct mq_attr *attr)
294294
{
295-
struct user_struct *u = current_user();
296295
struct inode *inode;
297296
int ret = -ENOMEM;
298297

@@ -321,7 +320,7 @@ static struct inode *mqueue_get_inode(struct super_block *sb,
321320
info->notify_owner = NULL;
322321
info->notify_user_ns = NULL;
323322
info->qsize = 0;
324-
info->user = NULL; /* set when all is ok */
323+
info->ucounts = NULL; /* set when all is ok */
325324
info->msg_tree = RB_ROOT;
326325
info->msg_tree_rightmost = NULL;
327326
info->node_cache = NULL;
@@ -371,19 +370,23 @@ static struct inode *mqueue_get_inode(struct super_block *sb,
371370
if (mq_bytes + mq_treesize < mq_bytes)
372371
goto out_inode;
373372
mq_bytes += mq_treesize;
374-
spin_lock(&mq_lock);
375-
if (u->mq_bytes + mq_bytes < u->mq_bytes ||
376-
u->mq_bytes + mq_bytes > rlimit(RLIMIT_MSGQUEUE)) {
373+
info->ucounts = get_ucounts(current_ucounts());
374+
if (info->ucounts) {
375+
long msgqueue;
376+
377+
spin_lock(&mq_lock);
378+
msgqueue = inc_rlimit_ucounts(info->ucounts, UCOUNT_RLIMIT_MSGQUEUE, mq_bytes);
379+
if (msgqueue == LONG_MAX || msgqueue > rlimit(RLIMIT_MSGQUEUE)) {
380+
dec_rlimit_ucounts(info->ucounts, UCOUNT_RLIMIT_MSGQUEUE, mq_bytes);
381+
spin_unlock(&mq_lock);
382+
put_ucounts(info->ucounts);
383+
info->ucounts = NULL;
384+
/* mqueue_evict_inode() releases info->messages */
385+
ret = -EMFILE;
386+
goto out_inode;
387+
}
377388
spin_unlock(&mq_lock);
378-
/* mqueue_evict_inode() releases info->messages */
379-
ret = -EMFILE;
380-
goto out_inode;
381389
}
382-
u->mq_bytes += mq_bytes;
383-
spin_unlock(&mq_lock);
384-
385-
/* all is ok */
386-
info->user = get_uid(u);
387390
} else if (S_ISDIR(mode)) {
388391
inc_nlink(inode);
389392
/* Some things misbehave if size == 0 on a directory */
@@ -497,7 +500,6 @@ static void mqueue_free_inode(struct inode *inode)
497500
static void mqueue_evict_inode(struct inode *inode)
498501
{
499502
struct mqueue_inode_info *info;
500-
struct user_struct *user;
501503
struct ipc_namespace *ipc_ns;
502504
struct msg_msg *msg, *nmsg;
503505
LIST_HEAD(tmp_msg);
@@ -520,8 +522,7 @@ static void mqueue_evict_inode(struct inode *inode)
520522
free_msg(msg);
521523
}
522524

523-
user = info->user;
524-
if (user) {
525+
if (info->ucounts) {
525526
unsigned long mq_bytes, mq_treesize;
526527

527528
/* Total amount of bytes accounted for the mqueue */
@@ -533,7 +534,7 @@ static void mqueue_evict_inode(struct inode *inode)
533534
info->attr.mq_msgsize);
534535

535536
spin_lock(&mq_lock);
536-
user->mq_bytes -= mq_bytes;
537+
dec_rlimit_ucounts(info->ucounts, UCOUNT_RLIMIT_MSGQUEUE, mq_bytes);
537538
/*
538539
* get_ns_from_inode() ensures that the
539540
* (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
@@ -543,7 +544,8 @@ static void mqueue_evict_inode(struct inode *inode)
543544
if (ipc_ns)
544545
ipc_ns->mq_queues_count--;
545546
spin_unlock(&mq_lock);
546-
free_uid(user);
547+
put_ucounts(info->ucounts);
548+
info->ucounts = NULL;
547549
}
548550
if (ipc_ns)
549551
put_ipc_ns(ipc_ns);

kernel/fork.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ void __init fork_init(void)
823823
init_user_ns.ucount_max[i] = max_threads/2;
824824

825825
init_user_ns.ucount_max[UCOUNT_RLIMIT_NPROC] = task_rlimit(&init_task, RLIMIT_NPROC);
826+
init_user_ns.ucount_max[UCOUNT_RLIMIT_MSGQUEUE] = task_rlimit(&init_task, RLIMIT_MSGQUEUE);
826827

827828
#ifdef CONFIG_VMAP_STACK
828829
cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache",

kernel/ucount.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ static struct ctl_table user_table[] = {
8080
UCOUNT_ENTRY("max_inotify_instances"),
8181
UCOUNT_ENTRY("max_inotify_watches"),
8282
#endif
83+
{ },
8384
{ },
8485
{ }
8586
};

kernel/user_namespace.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ int create_user_ns(struct cred *new)
123123
ns->ucount_max[i] = INT_MAX;
124124
}
125125
ns->ucount_max[UCOUNT_RLIMIT_NPROC] = rlimit(RLIMIT_NPROC);
126+
ns->ucount_max[UCOUNT_RLIMIT_MSGQUEUE] = rlimit(RLIMIT_MSGQUEUE);
126127
ns->ucounts = ucounts;
127128

128129
/* Inherit USERNS_SETGROUPS_ALLOWED from our parent */

0 commit comments

Comments
 (0)