ipc_sysctl.c 5.37 KB
Newer Older
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>
17
#include <linux/ipc_namespace.h>
18 19
#include <linux/msg.h>
#include "util.h"
20

21
static void *get_ipc(struct ctl_table *table)
22 23 24 25 26 27 28
{
	char *which = table->data;
	struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
	which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns;
	return which;
}

29
#ifdef CONFIG_PROC_SYSCTL
30
static int proc_ipc_dointvec(struct ctl_table *table, int write,
31 32 33
	void __user *buffer, size_t *lenp, loff_t *ppos)
{
	struct ctl_table ipc_table;
34

35 36 37
	memcpy(&ipc_table, table, sizeof(ipc_table));
	ipc_table.data = get_ipc(table);

38
	return proc_dointvec(&ipc_table, write, buffer, lenp, ppos);
39 40
}

41
static int proc_ipc_dointvec_minmax(struct ctl_table *table, int write,
42 43 44 45 46 47 48 49 50 51
	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_minmax(&ipc_table, write, buffer, lenp, ppos);
}

52
static int proc_ipc_dointvec_minmax_orphans(struct ctl_table *table, int write,
53 54 55 56 57 58 59 60 61 62 63 64
	void __user *buffer, size_t *lenp, loff_t *ppos)
{
	struct ipc_namespace *ns = current->nsproxy->ipc_ns;
	int err = proc_ipc_dointvec_minmax(table, write, buffer, lenp, ppos);

	if (err < 0)
		return err;
	if (ns->shm_rmid_forced)
		shm_destroy_orphaned(ns);
	return err;
}

65
static int proc_ipc_doulongvec_minmax(struct ctl_table *table, int write,
66
	void __user *buffer, size_t *lenp, loff_t *ppos)
67 68 69 70 71
{
	struct ctl_table ipc_table;
	memcpy(&ipc_table, table, sizeof(ipc_table));
	ipc_table.data = get_ipc(table);

72
	return proc_doulongvec_minmax(&ipc_table, write, buffer,
73 74 75
					lenp, ppos);
}

76
static int proc_ipc_auto_msgmni(struct ctl_table *table, int write,
77
	void __user *buffer, size_t *lenp, loff_t *ppos)
78 79
{
	struct ctl_table ipc_table;
80
	int dummy = 0;
81 82

	memcpy(&ipc_table, table, sizeof(ipc_table));
83 84 85 86 87 88
	ipc_table.data = &dummy;

	if (write)
		pr_info_once("writing to auto_msgmni has no effect");

	return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
89 90
}

91 92 93
#else
#define proc_ipc_doulongvec_minmax NULL
#define proc_ipc_dointvec	   NULL
94 95
#define proc_ipc_dointvec_minmax   NULL
#define proc_ipc_dointvec_minmax_orphans   NULL
96
#define proc_ipc_auto_msgmni	   NULL
97 98
#endif

99 100
static int zero;
static int one = 1;
101
static int int_max = INT_MAX;
102

103 104 105 106
static struct ctl_table ipc_kern_table[] = {
	{
		.procname	= "shmmax",
		.data		= &init_ipc_ns.shm_ctlmax,
Manfred Spraul's avatar
Manfred Spraul committed
107
		.maxlen		= sizeof(init_ipc_ns.shm_ctlmax),
108 109 110 111 112 113
		.mode		= 0644,
		.proc_handler	= proc_ipc_doulongvec_minmax,
	},
	{
		.procname	= "shmall",
		.data		= &init_ipc_ns.shm_ctlall,
Manfred Spraul's avatar
Manfred Spraul committed
114
		.maxlen		= sizeof(init_ipc_ns.shm_ctlall),
115 116 117 118 119 120
		.mode		= 0644,
		.proc_handler	= proc_ipc_doulongvec_minmax,
	},
	{
		.procname	= "shmmni",
		.data		= &init_ipc_ns.shm_ctlmni,
Manfred Spraul's avatar
Manfred Spraul committed
121
		.maxlen		= sizeof(init_ipc_ns.shm_ctlmni),
122 123 124
		.mode		= 0644,
		.proc_handler	= proc_ipc_dointvec,
	},
125 126 127 128 129 130 131 132 133
	{
		.procname	= "shm_rmid_forced",
		.data		= &init_ipc_ns.shm_rmid_forced,
		.maxlen		= sizeof(init_ipc_ns.shm_rmid_forced),
		.mode		= 0644,
		.proc_handler	= proc_ipc_dointvec_minmax_orphans,
		.extra1		= &zero,
		.extra2		= &one,
	},
134 135 136
	{
		.procname	= "msgmax",
		.data		= &init_ipc_ns.msg_ctlmax,
Manfred Spraul's avatar
Manfred Spraul committed
137
		.maxlen		= sizeof(init_ipc_ns.msg_ctlmax),
138
		.mode		= 0644,
139 140 141
		.proc_handler	= proc_ipc_dointvec_minmax,
		.extra1		= &zero,
		.extra2		= &int_max,
142 143 144 145
	},
	{
		.procname	= "msgmni",
		.data		= &init_ipc_ns.msg_ctlmni,
Manfred Spraul's avatar
Manfred Spraul committed
146
		.maxlen		= sizeof(init_ipc_ns.msg_ctlmni),
147
		.mode		= 0644,
148
		.proc_handler	= proc_ipc_dointvec_minmax,
149 150
		.extra1		= &zero,
		.extra2		= &int_max,
151
	},
152 153 154 155 156 157 158 159 160
	{
		.procname	= "auto_msgmni",
		.data		= NULL,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_ipc_auto_msgmni,
		.extra1		= &zero,
		.extra2		= &one,
	},
161 162 163
	{
		.procname	=  "msgmnb",
		.data		= &init_ipc_ns.msg_ctlmnb,
Manfred Spraul's avatar
Manfred Spraul committed
164
		.maxlen		= sizeof(init_ipc_ns.msg_ctlmnb),
165
		.mode		= 0644,
166 167 168
		.proc_handler	= proc_ipc_dointvec_minmax,
		.extra1		= &zero,
		.extra2		= &int_max,
169 170 171 172
	},
	{
		.procname	= "sem",
		.data		= &init_ipc_ns.sem_ctls,
Manfred Spraul's avatar
Manfred Spraul committed
173
		.maxlen		= 4*sizeof(int),
174 175 176
		.mode		= 0644,
		.proc_handler	= proc_ipc_dointvec,
	},
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
#ifdef CONFIG_CHECKPOINT_RESTORE
	{
		.procname	= "sem_next_id",
		.data		= &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
		.mode		= 0644,
		.proc_handler	= proc_ipc_dointvec_minmax,
		.extra1		= &zero,
		.extra2		= &int_max,
	},
	{
		.procname	= "msg_next_id",
		.data		= &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
		.maxlen		= sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
		.mode		= 0644,
		.proc_handler	= proc_ipc_dointvec_minmax,
		.extra1		= &zero,
		.extra2		= &int_max,
	},
	{
		.procname	= "shm_next_id",
		.data		= &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
		.mode		= 0644,
		.proc_handler	= proc_ipc_dointvec_minmax,
		.extra1		= &zero,
		.extra2		= &int_max,
	},
#endif
206 207 208 209 210 211 212 213 214 215 216 217 218 219
	{}
};

static struct ctl_table ipc_root_table[] = {
	{
		.procname	= "kernel",
		.mode		= 0555,
		.child		= ipc_kern_table,
	},
	{}
};

static int __init ipc_sysctl_init(void)
{
220
	register_sysctl_table(ipc_root_table);
221 222 223
	return 0;
}

224
device_initcall(ipc_sysctl_init);