Blame view

ipc/ipc_sysctl.c 6.96 KB
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   *  Copyright (C) 2007
   *
   *  Author: Eric Biederman <ebiederm@xmision.com>
   *
   *  This program is free software; you can redistribute it and/or
   *  modify it under the terms of the GNU General Public License as
   *  published by the Free Software Foundation, version 2 of the
   *  License.
   */
  
  #include <linux/module.h>
  #include <linux/ipc.h>
  #include <linux/nsproxy.h>
  #include <linux/sysctl.h>
  #include <linux/uaccess.h>
ae5e1b22f   Pavel Emelyanov   namespaces: move ...
17
  #include <linux/ipc_namespace.h>
6546bc427   Nadia Derbey   ipc: re-enable ms...
18
19
  #include <linux/msg.h>
  #include "util.h"
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
20

a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
21
22
23
24
25
26
27
  static void *get_ipc(ctl_table *table)
  {
  	char *which = table->data;
  	struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  	which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns;
  	return which;
  }
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
28

6546bc427   Nadia Derbey   ipc: re-enable ms...
29
  /*
9eefe520c   Nadia Derbey   ipc: do not use a...
30
31
32
33
34
35
36
   * Routine that is called when the file "auto_msgmni" has successfully been
   * written.
   * Two values are allowed:
   * 0: unregister msgmni's callback routine from the ipc namespace notifier
   *    chain. This means that msgmni won't be recomputed anymore upon memory
   *    add/remove or ipc namespace creation/removal.
   * 1: register back the callback routine.
6546bc427   Nadia Derbey   ipc: re-enable ms...
37
   */
9eefe520c   Nadia Derbey   ipc: do not use a...
38
  static void ipc_auto_callback(int val)
6546bc427   Nadia Derbey   ipc: re-enable ms...
39
  {
9eefe520c   Nadia Derbey   ipc: do not use a...
40
  	if (!val)
6546bc427   Nadia Derbey   ipc: re-enable ms...
41
42
43
44
45
46
47
48
49
50
  		unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  	else {
  		/*
  		 * Re-enable automatic recomputing only if not already
  		 * enabled.
  		 */
  		recompute_msgmni(current->nsproxy->ipc_ns);
  		cond_register_ipcns_notifier(current->nsproxy->ipc_ns);
  	}
  }
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
51
52
53
54
55
56
57
58
59
60
  #ifdef CONFIG_PROC_FS
  static int proc_ipc_dointvec(ctl_table *table, int write, struct file *filp,
  	void __user *buffer, size_t *lenp, loff_t *ppos)
  {
  	struct ctl_table ipc_table;
  	memcpy(&ipc_table, table, sizeof(ipc_table));
  	ipc_table.data = get_ipc(table);
  
  	return proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
  }
