blob: ef313ecfb53a12e4f63be71d5b3f6a05afcb79de [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Eric W. Biedermana5494dc2007-02-14 00:34:06 -08002/*
3 * Copyright (C) 2007
4 *
5 * Author: Eric Biederman <ebiederm@xmision.com>
Eric W. Biedermana5494dc2007-02-14 00:34:06 -08006 */
7
8#include <linux/module.h>
9#include <linux/ipc.h>
10#include <linux/nsproxy.h>
11#include <linux/sysctl.h>
12#include <linux/uaccess.h>
Michal Clapinski5563cab2021-11-08 18:35:59 -080013#include <linux/capability.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080014#include <linux/ipc_namespace.h>
Nadia Derbey6546bc42008-04-29 01:00:45 -070015#include <linux/msg.h>
Alexey Gladkov1f5c1352022-02-14 19:18:15 +010016#include <linux/slab.h>
Nadia Derbey6546bc42008-04-29 01:00:45 -070017#include "util.h"
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080018
Joe Perchesa5c59282014-06-06 14:38:07 -070019static int proc_ipc_dointvec_minmax_orphans(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020020 void *buffer, size_t *lenp, loff_t *ppos)
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -070021{
Alexey Gladkovdd141a42022-05-03 15:39:55 +020022 struct ipc_namespace *ns =
23 container_of(table->data, struct ipc_namespace, shm_rmid_forced);
Alexey Gladkov1f5c1352022-02-14 19:18:15 +010024 int err;
25
Alexey Gladkovdd141a42022-05-03 15:39:55 +020026 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -070027
28 if (err < 0)
29 return err;
30 if (ns->shm_rmid_forced)
31 shm_destroy_orphaned(ns);
32 return err;
33}
34
Manfred Spraul0050ee02014-12-12 16:58:17 -080035static int proc_ipc_auto_msgmni(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020036 void *buffer, size_t *lenp, loff_t *ppos)
Nadia Derbey9eefe522008-07-25 01:48:08 -070037{
38 struct ctl_table ipc_table;
Manfred Spraul0050ee02014-12-12 16:58:17 -080039 int dummy = 0;
Nadia Derbey9eefe522008-07-25 01:48:08 -070040
41 memcpy(&ipc_table, table, sizeof(ipc_table));
Manfred Spraul0050ee02014-12-12 16:58:17 -080042 ipc_table.data = &dummy;
Nadia Derbey9eefe522008-07-25 01:48:08 -070043
Manfred Spraul0050ee02014-12-12 16:58:17 -080044 if (write)
45 pr_info_once("writing to auto_msgmni has no effect");
Nadia Derbey9eefe522008-07-25 01:48:08 -070046
Manfred Spraul0050ee02014-12-12 16:58:17 -080047 return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
Nadia Derbey9eefe522008-07-25 01:48:08 -070048}
49
Waiman Long8c81ddd2018-10-30 15:07:24 -070050static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,
Tobias Klauserfff16622020-09-04 16:35:46 -070051 void *buffer, size_t *lenp, loff_t *ppos)
Waiman Long8c81ddd2018-10-30 15:07:24 -070052{
Alexey Gladkovdd141a42022-05-03 15:39:55 +020053 struct ipc_namespace *ns =
54 container_of(table->data, struct ipc_namespace, sem_ctls);
Waiman Long8c81ddd2018-10-30 15:07:24 -070055 int ret, semmni;
Alexey Gladkov1f5c1352022-02-14 19:18:15 +010056
Waiman Long8c81ddd2018-10-30 15:07:24 -070057 semmni = ns->sem_ctls[3];
Alexey Gladkov1f5c1352022-02-14 19:18:15 +010058 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Waiman Long8c81ddd2018-10-30 15:07:24 -070059
60 if (!ret)
Alexey Gladkovdef73432022-05-03 15:39:54 +020061 ret = sem_check_semmni(ns);
Waiman Long8c81ddd2018-10-30 15:07:24 -070062
63 /*
64 * Reset the semmni value if an error happens.
65 */
66 if (ret)
67 ns->sem_ctls[3] = semmni;
68 return ret;
69}
70
Waiman Long5ac893b2019-05-14 15:46:29 -070071int ipc_mni = IPCMNI;
72int ipc_mni_shift = IPCMNI_SHIFT;
Manfred Spraul99db46e2019-05-14 15:46:36 -070073int ipc_min_cycle = RADIX_TREE_MAP_SIZE;
Nadia Derbey9eefe522008-07-25 01:48:08 -070074
Alexey Gladkov1f5c1352022-02-14 19:18:15 +010075static struct ctl_table ipc_sysctls[] = {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080076 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080077 .procname = "shmmax",
78 .data = &init_ipc_ns.shm_ctlmax,
Manfred Spraul239521f2014-01-27 17:07:04 -080079 .maxlen = sizeof(init_ipc_ns.shm_ctlmax),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080080 .mode = 0644,
Alexey Gladkov1f5c1352022-02-14 19:18:15 +010081 .proc_handler = proc_doulongvec_minmax,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080082 },
83 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080084 .procname = "shmall",
85 .data = &init_ipc_ns.shm_ctlall,
Manfred Spraul239521f2014-01-27 17:07:04 -080086 .maxlen = sizeof(init_ipc_ns.shm_ctlall),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080087 .mode = 0644,
Alexey Gladkov1f5c1352022-02-14 19:18:15 +010088 .proc_handler = proc_doulongvec_minmax,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080089 },
90 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080091 .procname = "shmmni",
92 .data = &init_ipc_ns.shm_ctlmni,
Manfred Spraul239521f2014-01-27 17:07:04 -080093 .maxlen = sizeof(init_ipc_ns.shm_ctlmni),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080094 .mode = 0644,
Alexey Gladkov1f5c1352022-02-14 19:18:15 +010095 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -070096 .extra1 = SYSCTL_ZERO,
Waiman Long6730e652018-10-30 15:07:20 -070097 .extra2 = &ipc_mni,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080098 },
99 {
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700100 .procname = "shm_rmid_forced",
101 .data = &init_ipc_ns.shm_rmid_forced,
102 .maxlen = sizeof(init_ipc_ns.shm_rmid_forced),
103 .mode = 0644,
104 .proc_handler = proc_ipc_dointvec_minmax_orphans,
Alexey Gladkovdd141a42022-05-03 15:39:55 +0200105 .extra1 = SYSCTL_ZERO,
106 .extra2 = SYSCTL_ONE,
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700107 },
108 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800109 .procname = "msgmax",
110 .data = &init_ipc_ns.msg_ctlmax,
Manfred Spraul239521f2014-01-27 17:07:04 -0800111 .maxlen = sizeof(init_ipc_ns.msg_ctlmax),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800112 .mode = 0644,
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100113 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700114 .extra1 = SYSCTL_ZERO,
115 .extra2 = SYSCTL_INT_MAX,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800116 },
117 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800118 .procname = "msgmni",
119 .data = &init_ipc_ns.msg_ctlmni,
Manfred Spraul239521f2014-01-27 17:07:04 -0800120 .maxlen = sizeof(init_ipc_ns.msg_ctlmni),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800121 .mode = 0644,
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100122 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700123 .extra1 = SYSCTL_ZERO,
Waiman Long6730e652018-10-30 15:07:20 -0700124 .extra2 = &ipc_mni,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800125 },
126 {
Manfred Spraul0050ee02014-12-12 16:58:17 -0800127 .procname = "auto_msgmni",
128 .data = NULL,
129 .maxlen = sizeof(int),
130 .mode = 0644,
131 .proc_handler = proc_ipc_auto_msgmni,
Matteo Croceeec48442019-07-18 15:58:50 -0700132 .extra1 = SYSCTL_ZERO,
133 .extra2 = SYSCTL_ONE,
Manfred Spraul0050ee02014-12-12 16:58:17 -0800134 },
135 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800136 .procname = "msgmnb",
137 .data = &init_ipc_ns.msg_ctlmnb,
Manfred Spraul239521f2014-01-27 17:07:04 -0800138 .maxlen = sizeof(init_ipc_ns.msg_ctlmnb),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800139 .mode = 0644,
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100140 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700141 .extra1 = SYSCTL_ZERO,
142 .extra2 = SYSCTL_INT_MAX,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800143 },
144 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800145 .procname = "sem",
146 .data = &init_ipc_ns.sem_ctls,
Manfred Spraul239521f2014-01-27 17:07:04 -0800147 .maxlen = 4*sizeof(int),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800148 .mode = 0644,
Waiman Long8c81ddd2018-10-30 15:07:24 -0700149 .proc_handler = proc_ipc_sem_dointvec,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800150 },
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800151#ifdef CONFIG_CHECKPOINT_RESTORE
152 {
153 .procname = "sem_next_id",
154 .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
155 .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
Alexey Gladkov0889f442022-05-03 15:39:56 +0200156 .mode = 0444,
157 .proc_handler = proc_dointvec_minmax,
158 .extra1 = SYSCTL_ZERO,
159 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800160 },
161 {
162 .procname = "msg_next_id",
163 .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
164 .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
Alexey Gladkov0889f442022-05-03 15:39:56 +0200165 .mode = 0444,
166 .proc_handler = proc_dointvec_minmax,
167 .extra1 = SYSCTL_ZERO,
168 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800169 },
170 {
171 .procname = "shm_next_id",
172 .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
173 .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
Alexey Gladkov0889f442022-05-03 15:39:56 +0200174 .mode = 0444,
175 .proc_handler = proc_dointvec_minmax,
176 .extra1 = SYSCTL_ZERO,
177 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800178 },
179#endif
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800180 {}
181};
182
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100183static struct ctl_table_set *set_lookup(struct ctl_table_root *root)
184{
185 return &current->nsproxy->ipc_ns->ipc_set;
186}
187
188static int set_is_seen(struct ctl_table_set *set)
189{
190 return &current->nsproxy->ipc_ns->ipc_set == set;
191}
192
Alexey Gladkov0889f442022-05-03 15:39:56 +0200193static int ipc_permissions(struct ctl_table_header *head, struct ctl_table *table)
194{
195 int mode = table->mode;
196
197#ifdef CONFIG_CHECKPOINT_RESTORE
198 struct ipc_namespace *ns = current->nsproxy->ipc_ns;
199
200 if (((table->data == &ns->ids[IPC_SEM_IDS].next_id) ||
201 (table->data == &ns->ids[IPC_MSG_IDS].next_id) ||
202 (table->data == &ns->ids[IPC_SHM_IDS].next_id)) &&
203 checkpoint_restore_ns_capable(ns->user_ns))
204 mode = 0666;
205#endif
206 return mode;
207}
208
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100209static struct ctl_table_root set_root = {
210 .lookup = set_lookup,
Alexey Gladkov0889f442022-05-03 15:39:56 +0200211 .permissions = ipc_permissions,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800212};
213
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100214bool setup_ipc_sysctls(struct ipc_namespace *ns)
215{
216 struct ctl_table *tbl;
217
218 setup_sysctl_set(&ns->ipc_set, &set_root, set_is_seen);
219
220 tbl = kmemdup(ipc_sysctls, sizeof(ipc_sysctls), GFP_KERNEL);
221 if (tbl) {
222 int i;
223
224 for (i = 0; i < ARRAY_SIZE(ipc_sysctls); i++) {
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200225 if (tbl[i].data == &init_ipc_ns.shm_ctlmax)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100226 tbl[i].data = &ns->shm_ctlmax;
227
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200228 else if (tbl[i].data == &init_ipc_ns.shm_ctlall)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100229 tbl[i].data = &ns->shm_ctlall;
230
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200231 else if (tbl[i].data == &init_ipc_ns.shm_ctlmni)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100232 tbl[i].data = &ns->shm_ctlmni;
233
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200234 else if (tbl[i].data == &init_ipc_ns.shm_rmid_forced)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100235 tbl[i].data = &ns->shm_rmid_forced;
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100236
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200237 else if (tbl[i].data == &init_ipc_ns.msg_ctlmax)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100238 tbl[i].data = &ns->msg_ctlmax;
239
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200240 else if (tbl[i].data == &init_ipc_ns.msg_ctlmni)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100241 tbl[i].data = &ns->msg_ctlmni;
242
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200243 else if (tbl[i].data == &init_ipc_ns.msg_ctlmnb)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100244 tbl[i].data = &ns->msg_ctlmnb;
245
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200246 else if (tbl[i].data == &init_ipc_ns.sem_ctls)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100247 tbl[i].data = &ns->sem_ctls;
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100248#ifdef CONFIG_CHECKPOINT_RESTORE
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200249 else if (tbl[i].data == &init_ipc_ns.ids[IPC_SEM_IDS].next_id)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100250 tbl[i].data = &ns->ids[IPC_SEM_IDS].next_id;
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100251
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200252 else if (tbl[i].data == &init_ipc_ns.ids[IPC_MSG_IDS].next_id)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100253 tbl[i].data = &ns->ids[IPC_MSG_IDS].next_id;
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100254
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200255 else if (tbl[i].data == &init_ipc_ns.ids[IPC_SHM_IDS].next_id)
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100256 tbl[i].data = &ns->ids[IPC_SHM_IDS].next_id;
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100257#endif
Alexey Gladkov38cd5b12022-05-03 15:39:57 +0200258 else
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100259 tbl[i].data = NULL;
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100260 }
261
262 ns->ipc_sysctls = __register_sysctl_table(&ns->ipc_set, "kernel", tbl);
263 }
264 if (!ns->ipc_sysctls) {
265 kfree(tbl);
266 retire_sysctl_set(&ns->ipc_set);
267 return false;
268 }
269
270 return true;
271}
272
273void retire_ipc_sysctls(struct ipc_namespace *ns)
274{
275 struct ctl_table *tbl;
276
277 tbl = ns->ipc_sysctls->ctl_table_arg;
278 unregister_sysctl_table(ns->ipc_sysctls);
279 retire_sysctl_set(&ns->ipc_set);
280 kfree(tbl);
281}
282
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800283static int __init ipc_sysctl_init(void)
284{
Alexey Gladkov1f5c1352022-02-14 19:18:15 +0100285 if (!setup_ipc_sysctls(&init_ipc_ns)) {
286 pr_warn("ipc sysctl registration failed\n");
287 return -ENOMEM;
288 }
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800289 return 0;
290}
291
Davidlohr Bueso6d08a252014-04-07 15:39:18 -0700292device_initcall(ipc_sysctl_init);
Waiman Long5ac893b2019-05-14 15:46:29 -0700293
294static int __init ipc_mni_extend(char *str)
295{
296 ipc_mni = IPCMNI_EXTEND;
297 ipc_mni_shift = IPCMNI_EXTEND_SHIFT;
Manfred Spraul99db46e2019-05-14 15:46:36 -0700298 ipc_min_cycle = IPCMNI_EXTEND_MIN_CYCLE;
Waiman Long5ac893b2019-05-14 15:46:29 -0700299 pr_info("IPCMNI extended to %d.\n", ipc_mni);
300 return 0;
301}
302early_param("ipcmni_extend", ipc_mni_extend);