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> 18*6546bc42SNadia Derbey #include <linux/msg.h> 19*6546bc42SNadia 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 29*6546bc42SNadia Derbey /* 30*6546bc42SNadia Derbey * Routine that is called when a tunable has successfully been changed by 31*6546bc42SNadia Derbey * hand and it has a callback routine registered on the ipc namespace notifier 32*6546bc42SNadia Derbey * chain: we don't want such tunables to be recomputed anymore upon memory 33*6546bc42SNadia Derbey * add/remove or ipc namespace creation/removal. 34*6546bc42SNadia Derbey * They can come back to a recomputable state by being set to a <0 value. 35*6546bc42SNadia Derbey */ 36*6546bc42SNadia Derbey static void tunable_set_callback(int val) 37*6546bc42SNadia Derbey { 38*6546bc42SNadia Derbey if (val >= 0) 39*6546bc42SNadia Derbey unregister_ipcns_notifier(current->nsproxy->ipc_ns); 40*6546bc42SNadia Derbey else { 41*6546bc42SNadia Derbey /* 42*6546bc42SNadia Derbey * Re-enable automatic recomputing only if not already 43*6546bc42SNadia Derbey * enabled. 44*6546bc42SNadia Derbey */ 45*6546bc42SNadia Derbey recompute_msgmni(current->nsproxy->ipc_ns); 46*6546bc42SNadia Derbey cond_register_ipcns_notifier(current->nsproxy->ipc_ns); 47*6546bc42SNadia Derbey } 48*6546bc42SNadia Derbey } 49*6546bc42SNadia Derbey 50a5494dcdSEric W. Biederman #ifdef CONFIG_PROC_FS 51a5494dcdSEric W. Biederman static int proc_ipc_dointvec(ctl_table *table, int write, struct file *filp, 52a5494dcdSEric W. Biederman void __user *buffer, size_t *lenp, loff_t *ppos) 53a5494dcdSEric W. Biederman { 54a5494dcdSEric W. Biederman struct ctl_table ipc_table; 55a5494dcdSEric W. Biederman memcpy(&ipc_table, table, sizeof(ipc_table)); 56a5494dcdSEric W. Biederman ipc_table.data = get_ipc(table); 57a5494dcdSEric W. Biederman 58a5494dcdSEric W. Biederman return proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos); 59a5494dcdSEric W. Biederman } 60a5494dcdSEric W. Biederman 6191cfb2b4SNadia Derbey static int proc_ipc_callback_dointvec(ctl_table *table, int write, 6291cfb2b4SNadia Derbey struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) 6391cfb2b4SNadia Derbey { 64*6546bc42SNadia Derbey struct ctl_table ipc_table; 6591cfb2b4SNadia Derbey size_t lenp_bef = *lenp; 6691cfb2b4SNadia Derbey int rc; 6791cfb2b4SNadia Derbey 68*6546bc42SNadia Derbey memcpy(&ipc_table, table, sizeof(ipc_table)); 69*6546bc42SNadia Derbey ipc_table.data = get_ipc(table); 70*6546bc42SNadia Derbey 71*6546bc42SNadia Derbey rc = proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos); 7291cfb2b4SNadia Derbey 7391cfb2b4SNadia Derbey if (write && !rc && lenp_bef == *lenp) 74*6546bc42SNadia Derbey tunable_set_callback(*((int *)(ipc_table.data))); 7591cfb2b4SNadia Derbey 7691cfb2b4SNadia Derbey return rc; 7791cfb2b4SNadia Derbey } 7891cfb2b4SNadia Derbey 79a5494dcdSEric W. Biederman static int proc_ipc_doulongvec_minmax(ctl_table *table, int write, 80a5494dcdSEric W. Biederman struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) 81a5494dcdSEric W. Biederman { 82a5494dcdSEric W. Biederman struct ctl_table ipc_table; 83a5494dcdSEric W. Biederman memcpy(&ipc_table, table, sizeof(ipc_table)); 84a5494dcdSEric W. Biederman ipc_table.data = get_ipc(table); 85a5494dcdSEric W. Biederman 86a5494dcdSEric W. Biederman return proc_doulongvec_minmax(&ipc_table, write, filp, buffer, 87a5494dcdSEric W. Biederman lenp, ppos); 88a5494dcdSEric W. Biederman } 89a5494dcdSEric W. Biederman 90a5494dcdSEric W. Biederman #else 91a5494dcdSEric W. Biederman #define proc_ipc_doulongvec_minmax NULL 92a5494dcdSEric W. Biederman #define proc_ipc_dointvec NULL 9391cfb2b4SNadia Derbey #define proc_ipc_callback_dointvec NULL 94a5494dcdSEric W. Biederman #endif 95a5494dcdSEric W. Biederman 96a5494dcdSEric W. Biederman #ifdef CONFIG_SYSCTL_SYSCALL 97a5494dcdSEric W. Biederman /* The generic sysctl ipc data routine. */ 98a5494dcdSEric W. Biederman static int sysctl_ipc_data(ctl_table *table, int __user *name, int nlen, 99a5494dcdSEric W. Biederman void __user *oldval, size_t __user *oldlenp, 100a5494dcdSEric W. Biederman void __user *newval, size_t newlen) 101a5494dcdSEric W. Biederman { 102a5494dcdSEric W. Biederman size_t len; 103a5494dcdSEric W. Biederman void *data; 104a5494dcdSEric W. Biederman 105a5494dcdSEric W. Biederman /* Get out of I don't have a variable */ 106a5494dcdSEric W. Biederman if (!table->data || !table->maxlen) 107a5494dcdSEric W. Biederman return -ENOTDIR; 108a5494dcdSEric W. Biederman 109a5494dcdSEric W. Biederman data = get_ipc(table); 110a5494dcdSEric W. Biederman if (!data) 111a5494dcdSEric W. Biederman return -ENOTDIR; 112a5494dcdSEric W. Biederman 113a5494dcdSEric W. Biederman if (oldval && oldlenp) { 114a5494dcdSEric W. Biederman if (get_user(len, oldlenp)) 115a5494dcdSEric W. Biederman return -EFAULT; 116a5494dcdSEric W. Biederman if (len) { 117a5494dcdSEric W. Biederman if (len > table->maxlen) 118a5494dcdSEric W. Biederman len = table->maxlen; 119a5494dcdSEric W. Biederman if (copy_to_user(oldval, data, len)) 120a5494dcdSEric W. Biederman return -EFAULT; 121a5494dcdSEric W. Biederman if (put_user(len, oldlenp)) 122a5494dcdSEric W. Biederman return -EFAULT; 123a5494dcdSEric W. Biederman } 124a5494dcdSEric W. Biederman } 125a5494dcdSEric W. Biederman 126a5494dcdSEric W. Biederman if (newval && newlen) { 127a5494dcdSEric W. Biederman if (newlen > table->maxlen) 128a5494dcdSEric W. Biederman newlen = table->maxlen; 129a5494dcdSEric W. Biederman 130a5494dcdSEric W. Biederman if (copy_from_user(data, newval, newlen)) 131a5494dcdSEric W. Biederman return -EFAULT; 132a5494dcdSEric W. Biederman } 133a5494dcdSEric W. Biederman return 1; 134a5494dcdSEric W. Biederman } 13591cfb2b4SNadia Derbey 13691cfb2b4SNadia Derbey static int sysctl_ipc_registered_data(ctl_table *table, int __user *name, 13791cfb2b4SNadia Derbey int nlen, void __user *oldval, size_t __user *oldlenp, 13891cfb2b4SNadia Derbey void __user *newval, size_t newlen) 13991cfb2b4SNadia Derbey { 14091cfb2b4SNadia Derbey int rc; 14191cfb2b4SNadia Derbey 14291cfb2b4SNadia Derbey rc = sysctl_ipc_data(table, name, nlen, oldval, oldlenp, newval, 14391cfb2b4SNadia Derbey newlen); 14491cfb2b4SNadia Derbey 145*6546bc42SNadia Derbey if (newval && newlen && rc > 0) { 14691cfb2b4SNadia Derbey /* 147*6546bc42SNadia Derbey * Tunable has successfully been changed from userland 14891cfb2b4SNadia Derbey */ 149*6546bc42SNadia Derbey int *data = get_ipc(table); 150*6546bc42SNadia Derbey 151*6546bc42SNadia Derbey tunable_set_callback(*data); 152*6546bc42SNadia Derbey } 15391cfb2b4SNadia Derbey 15491cfb2b4SNadia Derbey return rc; 15591cfb2b4SNadia Derbey } 156a5494dcdSEric W. Biederman #else 157a5494dcdSEric W. Biederman #define sysctl_ipc_data NULL 15891cfb2b4SNadia Derbey #define sysctl_ipc_registered_data NULL 159a5494dcdSEric W. Biederman #endif 160a5494dcdSEric W. Biederman 161a5494dcdSEric W. Biederman static struct ctl_table ipc_kern_table[] = { 162a5494dcdSEric W. Biederman { 163a5494dcdSEric W. Biederman .ctl_name = KERN_SHMMAX, 164a5494dcdSEric W. Biederman .procname = "shmmax", 165a5494dcdSEric W. Biederman .data = &init_ipc_ns.shm_ctlmax, 166a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.shm_ctlmax), 167a5494dcdSEric W. Biederman .mode = 0644, 168a5494dcdSEric W. Biederman .proc_handler = proc_ipc_doulongvec_minmax, 169a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 170a5494dcdSEric W. Biederman }, 171a5494dcdSEric W. Biederman { 172a5494dcdSEric W. Biederman .ctl_name = KERN_SHMALL, 173a5494dcdSEric W. Biederman .procname = "shmall", 174a5494dcdSEric W. Biederman .data = &init_ipc_ns.shm_ctlall, 175a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.shm_ctlall), 176a5494dcdSEric W. Biederman .mode = 0644, 177a5494dcdSEric W. Biederman .proc_handler = proc_ipc_doulongvec_minmax, 178a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 179a5494dcdSEric W. Biederman }, 180a5494dcdSEric W. Biederman { 181a5494dcdSEric W. Biederman .ctl_name = KERN_SHMMNI, 182a5494dcdSEric W. Biederman .procname = "shmmni", 183a5494dcdSEric W. Biederman .data = &init_ipc_ns.shm_ctlmni, 184a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.shm_ctlmni), 185a5494dcdSEric W. Biederman .mode = 0644, 186a5494dcdSEric W. Biederman .proc_handler = proc_ipc_dointvec, 187a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 188a5494dcdSEric W. Biederman }, 189a5494dcdSEric W. Biederman { 190a5494dcdSEric W. Biederman .ctl_name = KERN_MSGMAX, 191a5494dcdSEric W. Biederman .procname = "msgmax", 192a5494dcdSEric W. Biederman .data = &init_ipc_ns.msg_ctlmax, 193a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.msg_ctlmax), 194a5494dcdSEric W. Biederman .mode = 0644, 195a5494dcdSEric W. Biederman .proc_handler = proc_ipc_dointvec, 196a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 197a5494dcdSEric W. Biederman }, 198a5494dcdSEric W. Biederman { 199a5494dcdSEric W. Biederman .ctl_name = KERN_MSGMNI, 200a5494dcdSEric W. Biederman .procname = "msgmni", 201a5494dcdSEric W. Biederman .data = &init_ipc_ns.msg_ctlmni, 202a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.msg_ctlmni), 203a5494dcdSEric W. Biederman .mode = 0644, 20491cfb2b4SNadia Derbey .proc_handler = proc_ipc_callback_dointvec, 20591cfb2b4SNadia Derbey .strategy = sysctl_ipc_registered_data, 206a5494dcdSEric W. Biederman }, 207a5494dcdSEric W. Biederman { 208a5494dcdSEric W. Biederman .ctl_name = KERN_MSGMNB, 209a5494dcdSEric W. Biederman .procname = "msgmnb", 210a5494dcdSEric W. Biederman .data = &init_ipc_ns.msg_ctlmnb, 211a5494dcdSEric W. Biederman .maxlen = sizeof (init_ipc_ns.msg_ctlmnb), 212a5494dcdSEric W. Biederman .mode = 0644, 213a5494dcdSEric W. Biederman .proc_handler = proc_ipc_dointvec, 214a5494dcdSEric W. Biederman .strategy = sysctl_ipc_data, 215a5494dcdSEric W. Biederman }, 216a5494dcdSEric W. Biederman { 217a5494dcdSEric W. Biederman .ctl_name = KERN_SEM, 218a5494dcdSEric W. Biederman .procname = "sem", 219a5494dcdSEric W. Biederman .data = &init_ipc_ns.sem_ctls, 220a5494dcdSEric W. Biederman .maxlen = 4*sizeof (int), 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 }; 227a5494dcdSEric W. Biederman 228a5494dcdSEric W. Biederman static struct ctl_table ipc_root_table[] = { 229a5494dcdSEric W. Biederman { 230a5494dcdSEric W. Biederman .ctl_name = CTL_KERN, 231a5494dcdSEric W. Biederman .procname = "kernel", 232a5494dcdSEric W. Biederman .mode = 0555, 233a5494dcdSEric W. Biederman .child = ipc_kern_table, 234a5494dcdSEric W. Biederman }, 235a5494dcdSEric W. Biederman {} 236a5494dcdSEric W. Biederman }; 237a5494dcdSEric W. Biederman 238a5494dcdSEric W. Biederman static int __init ipc_sysctl_init(void) 239a5494dcdSEric W. Biederman { 2400b4d4147SEric W. Biederman register_sysctl_table(ipc_root_table); 241a5494dcdSEric W. Biederman return 0; 242a5494dcdSEric W. Biederman } 243a5494dcdSEric W. Biederman 244a5494dcdSEric W. Biederman __initcall(ipc_sysctl_init); 245