Re: [PATCH] ipc: add set_ownership() and permissions() callbacks for posix mqueue sysctl

From: Davidlohr Bueso
Date: Fri Jul 30 2021 - 11:09:37 EST


On 2021-07-28 20:06, cgel.zte@xxxxxxxxx wrote:
This patch adds a ctl_table_set per ipc namespace, and also the
set_ownership() and permissions() callbacks for the new ctl_table_root
for ipc mqueue syscgtls.
^^ sysctls

This makes sense to me, just some nits below.

Acked-by: Davidlohr Bueso <dbueso@xxxxxxx>


Signed-off-by: Ran Xiaokai <ran.xiaokai@xxxxxxxxxx>
---
...
+static int set_permissions(struct ctl_table_header *head,
+ struct ctl_table *table)
+{
+ struct ipc_namespace *ipc_ns =
+ container_of(head->set, struct ipc_namespace, mq_set);
+ struct user_namespace *user_ns = ipc_ns->user_ns;
+ int mode;
+
+ /* Allow users with CAP_SYS_RESOURCE unrestrained access */
+ if (ns_capable(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;

Please use curly braces for the else.

+ return (mode << 6) | (mode << 3) | mode;
+}
+
+static void set_ownership(struct ctl_table_header *head,
+ struct ctl_table *table,
+ kuid_t *uid, kgid_t *gid)
+{
+ struct ipc_namespace *ipc_ns =
+ container_of(head->set, struct ipc_namespace, mq_set);
+ struct user_namespace *user_ns = ipc_ns->user_ns;
+ kuid_t ns_root_uid;
+ kgid_t ns_root_gid;
+
+ ns_root_uid = make_kuid(user_ns, 0);
+ if (uid_valid(ns_root_uid))
+ *uid = ns_root_uid;
+
+ ns_root_gid = make_kgid(user_ns, 0);
+ if (gid_valid(ns_root_gid))
+ *gid = ns_root_gid;
+}

Could set_permissions() and set_ownership() be factored such that we can avoid duplicated code between ipc and net ns? Something like:

void set_permissions(struct ctl_table_header *head, struct ctl_table *table)
{
struct ipc_namespace *ipc_ns = container_of(head->set, struct ipc_namespace, mq_set);
set_permissions_common(ipc_ns->user_ns);
}

Thanks,
Davidlohr