1a5494dcdSEric W. Biederman /* 2a5494dcdSEric W. Biederman * Copyright (C) 2007 3a5494dcdSEric W. Biederman * 4a5494dcdSEric W. Biederman * Author: Eric Biederman <ebiederm@xmision.com> 5a5494dcdSEric W. Biederman * 6a5494dcdSEric W. Biederman * This program is free software; you can redistribute it and/or 7a5494dcdSEric W. Biederman * modify it under the terms of the GNU General Public License as 8a5494dcdSEric W. Biederman * published by the Free Software Foundation, version 2 of the 9a5494dcdSEric W. Biederman * License. 10a5494dcdSEric W. Biederman */ 11a5494dcdSEric W. Biederman 12a5494dcdSEric W. Biederman #include <linux/module.h> 13a5494dcdSEric W. Biederman #include <linux/ipc.h> 14a5494dcdSEric W. Biederman #include <linux/nsproxy.h> 15a5494dcdSEric W. Biederman #include <linux/sysctl.h> 16a5494dcdSEric W. Biederman #include <linux/uaccess.h> 17ae5e1b22SPavel Emelyanov #include <linux/ipc_namespace.h> 186546bc42SNadia Derbey #include <linux/msg.h> 196546bc42SNadia Derbey #include "util.h" 20a5494dcdSEric W. Biederman 21a5494dcdSEric W. Biederman static void *get_ipc(ctl_table *table) 22a5494dcdSEric W. Biederman { 23a5494dcdSEric W. Biederman char *which = table->data; 24a5494dcdSEric W. Biederman struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns; 25a5494dcdSEric W. Biederman which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns; 26a5494dcdSEric W. Biederman return which; 27a5494dcdSEric W. Biederman } 28a5494dcdSEric W. Biederman 296546bc42SNadia Derbey /* 30*9eefe520SNadia Derbey * Routine that is called when the file "auto_msgmni" has successfully been 31*9eefe520SNadia Derbey * written. 32*9eefe520SNadia Derbey * Two values are allowed: 33*9eefe520SNadia Derbey * 0: unregister msgmni's callback routine from the ipc namespace notifier 34*9eefe520SNadia Derbey * chain. This means that msgmni won't be recomputed anymore upon memory 356546bc42SNadia Derbey * add/remove or ipc namespace creation/removal. 36*9eefe520SNadia Derbey * 1: register back the callback routine. 376546bc42SNadia Derbey */ 38*9eefe520SNadia Derbey static void ipc_auto_callback(int val) 396546bc42SNadia Derbey { 40*9eefe520SNadia Derbey if (!val) 416546bc42SNadia Derbey unregister_ipcns_notifier(current->nsproxy->ipc_ns); 426546bc42SNadia Derbey else { 436546bc42SNadia Derbey /* 446546bc42SNadia Derbey * Re-enable automatic recomputing only if not already 456546bc42SNadia Derbey * enabled. 466546bc42SNadia Derbey */ 476546bc42SNadia Derbey recompute_msgmni(current->nsproxy->ipc_ns); 486546bc42SNadia Derbey cond_register_ipcns_notifier(current->nsproxy->ipc_ns); 496546bc42SNadia Derbey } 506546bc42SNadia Derbey } 516546bc42SNadia Derbey 52a5494dcdSEric W. Biederman #ifdef CONFIG_PROC_FS 53a5494dcdSEric W. Biederman static int proc_ipc_dointvec(ctl_table *table, int write, struct file *filp, 54a5494dcdSEric W. Biederman void __user *buffer, size_t *lenp, loff_t *ppos) 55a5494dcdSEric W. Biederman { 56a5494dcdSEric W. Biederman struct ctl_table ipc_table; 57a5494dcdSEric W. Biederman memcpy(&ipc_table, table, sizeof(ipc_table)); 58a5494dcdSEric W. Biederman ipc_table.data = get_ipc(table); 59a5494dcdSEric W. Biederman 60a5494dcdSEric W. Biederman return proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos); 61a5494dcdSEric W. Biederman } 62a5494dcdSEric W. Biederman 6391cfb2b4SNadia Derbey static int proc_ipc_callback_dointvec(ctl_table *table, int write, 6491cfb2b4SNadia Derbey struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) 6591cfb2b4SNadia Derbey { 666546bc42SNadia Derbey struct ctl_table ipc_table; 6791cfb2b4SNadia Derbey size_t lenp_bef = *lenp; 6891cfb2b4SNadia Derbey int rc; 6991cfb2b4SNadia Derbey 706546bc42SNadia Derbey memcpy(&ipc_table, table, sizeof(ipc_table)); 716546bc42SNadia Derbey ipc_table.data = get_ipc(table); 726546bc42SNadia Derbey 736546bc42SNadia Derbey rc = proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos); 7491cfb2b4SNadia Derbey 7591cfb2b4SNadia Derbey if (write && !rc && lenp_bef == *lenp) 76*9eefe520SNadia Derbey /* 77*9eefe520SNadia Derbey * Tunable has successfully been changed by hand. Disable its 78*9eefe520SNadia Derbey * automatic adjustment. This simply requires unregistering 79*9eefe520SNadia Derbey * the notifiers that trigger recalculation. 80*9eefe520SNadia Derbey */ 81*9eefe520SNadia Derbey unregister_ipcns_notifier(current->nsproxy->ipc_ns); 8291cfb2b4SNadia Derbey 8391cfb2b4SNadia Derbey return rc; 8491cfb2b4SNadia Derbey } 8591cfb2b4SNadia Derbey 86a5494dcdSEric W. Biederman static int proc_ipc_doulongvec_minmax(ctl_table *table, int write, 87a5494dcdSEric W. Biederman struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) 88a5494dcdSEric W. Biederman { 89a5494dcdSEric W. Biederman struct ctl_table ipc_table; 90a5494dcdSEric W. Biederman memcpy(&ipc_table, table, sizeof(ipc_table)); 91a5494dcdSEric W. Biederman ipc_table.data = get_ipc(table); 92a5494dcdSEric W. Biederman 93a5494dcdSEric W. Biederman return proc_doulongvec_minmax(&ipc_table, write, filp, buffer, 94a5494dcdSEric W. Biederman lenp, ppos); 95a5494dcdSEric W. Biederman } 96a5494dcdSEric W. Biederman 97*9eefe520SNadia Derbey static int proc_ipcauto_dointvec_minmax(ctl_table *table, int write, 98*9eefe520SNadia Derbey struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) 99*9eefe520SNadia Derbey { 100*9eefe520SNadia Derbey struct ctl_table ipc_table; 101*9eefe520SNadia Derbey size_t lenp_bef = *lenp; 102*9eefe520SNadia Derbey int oldval; 103*9eefe520SNadia Derbey int rc; 104*9eefe520SNadia Derbey 105*9eefe520SNadia Derbey memcpy(&ipc_table, table, sizeof(ipc_table)); 106*9eefe520SNadia Derbey ipc_table.data = get_ipc(table); 107*9eefe520SNadia Derbey oldval = *((int *)(ipc_table.data)); 108*9eefe520SNadia Derbey 109*9eefe520SNadia Derbey rc = proc_dointvec_minmax(&ipc_table, write, filp, buffer, lenp, ppos); 110*9eefe520SNadia Derbey 111*9eefe520SNadia Derbey if (write && !rc && lenp_bef == *lenp) { 112*9eefe520SNadia Derbey int newval = *((int *)(ipc_table.data)); 113*9eefe520SNadia Derbey /* 114*9eefe520SNadia Derbey * The file "auto_msgmni" has correctly been set. 115*9eefe520SNadia Derbey * React by (un)registering the corresponding tunable, if the 116*9eefe520SNadia Derbey * value has changed. 117*9eefe520SNadia Derbey */ 118*9eefe520SNadia Derbey if (newval != oldval) 119*9eefe520SNadia Derbey ipc_auto_callback(newval); 120*9eefe520SNadia Derbey } 121*9eefe520SNadia Derbey 122*9eefe520SNadia Derbey return rc; 123*9eefe520SNadia Derbey } 124*9eefe520SNadia Derbey 125a5494dcdSEric W. Biederman #else 126a5494dcdSEric W. Biederman #define proc_ipc_doulongvec_minmax NULL 127a5494dcdSEric W. Biederman #define proc_ipc_dointvec NULL 12891cfb2b4SNadia Derbey #define proc_ipc_callback_dointvec NULL 129*9eefe520SNadia Derbey #define proc_ipcauto_dointvec_minmax NULL 130a5494dcdSEric W. Biederman #endif 131a5494dcdSEric W. Biederman 132a5494dcdSEric W. Biederman #ifdef CONFIG_SYSCTL_SYSCALL 133a5494dcdSEric W. Biederman /* The generic sysctl ipc data routine. */ 134a5494dcdSEric W. Biederman static int sysctl_ipc_data(ctl_table *table, int __user *name, int nlen, 135a5494dcdSEric W. Biederman void __user *oldval, size_t __user *oldlenp, 136a5494dcdSEric W. Biederman void __user *newval, size_t newlen) 137a5494dcdSEric W. Biederman { 138a5494dcdSEric W. Biederman size_t len; 139a5494dcdSEric W. Biederman void *data; 140a5494dcdSEric W. Biederman 141a5494dcdSEric W. Biederman /* Get out of I don't have a variable */ 142a5494dcdSEric W. Biederman if (!table->data || !table->maxlen) 143a5494dcdSEric W. Biederman return -ENOTDIR; 144a5494dcdSEric W. Biederman 145a5494dcdSEric W. Biederman data = get_ipc(table); 146a5494dcdSEric W. Biederman if (!data) 147a5494dcdSEric W. Biederman return -ENOTDIR; 148a5494dcdSEric W. Biederman 149a5494dcdSEric W. Biederman if (oldval && oldlenp) { 150a5494dcdSEric W. Biederman if (get_user(len, oldlenp)) 151a5494dcdSEric W. Biederman return -EFAULT; 152a5494dcdSEric W. Biederman if (len) { 153a5494dcdSEric W. Biederman if (len > table->maxlen) 154a5494dcdSEric W. Biederman len = table->maxlen; 155a5494dcdSEric W. Biederman if (copy_to_user(oldval, data, len)) 156a5494dcdSEric W. Biederman return -EFAULT; 157a5494dcdSEric W. Biederman if (put_user(len, oldlenp)) 158a5494dcdSEric W. Biederman return -EFAULT; 159a5494dcdSEric W. Biederman } 160a5494dcdSEric W. Biederman } 161a5494dcdSEric W. Biederman 162a5494dcdSEric W. Biederman if (newval && newlen) { 163a5494dcdSEric W. Biederman if (newlen > table->maxlen) 164a5494dcdSEric W. Biederman newlen = table->maxlen; 165a5494dcdSEric W. Biederman 166a5494dcdSEric W. Biederman if (copy_from_user(data, newval, newlen)) 167a5494dcdSEric W. Biederman return -EFAULT; 168a5494dcdSEric W. Biederman } 169a5494dcdSEric W. Biederman return 1; 170a5494dcdSEric W. Biederman } 17191cfb2b4SNadia Derbey 17291cfb2b4SNadia Derbey static int sysctl_ipc_registered_data(ctl_table *table, int __user *name, 17391cfb2b4SNadia Derbey int nlen, void __user *oldval, size_t __user *oldlenp, 17491cfb2b4SNadia Derbey void __user *newval, size_t newlen) 17591cfb2b4SNadia Derbey { 17691cfb2b4SNadia Derbey int rc; 17791cfb2b4SNadia Derbey 17891cfb2b4SNadia Derbey rc = sysctl_ipc_data(table, name, nlen, oldval, oldlenp, newval, 17991cfb2b4SNadia Derbey newlen); 18091cfb2b4SNadia Derbey 181*9eefe520SNadia Derbey if (newval && newlen && rc > 0) 18291cfb2b4SNadia Derbey /* 1836546bc42SNadia Derbey * Tunable has successfully been changed from userland 18491cfb2b4SNadia Derbey */ 185*9eefe520SNadia Derbey unregister_ipcns_notifier(current->nsproxy->ipc_ns); 18691cfb2b4SNadia Derbey 18791cfb2b4SNadia Derbey return rc; 18891cfb2b4SNadia Derbey } 189a5494dcdSEric W. Biederman #else 190a5494dcdSEric W. Biederman #define sysctl_ipc_data NULL 19191cfb2b4SNadia Derbey #define sysctl_ipc_registered_data NULL 192a5494dcdSEric W. Biederman #endif 193a5494dcdSEric W. Biederman 194*9eefe520SNadia Derbey static int zero; 195*9eefe520SNadia Derbey static int one = 1; 196*9eefe520SNadia Derbey 197a5494dcdSEric W. Biederman static struct ctl_table ipc_kern_table[] = { 198a5494dcdSEric W. Biederman { 199a5494dcdSEric W. Biederman .ctl_name = KERN_SHMMAX, 200a5494dcdSEric W. Biederman .procname = "shmmax", 201a5494dcdSEric W. Biederman .data = &init_ipc_ns.shm_ctlmax, 202a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.shm_ctlmax), 203a5494dcdSEric W. Biederman .mode = 0644, 204a5494dcdSEric W. Biederman .proc_handler = proc_ipc_doulongvec_minmax, 205a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 206a5494dcdSEric W. Biederman }, 207a5494dcdSEric W. Biederman { 208a5494dcdSEric W. Biederman .ctl_name = KERN_SHMALL, 209a5494dcdSEric W. Biederman .procname = "shmall", 210a5494dcdSEric W. Biederman .data = &init_ipc_ns.shm_ctlall, 211a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.shm_ctlall), 212a5494dcdSEric W. Biederman .mode = 0644, 213a5494dcdSEric W. Biederman .proc_handler = proc_ipc_doulongvec_minmax, 214a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 215a5494dcdSEric W. Biederman }, 216a5494dcdSEric W. Biederman { 217a5494dcdSEric W. Biederman .ctl_name = KERN_SHMMNI, 218a5494dcdSEric W. Biederman .procname = "shmmni", 219a5494dcdSEric W. Biederman .data = &init_ipc_ns.shm_ctlmni, 220a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.shm_ctlmni), 221a5494dcdSEric W. Biederman .mode = 0644, 222a5494dcdSEric W. Biederman .proc_handler = proc_ipc_dointvec, 223a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 224a5494dcdSEric W. Biederman }, 225a5494dcdSEric W. Biederman { 226a5494dcdSEric W. Biederman .ctl_name = KERN_MSGMAX, 227a5494dcdSEric W. Biederman .procname = "msgmax", 228a5494dcdSEric W. Biederman .data = &init_ipc_ns.msg_ctlmax, 229a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.msg_ctlmax), 230a5494dcdSEric W. Biederman .mode = 0644, 231a5494dcdSEric W. Biederman .proc_handler = proc_ipc_dointvec, 232a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 233a5494dcdSEric W. Biederman }, 234a5494dcdSEric W. Biederman { 235a5494dcdSEric W. Biederman .ctl_name = KERN_MSGMNI, 236a5494dcdSEric W. Biederman .procname = "msgmni", 237a5494dcdSEric W. Biederman .data = &init_ipc_ns.msg_ctlmni, 238a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.msg_ctlmni), 239a5494dcdSEric W. Biederman .mode = 0644, 24091cfb2b4SNadia Derbey .proc_handler = proc_ipc_callback_dointvec, 24191cfb2b4SNadia Derbey .strategy = sysctl_ipc_registered_data, 242a5494dcdSEric W. Biederman }, 243a5494dcdSEric W. Biederman { 244a5494dcdSEric W. Biederman .ctl_name = KERN_MSGMNB, 245a5494dcdSEric W. Biederman .procname = "msgmnb", 246a5494dcdSEric W. Biederman .data = &init_ipc_ns.msg_ctlmnb, 247a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.msg_ctlmnb), 248a5494dcdSEric W. Biederman .mode = 0644, 249a5494dcdSEric W. Biederman .proc_handler = proc_ipc_dointvec, 250a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 251a5494dcdSEric W. Biederman }, 252a5494dcdSEric W. Biederman { 253a5494dcdSEric W. Biederman .ctl_name = KERN_SEM, 254a5494dcdSEric W. Biederman .procname = "sem", 255a5494dcdSEric W. Biederman .data = &init_ipc_ns.sem_ctls, 256a5494dcdSEric W. Biederman .maxlen = 4*sizeof (int), 257a5494dcdSEric W. Biederman .mode = 0644, 258a5494dcdSEric W. Biederman .proc_handler = proc_ipc_dointvec, 259a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 260a5494dcdSEric W. Biederman }, 261*9eefe520SNadia Derbey { 262*9eefe520SNadia Derbey .ctl_name = CTL_UNNUMBERED, 263*9eefe520SNadia Derbey .procname = "auto_msgmni", 264*9eefe520SNadia Derbey .data = &init_ipc_ns.auto_msgmni, 265*9eefe520SNadia Derbey .maxlen = sizeof(int), 266*9eefe520SNadia Derbey .mode = 0644, 267*9eefe520SNadia Derbey .proc_handler = proc_ipcauto_dointvec_minmax, 268*9eefe520SNadia Derbey .extra1 = &zero, 269*9eefe520SNadia Derbey .extra2 = &one, 270*9eefe520SNadia Derbey }, 271a5494dcdSEric W. Biederman {} 272a5494dcdSEric W. Biederman }; 273a5494dcdSEric W. Biederman 274a5494dcdSEric W. Biederman static struct ctl_table ipc_root_table[] = { 275a5494dcdSEric W. Biederman { 276a5494dcdSEric W. Biederman .ctl_name = CTL_KERN, 277a5494dcdSEric W. Biederman .procname = "kernel", 278a5494dcdSEric W. Biederman .mode = 0555, 279a5494dcdSEric W. Biederman .child = ipc_kern_table, 280a5494dcdSEric W. Biederman }, 281a5494dcdSEric W. Biederman {} 282a5494dcdSEric W. Biederman }; 283a5494dcdSEric W. Biederman 284a5494dcdSEric W. Biederman static int __init ipc_sysctl_init(void) 285a5494dcdSEric W. Biederman { 2860b4d4147SEric W. Biederman register_sysctl_table(ipc_root_table); 287a5494dcdSEric W. Biederman return 0; 288a5494dcdSEric W. Biederman } 289a5494dcdSEric W. Biederman 290a5494dcdSEric W. Biederman __initcall(ipc_sysctl_init); 291