Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751621AbdITSgd (ORCPT ); Wed, 20 Sep 2017 14:36:33 -0400 Received: from mail-it0-f51.google.com ([209.85.214.51]:50392 "EHLO mail-it0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751436AbdITSgc (ORCPT ); Wed, 20 Sep 2017 14:36:32 -0400 X-Google-Smtp-Source: AOwi7QBkts4MkZKsQ4Yj/XeKp1R6MIJ9aP8vPZ/TEkCgfZ37HYQOWgmP2nI+wbtvf+6cpDbQDTdHS9RmxfO6SjEHgXM= MIME-Version: 1.0 In-Reply-To: <20170920125621.GA3599@redhat.com> References: <20170920125621.GA3599@redhat.com> From: Kees Cook Date: Wed, 20 Sep 2017 11:36:30 -0700 X-Google-Sender-Auth: dh2oTohwMFXKl6YfgaiNiG4d9HI Message-ID: Subject: Re: [PATCH] seccomp: fix the usage of get/put_seccomp_filter() in seccomp_get_filter() To: Oleg Nesterov Cc: Chris Salls , Andy Lutomirski , Will Drewry , "security@kernel.org" , LKML , Tycho Andersen Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2427 Lines: 67 On Wed, Sep 20, 2017 at 5:56 AM, Oleg Nesterov wrote: > As Chris explains, get_seccomp_filter() and put_seccomp_filter() can > use the different filters, once we drop ->siglock task->seccomp.filter > can be replaced by SECCOMP_FILTER_FLAG_TSYNC. > > Fixes: f8e529ed941b ("seccomp, ptrace: add support for dumping seccomp filters") > Reported-by: Chris Salls > Cc: stable@vger.kernel.org > Signed-off-by: Oleg Nesterov > --- > kernel/seccomp.c | 14 +++++++++----- > 1 file changed, 9 insertions(+), 5 deletions(-) > > diff --git a/kernel/seccomp.c b/kernel/seccomp.c > index 98b59b5..897f153 100644 > --- a/kernel/seccomp.c > +++ b/kernel/seccomp.c > @@ -476,10 +476,8 @@ static inline void seccomp_filter_free(struct seccomp_filter *filter) > } > } > > -/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ > -void put_seccomp_filter(struct task_struct *tsk) > +static void __put_seccomp_filter(struct seccomp_filter *orig) > { > - struct seccomp_filter *orig = tsk->seccomp.filter; > /* Clean up single-reference branches iteratively. */ > while (orig && refcount_dec_and_test(&orig->usage)) { > struct seccomp_filter *freeme = orig; > @@ -488,6 +486,12 @@ void put_seccomp_filter(struct task_struct *tsk) > } > } > > +/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ > +void put_seccomp_filter(struct task_struct *tsk) > +{ > + __put_seccomp_filter(tsk->seccomp.filter); > +} > + > static void seccomp_init_siginfo(siginfo_t *info, int syscall, int reason) > { > memset(info, 0, sizeof(*info)); > @@ -908,13 +912,13 @@ long seccomp_get_filter(struct task_struct *task, unsigned long filter_off, > if (!data) > goto out; > > - get_seccomp_filter(task); > + refcount_inc(&filter->usage); > spin_unlock_irq(&task->sighand->siglock); > > if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog))) > ret = -EFAULT; > > - put_seccomp_filter(task); > + __put_seccomp_filter(filter); > return ret; Given how reference counting is done for filters, I'd be happier with leaving the get_seccomp_filter() as-is, and providing the __put_seccomp_filter() as the only change here (i.e. don't open-code the refcount_inc()). -Kees -- Kees Cook Pixel Security