Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp1067409ybs; Mon, 25 May 2020 06:29:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzfHcAjWiB9wmauBv+HO8YXLJdZmu1Cb98NB7pT2NWxgrHgJi8dg4Hq+BiSK9BAhNYhppzF X-Received: by 2002:a17:906:29d3:: with SMTP id y19mr18508962eje.258.1590413377819; Mon, 25 May 2020 06:29:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590413377; cv=none; d=google.com; s=arc-20160816; b=NNarasw1K+x226x6L0l0ljQQfhl6InqqIqUYDUE3hDNVMFUaUAHi7QfmcLhN9CeOog CaLbUifFIbOrPBZTmaE4apU/tGN3tzfVn/yhavP/j8P6BSZMi7OqNjcjyJbLd8hWa83h o+D1FWpoSCdI7XV4jKzVqxZHGTg3oqTMY9OMLlsUFUUHE6fwv/nQU3Pi9zIVhfxyNmaB Uax+jxP1WjVgzs3lsF1wIzkU5zdrdiqe/K5QdnsG18JCkSMospB4RiGzCGxPUDKAzHmj Zm5obEsWfSp597J7jsnbibTzcAOVJnOrIQheSSUOcHkVXoLMAtthtQ/vbn1yUZ4BAZoI a9aA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=UrbRF2U+06nx8LrfsyQAPSXUnJvqdWgxBV3/ojNYqx0=; b=iLBuBpgkoN2ohqqNH8/uOEuWy6zzU/a9YLSWJmHacXuFuI6Nf83kXe92yaeQtMFxpM 32dckvaSL2MU5fB3CVtz2tuZ5XUqD0wH9eIu2CG6o6WPvX4uVNOsktVU16/RG9rhr5Qq 3pMP4e1HIiaZu6EUp76bP3yJmvhwVF+lGSjUGPJro0dfIsaV5r3/i6tFy3prxW5cVwK3 p152RQUkq2Wrfk/z0TYsCGm7BQyMXyQRvrQpUAqL2ekCJMPX4oIXfO387lLQ41dh3+DO ReuN/1ZFcAJm/D+pHTJDkTDa4gFTdH90MJJZ4YLYCkBdPkNiYH8kHy+VJjj/4/gKGXjl Pbdg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r20si9242482ejx.622.2020.05.25.06.29.14; Mon, 25 May 2020 06:29:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390807AbgEYN0R (ORCPT + 99 others); Mon, 25 May 2020 09:26:17 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:37096 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388794AbgEYN0Q (ORCPT ); Mon, 25 May 2020 09:26:16 -0400 Received: from ip5f5af183.dynamic.kabel-deutschland.de ([95.90.241.131] helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jdD7M-0001k3-Eu; Mon, 25 May 2020 13:26:12 +0000 Date: Mon, 25 May 2020 15:26:11 +0200 From: Christian Brauner To: Sargun Dhillon Cc: linux-kernel@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org, tycho@tycho.ws, keescook@chromium.org, cyphar@cyphar.com, Jeffrey Vander Stoep , jannh@google.com, rsesek@google.com, palmer@google.com, Matt Denton , Kees Cook Subject: Re: [PATCH 1/5] seccomp: Add find_notification helper Message-ID: <20200525132611.ltpkbe6ditxkag2r@wittgenstein> References: <20200524233942.8702-1-sargun@sargun.me> <20200524233942.8702-2-sargun@sargun.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200524233942.8702-2-sargun@sargun.me> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, May 24, 2020 at 04:39:38PM -0700, Sargun Dhillon wrote: > This adds a helper which can iterate through a seccomp_filter to > find a notification matching an ID. It removes several replicated > chunks of code. > > Signed-off-by: Sargun Dhillon > Cc: Matt Denton > Cc: Kees Cook , > Cc: Jann Horn , > Cc: Robert Sesek , > Cc: Chris Palmer > Cc: Christian Brauner > Cc: Tycho Andersen > --- > kernel/seccomp.c | 38 +++++++++++++++++++++----------------- > 1 file changed, 21 insertions(+), 17 deletions(-) > > diff --git a/kernel/seccomp.c b/kernel/seccomp.c > index 55a6184f5990..f6ce94b7a167 100644 > --- a/kernel/seccomp.c > +++ b/kernel/seccomp.c > @@ -1021,10 +1021,25 @@ static int seccomp_notify_release(struct inode *inode, struct file *file) > return 0; > } > > +/* must be called with notif_lock held */ > +static inline struct seccomp_knotif * > +find_notification(struct seccomp_filter *filter, u64 id) > +{ > + struct seccomp_knotif *cur; > + > + list_for_each_entry(cur, &filter->notif->notifications, list) { > + if (cur->id == id) > + return cur; > + } > + > + return NULL; > +} > + > + > static long seccomp_notify_recv(struct seccomp_filter *filter, > void __user *buf) > { > - struct seccomp_knotif *knotif = NULL, *cur; > + struct seccomp_knotif *knotif, *cur; > struct seccomp_notif unotif; > ssize_t ret; > > @@ -1078,14 +1093,8 @@ static long seccomp_notify_recv(struct seccomp_filter *filter, > * may have died when we released the lock, so we need to make > * sure it's still around. > */ > - knotif = NULL; > mutex_lock(&filter->notify_lock); > - list_for_each_entry(cur, &filter->notif->notifications, list) { > - if (cur->id == unotif.id) { > - knotif = cur; > - break; > - } > - } > + knotif = find_notification(filter, unotif.id); > > if (knotif) { > knotif->state = SECCOMP_NOTIFY_INIT; > @@ -1150,7 +1159,7 @@ static long seccomp_notify_send(struct seccomp_filter *filter, > static long seccomp_notify_id_valid(struct seccomp_filter *filter, > void __user *buf) > { > - struct seccomp_knotif *knotif = NULL; > + struct seccomp_knotif *knotif; > u64 id; > long ret; > > @@ -1162,15 +1171,10 @@ static long seccomp_notify_id_valid(struct seccomp_filter *filter, > return ret; > > ret = -ENOENT; > - list_for_each_entry(knotif, &filter->notif->notifications, list) { > - if (knotif->id == id) { > - if (knotif->state == SECCOMP_NOTIFY_SENT) > - ret = 0; > - goto out; > - } > - } > + knotif = find_notification(filter, id); > + if (knotif && knotif->state == SECCOMP_NOTIFY_SENT) > + ret = 0; Coul be a little nicer to have this be: if (knotif && knotif->state == SECCOMP_NOTIFY_SENT) ret = 0; else ret = -ENOENT; or, if you want to keep the assignment out of the lock: ret = -ENOENT; ret = mutex_lock_interruptible(&filter->notify_lock); if (ret < 0) return ret; knotif = find_notification(filter, id); if (knotif && knotif->state == SECCOMP_NOTIFY_SENT) ret = 0; otherwise looks like a good cleanup to me.