Rootless containers are not allowed to modify kernel IPC parameters such
as kernel.msgmnb.
It seems to me that we can allow customization of these parameters if
the user has CAP_SYS_RESOURCE in that ipc namespace.
CAP_SYS_RESOURCE is already needed in order to overcome mqueue limits
(msg_max and msgsize_max).
Signed-off-by: Alexey Gladkov <[email protected]>
---
ipc/ipc_sysctl.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c
index ef313ecfb53a..e79452867720 100644
--- a/ipc/ipc_sysctl.c
+++ b/ipc/ipc_sysctl.c
@@ -193,16 +193,19 @@ static int set_is_seen(struct ctl_table_set *set)
static int ipc_permissions(struct ctl_table_header *head, struct ctl_table *table)
{
int mode = table->mode;
-
-#ifdef CONFIG_CHECKPOINT_RESTORE
struct ipc_namespace *ns = current->nsproxy->ipc_ns;
+#ifdef CONFIG_CHECKPOINT_RESTORE
if (((table->data == &ns->ids[IPC_SEM_IDS].next_id) ||
(table->data == &ns->ids[IPC_MSG_IDS].next_id) ||
(table->data == &ns->ids[IPC_SHM_IDS].next_id)) &&
checkpoint_restore_ns_capable(ns->user_ns))
mode = 0666;
+ else
#endif
+ if (ns_capable(ns->user_ns, CAP_SYS_RESOURCE))
+ mode = 0666;
+
return mode;
}
--
2.33.3
Alexey Gladkov <[email protected]> writes:
> Rootless containers are not allowed to modify kernel IPC parameters such
> as kernel.msgmnb.
>
> It seems to me that we can allow customization of these parameters if
> the user has CAP_SYS_RESOURCE in that ipc namespace.
>
> CAP_SYS_RESOURCE is already needed in order to overcome mqueue limits
> (msg_max and msgsize_max).
For changing the permissions on who can modify the SysV limits, I don't
think this change is safe. I don't see anything that will prevent abuse
if anyone can modify these limits. Replacing the ordinary unix DAC
permission check with ns_capable will allow anyone to modify the limits.
That said there is RLIMIT_MSGQUEUE that limits the posix messages queues
so those should be safe to allow anyone to modify their limits.
The code in mqueue_get_inode is where that limiting happens.
For the posix message queues all that should be needed is to change the
owner of the sysctl files from the global root to the user namespace
root. There are also two capable calls in ipc/mqueue.c that can
probably be changed to ns_capable calls.
The only posix message queue limit that I don't immediately see
something that will prevent abuse of is /proc/sys/fs/mqueue/queus_max.
That probably still runs into RLIMIT_MSGQUEUE somewhere but it was
not immediately obvious at first glance.
Eric
>
> Signed-off-by: Alexey Gladkov <[email protected]>
> ---
> ipc/ipc_sysctl.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c
> index ef313ecfb53a..e79452867720 100644
> --- a/ipc/ipc_sysctl.c
> +++ b/ipc/ipc_sysctl.c
> @@ -193,16 +193,19 @@ static int set_is_seen(struct ctl_table_set *set)
> static int ipc_permissions(struct ctl_table_header *head, struct ctl_table *table)
> {
> int mode = table->mode;
> -
> -#ifdef CONFIG_CHECKPOINT_RESTORE
> struct ipc_namespace *ns = current->nsproxy->ipc_ns;
>
> +#ifdef CONFIG_CHECKPOINT_RESTORE
> if (((table->data == &ns->ids[IPC_SEM_IDS].next_id) ||
> (table->data == &ns->ids[IPC_MSG_IDS].next_id) ||
> (table->data == &ns->ids[IPC_SHM_IDS].next_id)) &&
> checkpoint_restore_ns_capable(ns->user_ns))
> mode = 0666;
> + else
> #endif
> + if (ns_capable(ns->user_ns, CAP_SYS_RESOURCE))
> + mode = 0666;
> +
> return mode;
> }
On Mon, Jul 25, 2022 at 11:16:07AM -0500, Eric W. Biederman wrote:
> Alexey Gladkov <[email protected]> writes:
>
> > Rootless containers are not allowed to modify kernel IPC parameters such
> > as kernel.msgmnb.
> >
> > It seems to me that we can allow customization of these parameters if
> > the user has CAP_SYS_RESOURCE in that ipc namespace.
> >
> > CAP_SYS_RESOURCE is already needed in order to overcome mqueue limits
> > (msg_max and msgsize_max).
>
>
> For changing the permissions on who can modify the SysV limits, I don't
> think this change is safe. I don't see anything that will prevent abuse
> if anyone can modify these limits. Replacing the ordinary unix DAC
> permission check with ns_capable will allow anyone to modify the limits.
All limits are set to almost maximum values - ULONG_MAX. Limit values
are not inherited and are counted in the each ipc namespace (shm_tot is
not global and is located in ipc_ns). In fact, limits are disabled by
default. They can only be reduced.
> That said there is RLIMIT_MSGQUEUE that limits the posix messages queues
> so those should be safe to allow anyone to modify their limits.
>
> The code in mqueue_get_inode is where that limiting happens.
>
> For the posix message queues all that should be needed is to change the
> owner of the sysctl files from the global root to the user namespace
> root. There are also two capable calls in ipc/mqueue.c that can
> probably be changed to ns_capable calls.
>
>
> The only posix message queue limit that I don't immediately see
> something that will prevent abuse of is /proc/sys/fs/mqueue/queus_max.
> That probably still runs into RLIMIT_MSGQUEUE somewhere but it was
> not immediately obvious at first glance.
Everything always ends in mqueue_get_inode. In mqueue_create_attr we
check mq_queues_max and call mqueue_get_inode almost immediately.
I suggest allowing root in user namespace to change ipc namespace
limits.
--
Alexey Gladkov (3):
sysctl: Allow change system v ipc sysctls inside ipc namespace
sysctl: Allow to change limits for posix messages queues
docs: Add information about ipc sysctls limitations
Documentation/admin-guide/sysctl/kernel.rst | 14 ++++++--
ipc/ipc_sysctl.c | 34 ++++++++++++++++---
ipc/mq_sysctl.c | 36 +++++++++++++++++++++
3 files changed, 76 insertions(+), 8 deletions(-)
--
2.33.4
Rootless containers are not allowed to modify kernel IPC parameters.
All default limits are set to such high values that in fact there are no
limits at all. All limits are not inherited and are initialized to
default values when a new ipc_namespace is created.
For new ipc_namespace:
size_t ipc_ns.shm_ctlmax = SHMMAX; // (ULONG_MAX - (1UL << 24))
size_t ipc_ns.shm_ctlall = SHMALL; // (ULONG_MAX - (1UL << 24))
int ipc_ns.shm_ctlmni = IPCMNI; // (1 << 15)
int ipc_ns.shm_rmid_forced = 0;
unsigned int ipc_ns.msg_ctlmax = MSGMAX; // 8192
unsigned int ipc_ns.msg_ctlmni = MSGMNI; // 32000
unsigned int ipc_ns.msg_ctlmnb = MSGMNB; // 16384
The shm_tot (total amount of shared pages) has also ceased to be
global, it is located in ipc_namespace and is not inherited from
anywhere.
In such conditions, it cannot be said that these limits limit anything.
The real limiter for them is cgroups.
If we allow rootless containers to change these parameters, then it can
only be reduced.
Signed-off-by: Alexey Gladkov <[email protected]>
---
ipc/ipc_sysctl.c | 34 +++++++++++++++++++++++++++++-----
1 file changed, 29 insertions(+), 5 deletions(-)
diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c
index ef313ecfb53a..87eb1b1e42fa 100644
--- a/ipc/ipc_sysctl.c
+++ b/ipc/ipc_sysctl.c
@@ -192,23 +192,47 @@ static int set_is_seen(struct ctl_table_set *set)
static int ipc_permissions(struct ctl_table_header *head, struct ctl_table *table)
{
- int mode = table->mode;
-
-#ifdef CONFIG_CHECKPOINT_RESTORE
struct ipc_namespace *ns = current->nsproxy->ipc_ns;
+#ifdef CONFIG_CHECKPOINT_RESTORE
if (((table->data == &ns->ids[IPC_SEM_IDS].next_id) ||
(table->data == &ns->ids[IPC_MSG_IDS].next_id) ||
(table->data == &ns->ids[IPC_SHM_IDS].next_id)) &&
checkpoint_restore_ns_capable(ns->user_ns))
- mode = 0666;
+ return 0666;
#endif
- return mode;
+ if (ns->user_ns != &init_user_ns) {
+ kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
+ kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
+
+ if (uid_valid(ns_root_uid) && uid_eq(current_euid(), ns_root_uid))
+ return table->mode >> 6;
+
+ if (gid_valid(ns_root_gid) && in_egroup_p(ns_root_gid))
+ return table->mode >> 3;
+ }
+
+ return table->mode;
+}
+
+static void ipc_set_ownership(struct ctl_table_header *head,
+ struct ctl_table *table,
+ kuid_t *uid, kgid_t *gid)
+{
+ struct ipc_namespace *ns =
+ container_of(head->set, struct ipc_namespace, ipc_set);
+
+ kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
+ kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
+
+ *uid = uid_valid(ns_root_uid) ? ns_root_uid : GLOBAL_ROOT_UID;
+ *gid = gid_valid(ns_root_gid) ? ns_root_gid : GLOBAL_ROOT_GID;
}
static struct ctl_table_root set_root = {
.lookup = set_lookup,
.permissions = ipc_permissions,
+ .set_ownership = ipc_set_ownership,
};
bool setup_ipc_sysctls(struct ipc_namespace *ns)
--
2.33.4
After 25b21cb2f6d6 ("[PATCH] IPC namespace core") and 4e9823111bdc
("[PATCH] IPC namespace - shm") the shared memory page count stopped
being global and started counting per ipc namespace. The documentation
and shmget(2) still says that shmall is a global option.
shmget(2):
SHMALL System-wide limit on the total amount of shared memory, measured
in units of the system page size. On Linux, this limit can be read and
modified via /proc/sys/kernel/shmall.
I think the changes made in 2006 should be documented.
Signed-off-by: Alexey Gladkov <[email protected]>
---
Documentation/admin-guide/sysctl/kernel.rst | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/admin-guide/sysctl/kernel.rst
index ddccd1077462..9ad344b5e7a1 100644
--- a/Documentation/admin-guide/sysctl/kernel.rst
+++ b/Documentation/admin-guide/sysctl/kernel.rst
@@ -541,6 +541,9 @@ default (``MSGMNB``).
``msgmni`` is the maximum number of IPC queues. 32000 by default
(``MSGMNI``).
+All of these parameters are set per ipc namespace. The maximum number of bytes
+in POSIX message queues is limited by ``RLIMIT_MSGQUEUE``. This limit is
+respected hierarchically in the each user namespace.
msg_next_id, sem_next_id, and shm_next_id (System V IPC)
========================================================
@@ -1169,15 +1172,20 @@ are doing anyway :)
shmall
======
-This parameter sets the total amount of shared memory pages that
-can be used system wide. Hence, ``shmall`` should always be at least
-``ceil(shmmax/PAGE_SIZE)``.
+This parameter sets the total amount of shared memory pages that can be used
+inside ipc namespace. The shared memory pages counting occurs for each ipc
+namespace separately and is not inherited. Hence, ``shmall`` should always be at
+least ``ceil(shmmax/PAGE_SIZE)``.
If you are not sure what the default ``PAGE_SIZE`` is on your Linux
system, you can run the following command::
# getconf PAGE_SIZE
+To reduce or disable the ability to allocate shared memory, you must create a
+new ipc namespace, set this parameter to the required value and prohibit the
+creation of a new ipc namespace in the current user namespace or cgroups can
+be used.
shmmax
======
--
2.33.4
All parameters of posix messages queues (queues_max/msg_max/msgsize_max)
end up being limited by RLIMIT_MSGQUEUE. The code in mqueue_get_inode is
where that limiting happens.
The RLIMIT_MSGQUEUE is bound to the user namespace and is counted
hierarchically.
We can allow root in the user namespace to modify the posix messages
queues parameters.
Signed-off-by: Alexey Gladkov <[email protected]>
---
ipc/mq_sysctl.c | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/ipc/mq_sysctl.c b/ipc/mq_sysctl.c
index fbf6a8b93a26..39dcf086b7c2 100644
--- a/ipc/mq_sysctl.c
+++ b/ipc/mq_sysctl.c
@@ -12,6 +12,7 @@
#include <linux/stat.h>
#include <linux/capability.h>
#include <linux/slab.h>
+#include <linux/cred.h>
static int msg_max_limit_min = MIN_MSGMAX;
static int msg_max_limit_max = HARD_MSGMAX;
@@ -76,8 +77,43 @@ static int set_is_seen(struct ctl_table_set *set)
return ¤t->nsproxy->ipc_ns->mq_set == set;
}
+static int mq_permissions(struct ctl_table_header *head, struct ctl_table *table)
+{
+ struct ipc_namespace *ns =
+ container_of(head->set, struct ipc_namespace, mq_set);
+
+ if (ns->user_ns != &init_user_ns) {
+ kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
+ kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
+
+ if (uid_valid(ns_root_uid) && uid_eq(current_euid(), ns_root_uid))
+ return table->mode >> 6;
+
+ if (gid_valid(ns_root_gid) && in_egroup_p(ns_root_gid))
+ return table->mode >> 3;
+ }
+
+ return table->mode;
+}
+
+static void mq_set_ownership(struct ctl_table_header *head,
+ struct ctl_table *table,
+ kuid_t *uid, kgid_t *gid)
+{
+ struct ipc_namespace *ns =
+ container_of(head->set, struct ipc_namespace, mq_set);
+
+ kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
+ kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
+
+ *uid = uid_valid(ns_root_uid) ? ns_root_uid : GLOBAL_ROOT_UID;
+ *gid = gid_valid(ns_root_gid) ? ns_root_gid : GLOBAL_ROOT_GID;
+}
+
static struct ctl_table_root set_root = {
.lookup = set_lookup,
+ .permissions = mq_permissions,
+ .set_ownership = mq_set_ownership,
};
bool setup_mq_sysctls(struct ipc_namespace *ns)
--
2.33.4
Alexey Gladkov <[email protected]> writes:
> After 25b21cb2f6d6 ("[PATCH] IPC namespace core") and 4e9823111bdc
> ("[PATCH] IPC namespace - shm") the shared memory page count stopped
> being global and started counting per ipc namespace. The documentation
> and shmget(2) still says that shmall is a global option.
>
> shmget(2):
>
> SHMALL System-wide limit on the total amount of shared memory, measured
> in units of the system page size. On Linux, this limit can be read and
> modified via /proc/sys/kernel/shmall.
>
> I think the changes made in 2006 should be documented.
Agreed. Documenting these limits only apply to their ipc namespace is
overdue.
Acked-by: "Eric W. Biederman" <[email protected]>
> Signed-off-by: Alexey Gladkov <[email protected]>
> ---
> Documentation/admin-guide/sysctl/kernel.rst | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/admin-guide/sysctl/kernel.rst
> index ddccd1077462..9ad344b5e7a1 100644
> --- a/Documentation/admin-guide/sysctl/kernel.rst
> +++ b/Documentation/admin-guide/sysctl/kernel.rst
> @@ -541,6 +541,9 @@ default (``MSGMNB``).
> ``msgmni`` is the maximum number of IPC queues. 32000 by default
> (``MSGMNI``).
>
> +All of these parameters are set per ipc namespace. The maximum number of bytes
> +in POSIX message queues is limited by ``RLIMIT_MSGQUEUE``. This limit is
> +respected hierarchically in the each user namespace.
>
> msg_next_id, sem_next_id, and shm_next_id (System V IPC)
> ========================================================
> @@ -1169,15 +1172,20 @@ are doing anyway :)
> shmall
> ======
>
> -This parameter sets the total amount of shared memory pages that
> -can be used system wide. Hence, ``shmall`` should always be at least
> -``ceil(shmmax/PAGE_SIZE)``.
> +This parameter sets the total amount of shared memory pages that can be used
> +inside ipc namespace. The shared memory pages counting occurs for each ipc
> +namespace separately and is not inherited. Hence, ``shmall`` should always be at
> +least ``ceil(shmmax/PAGE_SIZE)``.
>
> If you are not sure what the default ``PAGE_SIZE`` is on your Linux
> system, you can run the following command::
>
> # getconf PAGE_SIZE
>
> +To reduce or disable the ability to allocate shared memory, you must create a
> +new ipc namespace, set this parameter to the required value and prohibit the
> +creation of a new ipc namespace in the current user namespace or cgroups can
> +be used.
>
> shmmax
> ======
Alexey Gladkov <[email protected]> writes:
> All parameters of posix messages queues (queues_max/msg_max/msgsize_max)
> end up being limited by RLIMIT_MSGQUEUE. The code in mqueue_get_inode is
> where that limiting happens.
>
> The RLIMIT_MSGQUEUE is bound to the user namespace and is counted
> hierarchically.
>
> We can allow root in the user namespace to modify the posix messages
> queues parameters.
This looks good from 10,000 feet. But the same nits with setting
mode in mq_permissions as in ipc_set_permissions in your other patch.
Eric
> Signed-off-by: Alexey Gladkov <[email protected]>
> ---
> ipc/mq_sysctl.c | 36 ++++++++++++++++++++++++++++++++++++
> 1 file changed, 36 insertions(+)
>
> diff --git a/ipc/mq_sysctl.c b/ipc/mq_sysctl.c
> index fbf6a8b93a26..39dcf086b7c2 100644
> --- a/ipc/mq_sysctl.c
> +++ b/ipc/mq_sysctl.c
> @@ -12,6 +12,7 @@
> #include <linux/stat.h>
> #include <linux/capability.h>
> #include <linux/slab.h>
> +#include <linux/cred.h>
>
> static int msg_max_limit_min = MIN_MSGMAX;
> static int msg_max_limit_max = HARD_MSGMAX;
> @@ -76,8 +77,43 @@ static int set_is_seen(struct ctl_table_set *set)
> return ¤t->nsproxy->ipc_ns->mq_set == set;
> }
>
> +static int mq_permissions(struct ctl_table_header *head, struct ctl_table *table)
> +{
> + struct ipc_namespace *ns =
> + container_of(head->set, struct ipc_namespace, mq_set);
> +
> + if (ns->user_ns != &init_user_ns) {
> + kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
> + kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
> +
> + if (uid_valid(ns_root_uid) && uid_eq(current_euid(), ns_root_uid))
> + return table->mode >> 6;
> +
> + if (gid_valid(ns_root_gid) && in_egroup_p(ns_root_gid))
> + return table->mode >> 3;
> + }
> +
> + return table->mode;
> +}
> +
> +static void mq_set_ownership(struct ctl_table_header *head,
> + struct ctl_table *table,
> + kuid_t *uid, kgid_t *gid)
> +{
> + struct ipc_namespace *ns =
> + container_of(head->set, struct ipc_namespace, mq_set);
> +
> + kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
> + kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
> +
> + *uid = uid_valid(ns_root_uid) ? ns_root_uid : GLOBAL_ROOT_UID;
> + *gid = gid_valid(ns_root_gid) ? ns_root_gid : GLOBAL_ROOT_GID;
> +}
> +
> static struct ctl_table_root set_root = {
> .lookup = set_lookup,
> + .permissions = mq_permissions,
> + .set_ownership = mq_set_ownership,
> };
>
> bool setup_mq_sysctls(struct ipc_namespace *ns)
Alexey Gladkov <[email protected]> writes:
> Rootless containers are not allowed to modify kernel IPC parameters.
>
> All default limits are set to such high values that in fact there are no
> limits at all. All limits are not inherited and are initialized to
> default values when a new ipc_namespace is created.
>
> For new ipc_namespace:
>
> size_t ipc_ns.shm_ctlmax = SHMMAX; // (ULONG_MAX - (1UL << 24))
> size_t ipc_ns.shm_ctlall = SHMALL; // (ULONG_MAX - (1UL << 24))
> int ipc_ns.shm_ctlmni = IPCMNI; // (1 << 15)
> int ipc_ns.shm_rmid_forced = 0;
> unsigned int ipc_ns.msg_ctlmax = MSGMAX; // 8192
> unsigned int ipc_ns.msg_ctlmni = MSGMNI; // 32000
> unsigned int ipc_ns.msg_ctlmnb = MSGMNB; // 16384
>
> The shm_tot (total amount of shared pages) has also ceased to be
> global, it is located in ipc_namespace and is not inherited from
> anywhere.
>
> In such conditions, it cannot be said that these limits limit anything.
> The real limiter for them is cgroups.
>
> If we allow rootless containers to change these parameters, then it can
> only be reduced.
Manfred does that analysis sound correct to you?
Do you have any concerns about allowing the users who create the ipc
namespace to be able to set it's limits?
At a quick skim through everything Alex's analysis above appears
correct to me.
From 10,000 feet this patch looks good. I do see a couple of nits
that should be fixed before we merge this into Linus's tree.
> Signed-off-by: Alexey Gladkov <[email protected]>
> ---
> ipc/ipc_sysctl.c | 34 +++++++++++++++++++++++++++++-----
> 1 file changed, 29 insertions(+), 5 deletions(-)
>
> diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c
> index ef313ecfb53a..87eb1b1e42fa 100644
> --- a/ipc/ipc_sysctl.c
> +++ b/ipc/ipc_sysctl.c
> @@ -192,23 +192,47 @@ static int set_is_seen(struct ctl_table_set *set)
>
> static int ipc_permissions(struct ctl_table_header *head, struct ctl_table *table)
> {
> - int mode = table->mode;
> -
> -#ifdef CONFIG_CHECKPOINT_RESTORE
> struct ipc_namespace *ns = current->nsproxy->ipc_ns;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Historically that was the best we could do. But now that we have
an ipc_set member in struct ipc_namespace you can use container_of
to compute this value.
For a permission check that is much safer.
> +#ifdef CONFIG_CHECKPOINT_RESTORE
> if (((table->data == &ns->ids[IPC_SEM_IDS].next_id) ||
> (table->data == &ns->ids[IPC_MSG_IDS].next_id) ||
> (table->data == &ns->ids[IPC_SHM_IDS].next_id)) &&
> checkpoint_restore_ns_capable(ns->user_ns))
> - mode = 0666;
> + return 0666;
> #endif
> - return mode;
> + if (ns->user_ns != &init_user_ns) {
> + kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
> + kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
> +
> + if (uid_valid(ns_root_uid) && uid_eq(current_euid(), ns_root_uid))
> + return table->mode >> 6;
> +
> + if (gid_valid(ns_root_gid) && in_egroup_p(ns_root_gid))
> + return table->mode >> 3;
From 10,000 fee this is fine. But this has to interact with
test_perm in proc_systl.c. So can you please do what
net_ctl_permissions does and replicate the chosen mode all through
the mode line.
Perhaps something like:
kuid_t ns_root_uid;
kgid_t ns_root_gid
ipc_set_ownership(head, table, &ns_root_uid, &ns_root_gid);
#ifdef CONFIG_CHECKPOINT_RESTORE
if (...)
mode = 0666;
else
#endif
if (uid_eq(current_euid(), ns_root_uid))
mode >>= 6;
else if (uid_eq(in_group_p(ns_root_gid))
mode >>= 3;
mode &= 7;
mode = (mode << 6) | (mode << 3) | mode;
return mode;
If we always pass through the same logic there is the advantage that we
will always test it, and there is less room for bugs to slip through.
I added a couple of unnecessary simplifications in there that I just
saw as I was writing my example code.
Eric
> + }
> +
> + return table->mode;
> +}
> +
> +static void ipc_set_ownership(struct ctl_table_header *head,
> + struct ctl_table *table,
> + kuid_t *uid, kgid_t *gid)
> +{
> + struct ipc_namespace *ns =
> + container_of(head->set, struct ipc_namespace, ipc_set);
> +
> + kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
> + kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
> +
> + *uid = uid_valid(ns_root_uid) ? ns_root_uid : GLOBAL_ROOT_UID;
> + *gid = gid_valid(ns_root_gid) ? ns_root_gid : GLOBAL_ROOT_GID;
> }
>
> static struct ctl_table_root set_root = {
> .lookup = set_lookup,
> .permissions = ipc_permissions,
> + .set_ownership = ipc_set_ownership,
> };
>
> bool setup_ipc_sysctls(struct ipc_namespace *ns)
I can't see anything wrong with your proposed ipc_set_ownership.
Eric
On Mon, Sep 19, 2022 at 10:26:39AM -0500, Eric W. Biederman wrote:
> >
> > diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c
> > index ef313ecfb53a..87eb1b1e42fa 100644
> > --- a/ipc/ipc_sysctl.c
> > +++ b/ipc/ipc_sysctl.c
> > @@ -192,23 +192,47 @@ static int set_is_seen(struct ctl_table_set *set)
> >
> > static int ipc_permissions(struct ctl_table_header *head, struct ctl_table *table)
> > {
> > - int mode = table->mode;
> > -
> > -#ifdef CONFIG_CHECKPOINT_RESTORE
> > struct ipc_namespace *ns = current->nsproxy->ipc_ns;
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Historically that was the best we could do. But now that we have
> an ipc_set member in struct ipc_namespace you can use container_of
> to compute this value.
>
> For a permission check that is much safer.
Yes. It make sense.
> > +#ifdef CONFIG_CHECKPOINT_RESTORE
> > if (((table->data == &ns->ids[IPC_SEM_IDS].next_id) ||
> > (table->data == &ns->ids[IPC_MSG_IDS].next_id) ||
> > (table->data == &ns->ids[IPC_SHM_IDS].next_id)) &&
> > checkpoint_restore_ns_capable(ns->user_ns))
> > - mode = 0666;
> > + return 0666;
> > #endif
> > - return mode;
> > + if (ns->user_ns != &init_user_ns) {
> > + kuid_t ns_root_uid = make_kuid(ns->user_ns, 0);
> > + kgid_t ns_root_gid = make_kgid(ns->user_ns, 0);
> > +
> > + if (uid_valid(ns_root_uid) && uid_eq(current_euid(), ns_root_uid))
> > + return table->mode >> 6;
> > +
> > + if (gid_valid(ns_root_gid) && in_egroup_p(ns_root_gid))
> > + return table->mode >> 3;
>
> >From 10,000 fee this is fine. But this has to interact with
> test_perm in proc_systl.c. So can you please do what
> net_ctl_permissions does and replicate the chosen mode all through
> the mode line.
>
> Perhaps something like:
>
> kuid_t ns_root_uid;
> kgid_t ns_root_gid
>
> ipc_set_ownership(head, table, &ns_root_uid, &ns_root_gid);
>
> #ifdef CONFIG_CHECKPOINT_RESTORE
> if (...)
> mode = 0666;
> else
> #endif
> if (uid_eq(current_euid(), ns_root_uid))
> mode >>= 6;
>
> else if (uid_eq(in_group_p(ns_root_gid))
> mode >>= 3;
>
> mode &= 7;
> mode = (mode << 6) | (mode << 3) | mode;
> return mode;
>
>
> If we always pass through the same logic there is the advantage that we
> will always test it, and there is less room for bugs to slip through.
>
> I added a couple of unnecessary simplifications in there that I just
> saw as I was writing my example code.
Thanks! It looks better. I'll fix it and send a new version.
--
Rgrds, legion
Right now ipc and mq limits count as per ipc namespace, but only real
root can change them. By default, the current values of these limits are
such that it can only be reduced. Since only root can change the values,
it is impossible to reduce these limits in the rootless container.
We can allow limit changes within ipc namespace because mq parameters
are limited by RLIMIT_MSGQUEUE and ipc parameters are not limited to
anything other than cgroups.
--
Alexey Gladkov (3):
sysctl: Allow change system v ipc sysctls inside ipc namespace
sysctl: Allow to change limits for posix messages queues
docs: Add information about ipc sysctls limitations
Documentation/admin-guide/sysctl/kernel.rst | 14 ++++++--
ipc/ipc_sysctl.c | 34 ++++++++++++++++--
ipc/mq_sysctl.c | 38 +++++++++++++++++++++
3 files changed, 80 insertions(+), 6 deletions(-)
--
2.33.4