Skip to content

Commit c8bee43

Browse files
committed
seccomp: split filter prep from check and apply
In preparation for adding seccomp locking, move filter creation away from where it is checked and applied. This will allow for locking where no memory allocation is happening. The validation, filter attachment, and seccomp mode setting can all happen under the future locks. For extreme defensiveness, I've added a BUG_ON check for the calculated size of the buffer allocation in case BPF_MAXINSN ever changes, which shouldn't ever happen. The compiler should actually optimize out this check since the test above it makes it impossible. Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Oleg Nesterov <oleg@redhat.com> Reviewed-by: Andy Lutomirski <luto@amacapital.net>
1 parent 1d4457f commit c8bee43

1 file changed

Lines changed: 67 additions & 30 deletions

File tree

‎kernel/seccomp.c‎

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/compat.h>
1919
#include <linux/sched.h>
2020
#include <linux/seccomp.h>
21+
#include <linux/slab.h>
2122
#include <linux/syscalls.h>
2223

2324
/* #define SECCOMP_DEBUG 1 */
@@ -27,7 +28,6 @@
2728
#include <linux/filter.h>
2829
#include <linux/ptrace.h>
2930
#include <linux/security.h>
30-
#include <linux/slab.h>
3131
#include <linux/tracehook.h>
3232
#include <linux/uaccess.h>
3333

@@ -213,27 +213,23 @@ static inline void seccomp_assign_mode(unsigned long seccomp_mode)
213213

214214
#ifdef CONFIG_SECCOMP_FILTER
215215
/**
216-
* seccomp_attach_filter: Attaches a seccomp filter to current.
216+
* seccomp_prepare_filter: Prepares a seccomp filter for use.
217217
* @fprog: BPF program to install
218218
*
219-
* Returns 0 on success or an errno on failure.
219+
* Returns filter on success or an ERR_PTR on failure.
220220
*/
221-
static long seccomp_attach_filter(struct sock_fprog *fprog)
221+
static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
222222
{
223223
struct seccomp_filter *filter;
224-
unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
225-
unsigned long total_insns = fprog->len;
224+
unsigned long fp_size;
226225
struct sock_filter *fp;
227226
int new_len;
228227
long ret;
229228

230229
if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
231-
return -EINVAL;
232-
233-
for (filter = current->seccomp.filter; filter; filter = filter->prev)
234-
total_insns += filter->prog->len + 4; /* include a 4 instr penalty */
235-
if (total_insns > MAX_INSNS_PER_PATH)
236-
return -ENOMEM;
230+
return ERR_PTR(-EINVAL);
231+
BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
232+
fp_size = fprog->len * sizeof(struct sock_filter);
237233

238234
/*
239235
* Installing a seccomp filter requires that the task has
@@ -244,11 +240,11 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
244240
if (!task_no_new_privs(current) &&
245241
security_capable_noaudit(current_cred(), current_user_ns(),
246242
CAP_SYS_ADMIN) != 0)
247-
return -EACCES;
243+
return ERR_PTR(-EACCES);
248244

249245
fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
250246
if (!fp)
251-
return -ENOMEM;
247+
return ERR_PTR(-ENOMEM);
252248

253249
/* Copy the instructions from fprog. */
254250
ret = -EFAULT;
@@ -292,33 +288,28 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
292288

293289
sk_filter_select_runtime(filter->prog);
294290

295-
/*
296-
* If there is an existing filter, make it the prev and don't drop its
297-
* task reference.
298-
*/
299-
filter->prev = current->seccomp.filter;
300-
current->seccomp.filter = filter;
301-
return 0;
291+
return filter;
302292

303293
free_filter_prog:
304294
kfree(filter->prog);
305295
free_filter:
306296
kfree(filter);
307297
free_prog:
308298
kfree(fp);
309-
return ret;
299+
return ERR_PTR(ret);
310300
}
311301