91cfb2b4b   Nadia Derbey   ipc: do not recom...
61
62
63
  static int proc_ipc_callback_dointvec(ctl_table *table, int write,
  	struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  {
6546bc427   Nadia Derbey   ipc: re-enable ms...
64
  	struct ctl_table ipc_table;
91cfb2b4b   Nadia Derbey   ipc: do not recom...
65
66
  	size_t lenp_bef = *lenp;
  	int rc;
6546bc427   Nadia Derbey   ipc: re-enable ms...
67
68
69
70
  	memcpy(&ipc_table, table, sizeof(ipc_table));
  	ipc_table.data = get_ipc(table);
  
  	rc = proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
91cfb2b4b   Nadia Derbey   ipc: do not recom...
71
72
  
  	if (write && !rc && lenp_bef == *lenp)
9eefe520c   Nadia Derbey   ipc: do not use a...
73
74
75
76
77
78
  		/*
  		 * Tunable has successfully been changed by hand. Disable its
  		 * automatic adjustment. This simply requires unregistering
  		 * the notifiers that trigger recalculation.
  		 */
  		unregister_ipcns_notifier(current->nsproxy->ipc_ns);
91cfb2b4b   Nadia Derbey   ipc: do not recom...
79
80
81
  
  	return rc;
  }
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
82
83
84
85
86
87
88
89
90
91
  static int proc_ipc_doulongvec_minmax(ctl_table *table, int write,
  	struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  {
  	struct ctl_table ipc_table;
  	memcpy(&ipc_table, table, sizeof(ipc_table));
  	ipc_table.data = get_ipc(table);
  
  	return proc_doulongvec_minmax(&ipc_table, write, filp, buffer,
  					lenp, ppos);
  }
9eefe520c   Nadia Derbey   ipc: do not use a...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  static int proc_ipcauto_dointvec_minmax(ctl_table *table, int write,
  	struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  {
  	struct ctl_table ipc_table;
  	size_t lenp_bef = *lenp;
  	int oldval;
  	int rc;
  
  	memcpy(&ipc_table, table, sizeof(ipc_table));
  	ipc_table.data = get_ipc(table);
  	oldval = *((int *)(ipc_table.data));
  
  	rc = proc_dointvec_minmax(&ipc_table, write, filp, buffer, lenp, ppos);
  
  	if (write && !rc && lenp_bef == *lenp) {
  		int newval = *((int *)(ipc_table.data));
  		/*
  		 * The file "auto_msgmni" has correctly been set.
  		 * React by (un)registering the corresponding tunable, if the
  		 * value has changed.
  		 */
  		if (newval != oldval)
  			ipc_auto_callback(newval);
  	}
  
  	return rc;
  }
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
119
120
121
  #else
  #define proc_ipc_doulongvec_minmax NULL
  #define proc_ipc_dointvec	   NULL
91cfb2b4b   Nadia Derbey   ipc: do not recom...
122
  #define proc_ipc_callback_dointvec NULL
9eefe520c   Nadia Derbey   ipc: do not use a...
123
  #define proc_ipcauto_dointvec_minmax NULL
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  #endif
  
  #ifdef CONFIG_SYSCTL_SYSCALL
  /* The generic sysctl ipc data routine. */
  static int sysctl_ipc_data(ctl_table *table, int __user *name, int nlen,
  		void __user *oldval, size_t __user *oldlenp,
  		void __user *newval, size_t newlen)
  {
  	size_t len;
  	void *data;
  
  	/* Get out of I don't have a variable */
  	if (!table->data || !table->maxlen)
  		return -ENOTDIR;
  
  	data = get_ipc(table);
  	if (!data)
  		return -ENOTDIR;
  
  	if (oldval && oldlenp) {
  		if (get_user(len, oldlenp))
  			return -EFAULT;
  		if (len) {
  			if (len > table->maxlen)
  				len = table->maxlen;
  			if (copy_to_user(oldval, data, len))
  				return -EFAULT;
  			if (put_user(len, oldlenp))
  				return -EFAULT;
  		}
  	}
  
  	if (newval && newlen) {
  		if (newlen > table->maxlen)
  			newlen = table->maxlen;
  
  		if (copy_from_user(data, newval, newlen))
  			return -EFAULT;
  	}
  	return 1;
  }
91cfb2b4b   Nadia Derbey   ipc: do not recom...
165
166
167
168
169
170
171
172
173
  
  static int sysctl_ipc_registered_data(ctl_table *table, int __user *name,
  		int nlen, void __user *oldval, size_t __user *oldlenp,
  		void __user *newval, size_t newlen)
  {
  	int rc;
  
  	rc = sysctl_ipc_data(table, name, nlen, oldval, oldlenp, newval,
  		newlen);
9eefe520c   Nadia Derbey   ipc: do not use a...
174
  	if (newval && newlen && rc > 0)
91cfb2b4b   Nadia Derbey   ipc: do not recom...
175
  		/*
6546bc427   Nadia Derbey   ipc: re-enable ms...
176
  		 * Tunable has successfully been changed from userland
91cfb2b4b   Nadia Derbey   ipc: do not recom...
177
  		 */
9eefe520c   Nadia Derbey   ipc: do not use a...
178
  		unregister_ipcns_notifier(current->nsproxy->ipc_ns);
91cfb2b4b   Nadia Derbey   ipc: do not recom...
179
180
181
  
  	return rc;
  }
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
182
183
  #else
  #define sysctl_ipc_data NULL
91cfb2b4b   Nadia Derbey   ipc: do not recom...
184
  #define sysctl_ipc_registered_data NULL
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
185
  #endif
9eefe520c   Nadia Derbey   ipc: do not use a...
186
187
  static int zero;
  static int one = 1;
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  static struct ctl_table ipc_kern_table[] = {
  	{
  		.ctl_name	= KERN_SHMMAX,
  		.procname	= "shmmax",
  		.data		= &init_ipc_ns.shm_ctlmax,
  		.maxlen		= sizeof (init_ipc_ns.shm_ctlmax),
  		.mode		= 0644,
  		.proc_handler	= proc_ipc_doulongvec_minmax,
  		.strategy	= sysctl_ipc_data,
  	},
  	{
  		.ctl_name	= KERN_SHMALL,
  		.procname	= "shmall",
  		.data		= &init_ipc_ns.shm_ctlall,
  		.maxlen		= sizeof (init_ipc_ns.shm_ctlall),
  		.mode		= 0644,
  		.proc_handler	= proc_ipc_doulongvec_minmax,
  		.strategy	= sysctl_ipc_data,
  	},
  	{
  		.ctl_name	= KERN_SHMMNI,
  		.procname	= "shmmni",
  		.data		= &init_ipc_ns.shm_ctlmni,
  		.maxlen		= sizeof (init_ipc_ns.shm_ctlmni),
  		.mode		= 0644,
  		.proc_handler	= proc_ipc_dointvec,
  		.strategy	= sysctl_ipc_data,
  	},
  	{
  		.ctl_name	= KERN_MSGMAX,
  		.procname	= "msgmax",
  		.data		= &init_ipc_ns.msg_ctlmax,
  		.maxlen		= sizeof (init_ipc_ns.msg_ctlmax),
  		.mode		= 0644,
  		.proc_handler	= proc_ipc_dointvec,
  		.strategy	= sysctl_ipc_data,
  	},
  	{
  		.ctl_name	= KERN_MSGMNI,
  		.procname	= "msgmni",
  		.data		= &init_ipc_ns.msg_ctlmni,
  		.maxlen		= sizeof (init_ipc_ns.msg_ctlmni),
  		.mode		= 0644,
91cfb2b4b   Nadia Derbey   ipc: do not recom...
231
232
  		.proc_handler	= proc_ipc_callback_dointvec,
  		.strategy	= sysctl_ipc_registered_data,
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  	},
  	{
  		.ctl_name	= KERN_MSGMNB,
  		.procname	=  "msgmnb",
  		.data		= &init_ipc_ns.msg_ctlmnb,
  		.maxlen		= sizeof (init_ipc_ns.msg_ctlmnb),
  		.mode		= 0644,
  		.proc_handler	= proc_ipc_dointvec,
  		.strategy	= sysctl_ipc_data,
  	},
  	{
  		.ctl_name	= KERN_SEM,
  		.procname	= "sem",
  		.data		= &init_ipc_ns.sem_ctls,
  		.maxlen		= 4*sizeof (int),
  		.mode		= 0644,
  		.proc_handler	= proc_ipc_dointvec,
  		.strategy	= sysctl_ipc_data,
  	},
9eefe520c   Nadia Derbey   ipc: do not use a...
252
253
254
255
256
257
258
259
260
261
  	{
  		.ctl_name	= CTL_UNNUMBERED,
  		.procname	= "auto_msgmni",
  		.data		= &init_ipc_ns.auto_msgmni,
  		.maxlen		= sizeof(int),
  		.mode		= 0644,
  		.proc_handler	= proc_ipcauto_dointvec_minmax,
  		.extra1		= &zero,
  		.extra2		= &one,
  	},
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  	{}
  };
  
  static struct ctl_table ipc_root_table[] = {
  	{
  		.ctl_name	= CTL_KERN,
  		.procname	= "kernel",
  		.mode		= 0555,
  		.child		= ipc_kern_table,
  	},
  	{}
  };
  
  static int __init ipc_sysctl_init(void)
  {
0b4d41471   Eric W. Biederman   [PATCH] sysctl: r...
277
  	register_sysctl_table(ipc_root_table);
a5494dcd8   Eric W. Biederman   [PATCH] sysctl: m...
278
279
280
281
  	return 0;
  }
  
  __initcall(ipc_sysctl_init);