blob: affd66537e87550a3fb88c144957506f9140dcd1 [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>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080013#include <linux/ipc_namespace.h>
Nadia Derbey6546bc42008-04-29 01:00:45 -070014#include <linux/msg.h>
15#include "util.h"
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080016
Joe Perchesa5c59282014-06-06 14:38:07 -070017static void *get_ipc(struct ctl_table *table)
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080018{
19 char *which = table->data;
20 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
21 which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns;
22 return which;
23}
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080024
Serge E. Hallyn11dea192009-04-02 16:58:27 -070025#ifdef CONFIG_PROC_SYSCTL
Joe Perchesa5c59282014-06-06 14:38:07 -070026static int proc_ipc_dointvec(struct ctl_table *table, int write,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -080027 void __user *buffer, size_t *lenp, loff_t *ppos)
28{
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,
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -070038 void __user *buffer, size_t *lenp, loff_t *ppos)
39{
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,
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -070049 void __user *buffer, size_t *lenp, loff_t *ppos)
50{
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,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -070062 void __user *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,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -070073 void __user *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,
88 void __user *buffer, size_t *lenp, loff_t *ppos)
89{
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
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800107#else
108#define proc_ipc_doulongvec_minmax NULL
109#define proc_ipc_dointvec NULL
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700110#define proc_ipc_dointvec_minmax NULL
111#define proc_ipc_dointvec_minmax_orphans NULL
Manfred Spraul0050ee02014-12-12 16:58:17 -0800112#define proc_ipc_auto_msgmni NULL
Waiman Long8c81ddd2018-10-30 15:07:24 -0700113#define proc_ipc_sem_dointvec NULL
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800114#endif
115
Waiman Long5ac893b2019-05-14 15:46:29 -0700116int ipc_mni = IPCMNI;
117int ipc_mni_shift = IPCMNI_SHIFT;
Manfred Spraul99db46e2019-05-14 15:46:36 -0700118int ipc_min_cycle = RADIX_TREE_MAP_SIZE;
Nadia Derbey9eefe522008-07-25 01:48:08 -0700119
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800120static struct ctl_table ipc_kern_table[] = {
121 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800122 .procname = "shmmax",
123 .data = &init_ipc_ns.shm_ctlmax,
Manfred Spraul239521f2014-01-27 17:07:04 -0800124 .maxlen = sizeof(init_ipc_ns.shm_ctlmax),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800125 .mode = 0644,
126 .proc_handler = proc_ipc_doulongvec_minmax,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800127 },
128 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800129 .procname = "shmall",
130 .data = &init_ipc_ns.shm_ctlall,
Manfred Spraul239521f2014-01-27 17:07:04 -0800131 .maxlen = sizeof(init_ipc_ns.shm_ctlall),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800132 .mode = 0644,
133 .proc_handler = proc_ipc_doulongvec_minmax,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800134 },
135 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800136 .procname = "shmmni",
137 .data = &init_ipc_ns.shm_ctlmni,
Manfred Spraul239521f2014-01-27 17:07:04 -0800138 .maxlen = sizeof(init_ipc_ns.shm_ctlmni),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800139 .mode = 0644,
Waiman Long6730e652018-10-30 15:07:20 -0700140 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700141 .extra1 = SYSCTL_ZERO,
Waiman Long6730e652018-10-30 15:07:20 -0700142 .extra2 = &ipc_mni,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800143 },
144 {
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700145 .procname = "shm_rmid_forced",
146 .data = &init_ipc_ns.shm_rmid_forced,
147 .maxlen = sizeof(init_ipc_ns.shm_rmid_forced),
148 .mode = 0644,
149 .proc_handler = proc_ipc_dointvec_minmax_orphans,
Matteo Croceeec48442019-07-18 15:58:50 -0700150 .extra1 = SYSCTL_ZERO,
151 .extra2 = SYSCTL_ONE,
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700152 },
153 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800154 .procname = "msgmax",
155 .data = &init_ipc_ns.msg_ctlmax,
Manfred Spraul239521f2014-01-27 17:07:04 -0800156 .maxlen = sizeof(init_ipc_ns.msg_ctlmax),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800157 .mode = 0644,
Mathias Krause9bf76ca2013-11-03 12:36:28 +0100158 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700159 .extra1 = SYSCTL_ZERO,
160 .extra2 = SYSCTL_INT_MAX,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800161 },
162 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800163 .procname = "msgmni",
164 .data = &init_ipc_ns.msg_ctlmni,
Manfred Spraul239521f2014-01-27 17:07:04 -0800165 .maxlen = sizeof(init_ipc_ns.msg_ctlmni),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800166 .mode = 0644,
Manfred Spraul0050ee02014-12-12 16:58:17 -0800167 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700168 .extra1 = SYSCTL_ZERO,
Waiman Long6730e652018-10-30 15:07:20 -0700169 .extra2 = &ipc_mni,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800170 },
171 {
Manfred Spraul0050ee02014-12-12 16:58:17 -0800172 .procname = "auto_msgmni",
173 .data = NULL,
174 .maxlen = sizeof(int),
175 .mode = 0644,
176 .proc_handler = proc_ipc_auto_msgmni,
Matteo Croceeec48442019-07-18 15:58:50 -0700177 .extra1 = SYSCTL_ZERO,
178 .extra2 = SYSCTL_ONE,
Manfred Spraul0050ee02014-12-12 16:58:17 -0800179 },
180 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800181 .procname = "msgmnb",
182 .data = &init_ipc_ns.msg_ctlmnb,
Manfred Spraul239521f2014-01-27 17:07:04 -0800183 .maxlen = sizeof(init_ipc_ns.msg_ctlmnb),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800184 .mode = 0644,
Mathias Krause9bf76ca2013-11-03 12:36:28 +0100185 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700186 .extra1 = SYSCTL_ZERO,
187 .extra2 = SYSCTL_INT_MAX,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800188 },
189 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800190 .procname = "sem",
191 .data = &init_ipc_ns.sem_ctls,
Manfred Spraul239521f2014-01-27 17:07:04 -0800192 .maxlen = 4*sizeof(int),
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800193 .mode = 0644,
Waiman Long8c81ddd2018-10-30 15:07:24 -0700194 .proc_handler = proc_ipc_sem_dointvec,
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800195 },
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800196#ifdef CONFIG_CHECKPOINT_RESTORE
197 {
198 .procname = "sem_next_id",
199 .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
200 .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
201 .mode = 0644,
202 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700203 .extra1 = SYSCTL_ZERO,
204 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800205 },
206 {
207 .procname = "msg_next_id",
208 .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
209 .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
210 .mode = 0644,
211 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700212 .extra1 = SYSCTL_ZERO,
213 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800214 },
215 {
216 .procname = "shm_next_id",
217 .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
218 .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
219 .mode = 0644,
220 .proc_handler = proc_ipc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700221 .extra1 = SYSCTL_ZERO,
222 .extra2 = SYSCTL_INT_MAX,
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800223 },
224#endif
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800225 {}
226};
227
228static struct ctl_table ipc_root_table[] = {
229 {
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800230 .procname = "kernel",
231 .mode = 0555,
232 .child = ipc_kern_table,
233 },
234 {}
235};
236
237static int __init ipc_sysctl_init(void)
238{
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800239 register_sysctl_table(ipc_root_table);
Eric W. Biedermana5494dc2007-02-14 00:34:06 -0800240 return 0;
241}
242
Davidlohr Bueso6d08a252014-04-07 15:39:18 -0700243device_initcall(ipc_sysctl_init);
Waiman Long5ac893b2019-05-14 15:46:29 -0700244
245static int __init ipc_mni_extend(char *str)
246{
247 ipc_mni = IPCMNI_EXTEND;
248 ipc_mni_shift = IPCMNI_EXTEND_SHIFT;
Manfred Spraul99db46e2019-05-14 15:46:36 -0700249 ipc_min_cycle = IPCMNI_EXTEND_MIN_CYCLE;
Waiman Long5ac893b2019-05-14 15:46:29 -0700250 pr_info("IPCMNI extended to %d.\n", ipc_mni);
251 return 0;
252}
253early_param("ipcmni_extend", ipc_mni_extend);