Skip to content

Commit 0889f44

Browse files
legionusebiederm
authored andcommitted
ipc: Check permissions for checkpoint_restart sysctls at open time
As Eric Biederman pointed out, it is possible not to use a custom proc_handler and check permissions for every write, but to use a .permission handler. That will allow the checkpoint_restart sysctls to perform all of their permission checks at open time, and not need any other special code. Link: https://lore.kernel.org/lkml/87czib9g38.fsf@email.froward.int.ebiederm.org/ Fixes: 1f5c135 ("ipc: Store ipc sysctls in the ipc namespace") Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: Alexey Gladkov <legion@kernel.org> Link: https://lkml.kernel.org/r/65fa8459803830608da4610a39f33c76aa933eb9.1651584847.git.legion@kernel.org Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
1 parent dd141a4 commit 0889f44

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

ipc/ipc_sysctl.c

+29-28
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,6 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,
6868
return ret;
6969
}
7070

71-
#ifdef CONFIG_CHECKPOINT_RESTORE
72-
static int proc_ipc_dointvec_minmax_checkpoint_restore(struct ctl_table *table,
73-
int write, void *buffer, size_t *lenp, loff_t *ppos)
74-
{
75-
struct ipc_namespace *ns = table->extra1;
76-
struct ctl_table ipc_table;
77-
78-
if (write && !checkpoint_restore_ns_capable(ns->user_ns))
79-
return -EPERM;
80-
81-
memcpy(&ipc_table, table, sizeof(ipc_table));
82-
83-
ipc_table.extra1 = SYSCTL_ZERO;
84-
ipc_table.extra2 = SYSCTL_INT_MAX;
85-
86-
return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
87-
}
88-
#endif
89-
9071
int ipc_mni = IPCMNI;
9172
int ipc_mni_shift = IPCMNI_SHIFT;
9273
int ipc_min_cycle = RADIX_TREE_MAP_SIZE;
@@ -172,22 +153,28 @@ static struct ctl_table ipc_sysctls[] = {
172153
.procname = "sem_next_id",
173154
.data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
174155
.maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
175-
.mode = 0666,
176-
.proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore,
156+
.mode = 0444,
157+
.proc_handler = proc_dointvec_minmax,
158+
.extra1 = SYSCTL_ZERO,
159+
.extra2 = SYSCTL_INT_MAX,
177160
},
178161
{
179162
.procname = "msg_next_id",
180163
.data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
181164
.maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
182-
.mode = 0666,
183-
.proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore,
165+
.mode = 0444,
166+
.proc_handler = proc_dointvec_minmax,
167+
.extra1 = SYSCTL_ZERO,
168+
.extra2 = SYSCTL_INT_MAX,
184169
},
185170
{
186171
.procname = "shm_next_id",
187172
.data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
188173
.maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
189-
.mode = 0666,
190-
.proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore,
174+
.mode = 0444,
175+
.proc_handler = proc_dointvec_minmax,
176+
.extra1 = SYSCTL_ZERO,
177+
.extra2 = SYSCTL_INT_MAX,
191178
},
192179
#endif
193180
{}
@@ -203,8 +190,25 @@ static int set_is_seen(struct ctl_table_set *set)
203190
return &current->nsproxy->ipc_ns->ipc_set == set;
204191
}
205192

193+
static int ipc_permissions(struct ctl_table_header *head, struct ctl_table *table)
194+
{
195+
int mode = table->mode;
196+
197+
#ifdef CONFIG_CHECKPOINT_RESTORE
198+
struct ipc_namespace *ns = current->nsproxy->ipc_ns;
199+
200+
if (((table->data == &ns->ids[IPC_SEM_IDS].next_id) ||
201+
(table->data == &ns->ids[IPC_MSG_IDS].next_id) ||
202+
(table->data == &ns->ids[IPC_SHM_IDS].next_id)) &&
203+
checkpoint_restore_ns_capable(ns->user_ns))
204+
mode = 0666;
205+
#endif
206+
return mode;
207+
}
208+
206209
static struct ctl_table_root set_root = {
207210
.lookup = set_lookup,
211+
.permissions = ipc_permissions,
208212
};
209213

210214
bool setup_ipc_sysctls(struct ipc_namespace *ns)
@@ -244,15 +248,12 @@ bool setup_ipc_sysctls(struct ipc_namespace *ns)
244248
#ifdef CONFIG_CHECKPOINT_RESTORE
245249
} else if (tbl[i].data == &init_ipc_ns.ids[IPC_SEM_IDS].next_id) {
246250
tbl[i].data = &ns->ids[IPC_SEM_IDS].next_id;
247-
tbl[i].extra1 = ns;
248251

249252
} else if (tbl[i].data == &init_ipc_ns.ids[IPC_MSG_IDS].next_id) {
250253
tbl[i].data = &ns->ids[IPC_MSG_IDS].next_id;
251-
tbl[i].extra1 = ns;
252254

253255
} else if (tbl[i].data == &init_ipc_ns.ids[IPC_SHM_IDS].next_id) {
254256
tbl[i].data = &ns->ids[IPC_SHM_IDS].next_id;
255-
tbl[i].extra1 = ns;
256257
#endif
257258
} else {
258259
tbl[i].data = NULL;

0 commit comments

Comments
 (0)