Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 |
| 4 | * |
| 5 | * Author: Eric Biederman <ebiederm@xmision.com> |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 6 | */ |
| 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 Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 13 | #include <linux/ipc_namespace.h> |
Nadia Derbey | 6546bc4 | 2008-04-29 01:00:45 -0700 | [diff] [blame] | 14 | #include <linux/msg.h> |
| 15 | #include "util.h" |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 16 | |
Joe Perches | a5c5928 | 2014-06-06 14:38:07 -0700 | [diff] [blame] | 17 | static void *get_ipc(struct ctl_table *table) |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 18 | { |
| 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. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 24 | |
Serge E. Hallyn | 11dea19 | 2009-04-02 16:58:27 -0700 | [diff] [blame] | 25 | #ifdef CONFIG_PROC_SYSCTL |
Joe Perches | a5c5928 | 2014-06-06 14:38:07 -0700 | [diff] [blame] | 26 | static int proc_ipc_dointvec(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 27 | void *buffer, size_t *lenp, loff_t *ppos) |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 28 | { |
| 29 | struct ctl_table ipc_table; |
Vasiliy Kulikov | b34a6b1 | 2011-07-26 16:08:48 -0700 | [diff] [blame] | 30 | |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 31 | memcpy(&ipc_table, table, sizeof(ipc_table)); |
| 32 | ipc_table.data = get_ipc(table); |
| 33 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 34 | return proc_dointvec(&ipc_table, write, buffer, lenp, ppos); |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Joe Perches | a5c5928 | 2014-06-06 14:38:07 -0700 | [diff] [blame] | 37 | static int proc_ipc_dointvec_minmax(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 38 | void *buffer, size_t *lenp, loff_t *ppos) |
Vasiliy Kulikov | b34a6b1 | 2011-07-26 16:08:48 -0700 | [diff] [blame] | 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 Perches | a5c5928 | 2014-06-06 14:38:07 -0700 | [diff] [blame] | 48 | static int proc_ipc_dointvec_minmax_orphans(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 49 | void *buffer, size_t *lenp, loff_t *ppos) |
Vasiliy Kulikov | b34a6b1 | 2011-07-26 16:08:48 -0700 | [diff] [blame] | 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 Perches | a5c5928 | 2014-06-06 14:38:07 -0700 | [diff] [blame] | 61 | static int proc_ipc_doulongvec_minmax(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 62 | void *buffer, size_t *lenp, loff_t *ppos) |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 63 | { |
| 64 | struct ctl_table ipc_table; |
| 65 | memcpy(&ipc_table, table, sizeof(ipc_table)); |
| 66 | ipc_table.data = get_ipc(table); |
| 67 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 68 | return proc_doulongvec_minmax(&ipc_table, write, buffer, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 69 | lenp, ppos); |
| 70 | } |
| 71 | |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 72 | static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 73 | void *buffer, size_t *lenp, loff_t *ppos) |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 74 | { |
| 75 | struct ctl_table ipc_table; |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 76 | int dummy = 0; |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 77 | |
| 78 | memcpy(&ipc_table, table, sizeof(ipc_table)); |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 79 | ipc_table.data = &dummy; |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 80 | |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 81 | if (write) |
| 82 | pr_info_once("writing to auto_msgmni has no effect"); |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 83 | |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 84 | return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos); |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Waiman Long | 8c81ddd | 2018-10-30 15:07:24 -0700 | [diff] [blame] | 87 | static 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. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 107 | #else |
| 108 | #define proc_ipc_doulongvec_minmax NULL |
| 109 | #define proc_ipc_dointvec NULL |
Vasiliy Kulikov | b34a6b1 | 2011-07-26 16:08:48 -0700 | [diff] [blame] | 110 | #define proc_ipc_dointvec_minmax NULL |
| 111 | #define proc_ipc_dointvec_minmax_orphans NULL |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 112 | #define proc_ipc_auto_msgmni NULL |
Waiman Long | 8c81ddd | 2018-10-30 15:07:24 -0700 | [diff] [blame] | 113 | #define proc_ipc_sem_dointvec NULL |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 114 | #endif |
| 115 | |
Waiman Long | 5ac893b | 2019-05-14 15:46:29 -0700 | [diff] [blame] | 116 | int ipc_mni = IPCMNI; |
| 117 | int ipc_mni_shift = IPCMNI_SHIFT; |
Manfred Spraul | 99db46e | 2019-05-14 15:46:36 -0700 | [diff] [blame] | 118 | int ipc_min_cycle = RADIX_TREE_MAP_SIZE; |
Nadia Derbey | 9eefe52 | 2008-07-25 01:48:08 -0700 | [diff] [blame] | 119 | |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 120 | static struct ctl_table ipc_kern_table[] = { |
| 121 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 122 | .procname = "shmmax", |
| 123 | .data = &init_ipc_ns.shm_ctlmax, |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 124 | .maxlen = sizeof(init_ipc_ns.shm_ctlmax), |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 125 | .mode = 0644, |
| 126 | .proc_handler = proc_ipc_doulongvec_minmax, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 127 | }, |
| 128 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 129 | .procname = "shmall", |
| 130 | .data = &init_ipc_ns.shm_ctlall, |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 131 | .maxlen = sizeof(init_ipc_ns.shm_ctlall), |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 132 | .mode = 0644, |
| 133 | .proc_handler = proc_ipc_doulongvec_minmax, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 134 | }, |
| 135 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 136 | .procname = "shmmni", |
| 137 | .data = &init_ipc_ns.shm_ctlmni, |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 138 | .maxlen = sizeof(init_ipc_ns.shm_ctlmni), |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 139 | .mode = 0644, |
Waiman Long | 6730e65 | 2018-10-30 15:07:20 -0700 | [diff] [blame] | 140 | .proc_handler = proc_ipc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 141 | .extra1 = SYSCTL_ZERO, |
Waiman Long | 6730e65 | 2018-10-30 15:07:20 -0700 | [diff] [blame] | 142 | .extra2 = &ipc_mni, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 143 | }, |
| 144 | { |
Vasiliy Kulikov | b34a6b1 | 2011-07-26 16:08:48 -0700 | [diff] [blame] | 145 | .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 Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 150 | .extra1 = SYSCTL_ZERO, |
| 151 | .extra2 = SYSCTL_ONE, |
Vasiliy Kulikov | b34a6b1 | 2011-07-26 16:08:48 -0700 | [diff] [blame] | 152 | }, |
| 153 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 154 | .procname = "msgmax", |
| 155 | .data = &init_ipc_ns.msg_ctlmax, |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 156 | .maxlen = sizeof(init_ipc_ns.msg_ctlmax), |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 157 | .mode = 0644, |
Mathias Krause | 9bf76ca | 2013-11-03 12:36:28 +0100 | [diff] [blame] | 158 | .proc_handler = proc_ipc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 159 | .extra1 = SYSCTL_ZERO, |
| 160 | .extra2 = SYSCTL_INT_MAX, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 161 | }, |
| 162 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 163 | .procname = "msgmni", |
| 164 | .data = &init_ipc_ns.msg_ctlmni, |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 165 | .maxlen = sizeof(init_ipc_ns.msg_ctlmni), |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 166 | .mode = 0644, |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 167 | .proc_handler = proc_ipc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 168 | .extra1 = SYSCTL_ZERO, |
Waiman Long | 6730e65 | 2018-10-30 15:07:20 -0700 | [diff] [blame] | 169 | .extra2 = &ipc_mni, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 170 | }, |
| 171 | { |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 172 | .procname = "auto_msgmni", |
| 173 | .data = NULL, |
| 174 | .maxlen = sizeof(int), |
| 175 | .mode = 0644, |
| 176 | .proc_handler = proc_ipc_auto_msgmni, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 177 | .extra1 = SYSCTL_ZERO, |
| 178 | .extra2 = SYSCTL_ONE, |
Manfred Spraul | 0050ee0 | 2014-12-12 16:58:17 -0800 | [diff] [blame] | 179 | }, |
| 180 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 181 | .procname = "msgmnb", |
| 182 | .data = &init_ipc_ns.msg_ctlmnb, |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 183 | .maxlen = sizeof(init_ipc_ns.msg_ctlmnb), |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 184 | .mode = 0644, |
Mathias Krause | 9bf76ca | 2013-11-03 12:36:28 +0100 | [diff] [blame] | 185 | .proc_handler = proc_ipc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 186 | .extra1 = SYSCTL_ZERO, |
| 187 | .extra2 = SYSCTL_INT_MAX, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 188 | }, |
| 189 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 190 | .procname = "sem", |
| 191 | .data = &init_ipc_ns.sem_ctls, |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 192 | .maxlen = 4*sizeof(int), |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 193 | .mode = 0644, |
Waiman Long | 8c81ddd | 2018-10-30 15:07:24 -0700 | [diff] [blame] | 194 | .proc_handler = proc_ipc_sem_dointvec, |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 195 | }, |
Stanislav Kinsbursky | 03f5956 | 2013-01-04 15:34:50 -0800 | [diff] [blame] | 196 | #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 Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 203 | .extra1 = SYSCTL_ZERO, |
| 204 | .extra2 = SYSCTL_INT_MAX, |
Stanislav Kinsbursky | 03f5956 | 2013-01-04 15:34:50 -0800 | [diff] [blame] | 205 | }, |
| 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 Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 212 | .extra1 = SYSCTL_ZERO, |
| 213 | .extra2 = SYSCTL_INT_MAX, |
Stanislav Kinsbursky | 03f5956 | 2013-01-04 15:34:50 -0800 | [diff] [blame] | 214 | }, |
| 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 Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 221 | .extra1 = SYSCTL_ZERO, |
| 222 | .extra2 = SYSCTL_INT_MAX, |
Stanislav Kinsbursky | 03f5956 | 2013-01-04 15:34:50 -0800 | [diff] [blame] | 223 | }, |
| 224 | #endif |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 225 | {} |
| 226 | }; |
| 227 | |
| 228 | static struct ctl_table ipc_root_table[] = { |
| 229 | { |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 230 | .procname = "kernel", |
| 231 | .mode = 0555, |
| 232 | .child = ipc_kern_table, |
| 233 | }, |
| 234 | {} |
| 235 | }; |
| 236 | |
| 237 | static int __init ipc_sysctl_init(void) |
| 238 | { |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 239 | register_sysctl_table(ipc_root_table); |
Eric W. Biederman | a5494dc | 2007-02-14 00:34:06 -0800 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
Davidlohr Bueso | 6d08a25 | 2014-04-07 15:39:18 -0700 | [diff] [blame] | 243 | device_initcall(ipc_sysctl_init); |
Waiman Long | 5ac893b | 2019-05-14 15:46:29 -0700 | [diff] [blame] | 244 | |
| 245 | static int __init ipc_mni_extend(char *str) |
| 246 | { |
| 247 | ipc_mni = IPCMNI_EXTEND; |
| 248 | ipc_mni_shift = IPCMNI_EXTEND_SHIFT; |
Manfred Spraul | 99db46e | 2019-05-14 15:46:36 -0700 | [diff] [blame] | 249 | ipc_min_cycle = IPCMNI_EXTEND_MIN_CYCLE; |
Waiman Long | 5ac893b | 2019-05-14 15:46:29 -0700 | [diff] [blame] | 250 | pr_info("IPCMNI extended to %d.\n", ipc_mni); |
| 251 | return 0; |
| 252 | } |
| 253 | early_param("ipcmni_extend", ipc_mni_extend); |