312302
/**
313-
* seccomp_attach_user_filter - attaches a user-supplied sock_fprog
303+
* seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
314304
* @user_filter: pointer to the user data containing a sock_fprog.
315305
*
316306
* Returns 0 on success and non-zero otherwise.
317307
*/
318-
static long seccomp_attach_user_filter(const char __user *user_filter)
308+
static struct seccomp_filter *
309+
seccomp_prepare_user_filter(const char __user *user_filter)
319310
{
320311
struct sock_fprog fprog;
321-
long ret = -EFAULT;
312+
struct seccomp_filter *filter = ERR_PTR(-EFAULT);
322313

323314
#ifdef CONFIG_COMPAT
324315
if (is_compat_task()) {
@@ -331,9 +322,39 @@ static long seccomp_attach_user_filter(const char __user *user_filter)
331322
#endif
332323
if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
333324
goto out;
334-
ret = seccomp_attach_filter(&fprog);
325+
filter = seccomp_prepare_filter(&fprog);
335326
out:
336-
return ret;
327+
return filter;
328+
}
329+
330+
/**
331+
* seccomp_attach_filter: validate and attach filter
332+
* @flags: flags to change filter behavior
333+
* @filter: seccomp filter to add to the current process
334+
*
335+
* Returns 0 on success, -ve on error.
336+
*/
337+
static long seccomp_attach_filter(unsigned int flags,
338+
struct seccomp_filter *filter)
339+
{
340+
unsigned long total_insns;
341+
struct seccomp_filter *walker;
342+
343+
/* Validate resulting filter length. */
344+
total_insns = filter->prog->len;
345+
for (walker = current->seccomp.filter; walker; walker = walker->prev)
346+
total_insns += walker->prog->len + 4; /* 4 instr penalty */
347+
if (total_insns > MAX_INSNS_PER_PATH)
348+
return -ENOMEM;
349+
350+
/*
351+
* If there is an existing filter, make it the prev and don't drop its
352+
* task reference.
353+
*/
354+
filter->prev = current->seccomp.filter;
355+
current->seccomp.filter = filter;
356+
357+
return 0;
337358
}
338359

339360
/* get_seccomp_filter - increments the reference count of the filter on @tsk */
@@ -346,6 +367,14 @@ void get_seccomp_filter(struct task_struct *tsk)
346367
atomic_inc(&orig->usage);
347368
}
348369

370+
static inline void seccomp_filter_free(struct seccomp_filter *filter)
371+
{
372+
if (filter) {
373+
sk_filter_free(filter->prog);
374+
kfree(filter);
375+
}
376+
}
377+
349378
/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
350379
void put_seccomp_filter(struct task_struct *tsk)
351380
{
@@ -354,8 +383,7 @@ void put_seccomp_filter(struct task_struct *tsk)
354383
while (orig && atomic_dec_and_test(&orig->usage)) {
355384
struct seccomp_filter *freeme = orig;
356385
orig = orig->prev;
357-
sk_filter_free(freeme->prog);
358-
kfree(freeme);
386+
seccomp_filter_free(freeme);
359387
}
360388
}
361389

@@ -533,21 +561,30 @@ static long seccomp_set_mode_filter(unsigned int flags,
533561
const char __user *filter)
534562
{
535563
const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
564+
struct seccomp_filter *prepared = NULL;
536565
long ret = -EINVAL;
537566

538567
/* Validate flags. */
539568
if (flags != 0)
540569
goto out;
541570

571+
/* Prepare the new filter before holding any locks. */
572+
prepared = seccomp_prepare_user_filter(filter);
573+
if (IS_ERR(prepared))
574+
return PTR_ERR(prepared);
575+
542576
if (!seccomp_may_assign_mode(seccomp_mode))
543577
goto out;
544578

545-
ret = seccomp_attach_user_filter(filter);
579+
ret = seccomp_attach_filter(flags, prepared);
546580
if (ret)
547581
goto out;
582+
/* Do not free the successfully attached filter. */
583+
prepared = NULL;
548584

549585
seccomp_assign_mode(seccomp_mode);
550586
out:
587+
seccomp_filter_free(prepared);
551588
return ret;
552589
}
553590
#else

0 commit comments

Comments
 (0)