[RFC PATCH v3 4/4] ipc: Allow to modify ipc/mq sysctls if CAP_SYS_RESOURCE is present
From: Alexey Gladkov
Date: Fri Jan 21 2022 - 08:10:28 EST
The mq_overview(7) says that mq sysctls are available for modification
to a privileged process (CAP_SYS_RESOURCE). Right now, within userns, a
privileged process cannot modify these files. Once the mq and ipc
sysctls have been moved to the ipc namespace we can grant access to
these files.
mqueue sysctls
--------------
The mq sysctls are protected by an upper limit that cannot be exceeded
on the system:
For /proc/sys/fs/mqueue/msg_max the upper limit is HARD_MSGMAX.
For /proc/sys/fs/mqueue/msgsize_max the upper limit is HARD_MSGSIZEMAX.
Also RLIMIT_MSGQUEUE limits all queues used by the process. This limit
is also tied to userns.
ipc sysctls
-----------
The implementation has no specific limits for the per-process maximum
number of shared memory segments. Only SHM_LOCK and SHM_HUGETLB limited
by RLIMIT_MEMLOCK which is also tied to userns.
This patch is RPC only and should not be applied without a security
discussion.
Signed-off-by: Alexey Gladkov <legion@xxxxxxxxxx>
---
ipc/ipc_sysctl.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c
index 9fc8e3e75be7..f1d1c83656f9 100644
--- a/ipc/ipc_sysctl.c
+++ b/ipc/ipc_sysctl.c
@@ -254,6 +254,21 @@ static struct ctl_table mq_sysctls[] = {
{}
};
+static int set_permissions(struct ctl_table_header *head, struct ctl_table *table)
+{
+ struct ipc_namespace *ns = container_of(head->set, struct ipc_namespace, set);
+ int mode;
+
+ /* Allow users with CAP_SYS_RESOURCE unrestrained access */
+ if (ns_capable(ns->user_ns, CAP_SYS_RESOURCE))
+ mode = (table->mode & S_IRWXU) >> 6;
+ else
+ /* Allow all others at most read-only access */
+ mode = table->mode & S_IROTH;
+
+ return (mode << 6) | (mode << 3) | mode;
+}
+
static struct ctl_table_set *set_lookup(struct ctl_table_root *root)
{
return ¤t->nsproxy->ipc_ns->set;
@@ -266,6 +281,7 @@ static int set_is_seen(struct ctl_table_set *set)
static struct ctl_table_root set_root = {
.lookup = set_lookup,
+ .permissions = set_permissions,
};
static bool register_ipc_sysctl_table(struct ipc_namespace *ns)
--
2.33.0