blob: f101c171753f6fe652ca9bf4df3f251bb3d68019 [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>
16#include "util.h"
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080017
Joe Perchesa5c59282014-06-06 14:38:07 -070018static void *get_ipc(struct ctl_table *table)
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080019{
20 char *which = table->data;
21 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
22 which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns;
23 return which;
24}
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080025
Joe Perchesa5c59282014-06-06 14:38:07 -070026static int proc_ipc_dointvec(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020027 void *buffer, size_t *lenp, loff_t *ppos)
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080028{
29 struct ctl_table ipc_table;
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -070030
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080031 memcpy(&ipc_table, table, sizeof(ipc_table));
32 ipc_table.data = get_ipc(table);
33
Alexey Dobriyan8d65af72009-09-23 15:57:19 -070034 return proc_dointvec(&ipc_table, write, buffer, lenp, ppos);
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080035}
36
Joe Perchesa5c59282014-06-06 14:38:07 -070037static int proc_ipc_dointvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020038 void *buffer, size_t *lenp, loff_t *ppos)
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -070039{
40 struct ctl_table ipc_table;
41
42 memcpy(&ipc_table, table, sizeof(ipc_table));
43 ipc_table.data = get_ipc(table);
44
45 return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
46}
47
Joe Perchesa5c59282014-06-06 14:38:07 -070048static int proc_ipc_dointvec_minmax_orphans(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020049 void *buffer, size_t *lenp, loff_t *ppos)
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -070050{
51 struct ipc_namespace *ns = current->nsproxy->ipc_ns;
52 int err = proc_ipc_dointvec_minmax(table, write, buffer, lenp, ppos);
53
54 if (err < 0)
55 return err;
56 if (ns->shm_rmid_forced)
57 shm_destroy_orphaned(ns);
58 return err;
59}
60
Joe Perchesa5c59282014-06-06 14:38:07 -070061static int proc_ipc_doulongvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020062 void *buffer, size_t *lenp, loff_t *ppos)
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080063{
64 struct ctl_table ipc_table;
65 memcpy(&ipc_table, table, sizeof(ipc_table));
66 ipc_table.data = get_ipc(table);
67
Alexey Dobriyan8d65af72009-09-23 15:57:19 -070068 return proc_doulongvec_minmax(&ipc_table, write, buffer,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080069 lenp, ppos);
70}
71
Manfred Spraul0050ee02014-12-12 16:58:17 -080072static int proc_ipc_auto_msgmni(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020073 void *buffer, size_t *lenp, loff_t *ppos)
Nadia Derbey9eefe522008-07-25 01:48:08 -070074{
75 struct ctl_table ipc_table;
Manfred Spraul0050ee02014-12-12 16:58:17 -080076 int dummy = 0;
Nadia Derbey9eefe522008-07-25 01:48:08 -070077
78 memcpy(&ipc_table, table, sizeof(ipc_table));
Manfred Spraul0050ee02014-12-12 16:58:17 -080079 ipc_table.data = &dummy;
Nadia Derbey9eefe522008-07-25 01:48:08 -070080
Manfred Spraul0050ee02014-12-12 16:58:17 -080081 if (write)
82 pr_info_once("writing to auto_msgmni has no effect");
Nadia Derbey9eefe522008-07-25 01:48:08 -070083
Manfred Spraul0050ee02014-12-12 16:58:17 -080084 return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
Nadia Derbey9eefe522008-07-25 01:48:08 -070085}
86
Waiman Long8c81ddd2018-10-30 15:07:24 -070087static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,
Tobias Klauserfff16622020-09-04 16:35:46 -070088 void *buffer, size_t *lenp, loff_t *ppos)
Waiman Long8c81ddd2018-10-30 15:07:24 -070089{
90 int ret, semmni;
91 struct ipc_namespace *ns = current->nsproxy->ipc_ns;
92
93 semmni = ns->sem_ctls[3];
94 ret = proc_ipc_dointvec(table, write, buffer, lenp, ppos);
95
96 if (!ret)
97 ret = sem_check_semmni(current->nsproxy->ipc_ns);
98
99 /*
100 * Reset the semmni value if an error happens.
101 */
102 if (ret)
103 ns->sem_ctls[3] = semmni;
104 return ret;
105}
106
Michal Clapinski5563cab2021-11-08 18:35:59 -0800107#ifdef CONFIG_CHECKPOINT_RESTORE
108static int proc_ipc_dointvec_minmax_checkpoint_restore(struct ctl_table *table,
109 int write, void *buffer, size_t *lenp, loff_t *ppos)
110{
111 struct user_namespace *user_ns = current->nsproxy->ipc_ns->user_ns;
112
113 if (write && !checkpoint_restore_ns_capable(user_ns))
114 return -EPERM;
115
116 return proc_ipc_dointvec_minmax(table, write, buffer, lenp, ppos);
117}
118#endif
119
Waiman Long5ac893b2019-05-14 15:46:29 -0700120int ipc_mni = IPCMNI;
121int ipc_mni_shift = IPCMNI_SHIFT;
Manfred Spraul99db46e2019-05-14 15:46:36 -0700122int ipc_min_cycle = RADIX_TREE_MAP_SIZE;
Nadia Derbey9eefe522008-07-25 01:48:08 -0700123
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800124static struct ctl_table ipc_kern_table[] = {
125 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800126 .procname = "shmmax",
127 .data = &init_ipc_ns.shm_ctlmax,
Manfred Spraul239521f2014-01-27 17:07:04 -0800128 .maxlen = sizeof(init_ipc_ns.shm_ctlmax),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800129 .mode = 0644,
130 .proc_handler = proc_ipc_doulongvec_minmax,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800131 },
132 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800133 .procname = "shmall",
134 .data = &init_ipc_ns.shm_ctlall,
Manfred Spraul239521f2014-01-27 17:07:04 -0800135 .maxlen = sizeof(init_ipc_ns.shm_ctlall),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800136 .mode = 0644,
137 .proc_handler = proc_ipc_doulongvec_minmax,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800138 },
139 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800140 .procname = "shmmni",
141 .data = &init_ipc_ns.shm_ctlmni,
Manfred Spraul239521f2014-01-27 17:07:04 -0800142 .maxlen = sizeof(init_ipc_ns.shm_ctlmni),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800143 .mode = 0644,
Waiman Long6730e652018-10-30 15:07:20 -0700144 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700145 .extra1 = SYSCTL_ZERO,
Waiman Long6730e652018-10-30 15:07:20 -0700146 .extra2 = &ipc_mni,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800147 },
148 {
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700149 .procname = "shm_rmid_forced",
150 .data = &init_ipc_ns.shm_rmid_forced,
151 .maxlen = sizeof(init_ipc_ns.shm_rmid_forced),
152 .mode = 0644,
153 .proc_handler = proc_ipc_dointvec_minmax_orphans,
Matteo Croceeec48442019-07-18 15:58:50 -0700154 .extra1 = SYSCTL_ZERO,
155 .extra2 = SYSCTL_ONE,
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700156 },
157 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800158 .procname = "msgmax",
159 .data = &init_ipc_ns.msg_ctlmax,
Manfred Spraul239521f2014-01-27 17:07:04 -0800160 .maxlen = sizeof(init_ipc_ns.msg_ctlmax),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800161 .mode = 0644,
Mathias Krause9bf76ca2013-11-03 12:36:28 +0100162 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700163 .extra1 = SYSCTL_ZERO,
164 .extra2 = SYSCTL_INT_MAX,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800165 },
166 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800167 .procname = "msgmni",
168 .data = &init_ipc_ns.msg_ctlmni,
Manfred Spraul239521f2014-01-27 17:07:04 -0800169 .maxlen = sizeof(init_ipc_ns.msg_ctlmni),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800170 .mode = 0644,
Manfred Spraul0050ee02014-12-12 16:58:17 -0800171 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700172 .extra1 = SYSCTL_ZERO,
Waiman Long6730e652018-10-30 15:07:20 -0700173 .extra2 = &ipc_mni,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800174 },
175 {
Manfred Spraul0050ee02014-12-12 16:58:17 -0800176 .procname = "auto_msgmni",
177 .data = NULL,
178 .maxlen = sizeof(int),
179 .mode = 0644,
180 .proc_handler = proc_ipc_auto_msgmni,
Matteo Croceeec48442019-07-18 15:58:50 -0700181 .extra1 = SYSCTL_ZERO,
182 .extra2 = SYSCTL_ONE,
Manfred Spraul0050ee02014-12-12 16:58:17 -0800183 },
184 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800185 .procname = "msgmnb",
186 .data = &init_ipc_ns.msg_ctlmnb,
Manfred Spraul239521f2014-01-27 17:07:04 -0800187 .maxlen = sizeof(init_ipc_ns.msg_ctlmnb),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800188 .mode = 0644,
Mathias Krause9bf76ca2013-11-03 12:36:28 +0100189 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700190 .extra1 = SYSCTL_ZERO,
191 .extra2 = SYSCTL_INT_MAX,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800192 },
193 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800194 .procname = "sem",
195 .data = &init_ipc_ns.sem_ctls,
Manfred Spraul239521f2014-01-27 17:07:04 -0800196 .maxlen = 4*sizeof(int),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800197 .mode = 0644,
Waiman Long8c81ddd2018-10-30 15:07:24 -0700198 .proc_handler = proc_ipc_sem_dointvec,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800199 },
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800200#ifdef CONFIG_CHECKPOINT_RESTORE
201 {
202 .procname = "sem_next_id",
203 .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
204 .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
Michal Clapinski5563cab2021-11-08 18:35:59 -0800205 .mode = 0666,
206 .proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore,
Matteo Croceeec48442019-07-18 15:58:50 -0700207 .extra1 = SYSCTL_ZERO,
208 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800209 },
210 {
211 .procname = "msg_next_id",
212 .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
213 .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
Michal Clapinski5563cab2021-11-08 18:35:59 -0800214 .mode = 0666,
215 .proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore,
Matteo Croceeec48442019-07-18 15:58:50 -0700216 .extra1 = SYSCTL_ZERO,
217 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800218 },
219 {
220 .procname = "shm_next_id",
221 .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
222 .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
Michal Clapinski5563cab2021-11-08 18:35:59 -0800223 .mode = 0666,
224 .proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore,
Matteo Croceeec48442019-07-18 15:58:50 -0700225 .extra1 = SYSCTL_ZERO,
226 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800227 },
228#endif
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800229 {}
230};
231
232static struct ctl_table ipc_root_table[] = {
233 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800234 .procname = "kernel",
235 .mode = 0555,
236 .child = ipc_kern_table,
237 },
238 {}
239};
240
241static int __init ipc_sysctl_init(void)
242{
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800243 register_sysctl_table(ipc_root_table);
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800244 return 0;
245}
246
Davidlohr Bueso6d08a252014-04-07 15:39:18 -0700247device_initcall(ipc_sysctl_init);
Waiman Long5ac893b2019-05-14 15:46:29 -0700248
249static int __init ipc_mni_extend(char *str)
250{
251 ipc_mni = IPCMNI_EXTEND;
252 ipc_mni_shift = IPCMNI_EXTEND_SHIFT;
Manfred Spraul99db46e2019-05-14 15:46:36 -0700253 ipc_min_cycle = IPCMNI_EXTEND_MIN_CYCLE;
Waiman Long5ac893b2019-05-14 15:46:29 -0700254 pr_info("IPCMNI extended to %d.\n", ipc_mni);
255 return 0;
256}
257early_param("ipcmni_extend", ipc_mni_extend);