Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp337979ybm; Thu, 28 May 2020 04:12:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzb7lzHWky2LSYAKmhq5Cd8jb1Fid4kK7YaFbPEeM4YniIOh0EABhPvekheEf0M8wPf5m3V X-Received: by 2002:aa7:c944:: with SMTP id h4mr2422764edt.383.1590664378863; Thu, 28 May 2020 04:12:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590664378; cv=none; d=google.com; s=arc-20160816; b=EG1lzmIeI37df7ibpFpp2tbrsamWsPxsMRx3t9qzFsF696RFU7kqY+qobXTCtHcU9w R5mL/d9B8TN2VwrRFa8Cl8xrnFNYv30cZ7WxleCJFTDOjGbGq9i49NfbX3GoFOn6SoCG PSOgZbABEzWiaEiWdHvdOvCoHZ8+1gPftJ4AZLXMlqYIKdNTZ8i4CZD2Uu7DIkJIsIAK gl1kfQ1cv8BoA57JmL9Dzsf9mJm0dv9DbecE5ECJDbJ65FirUkllFVETHpUokPQx5dbC AO5rhNsy0FmLPbeWzYKQA+qfqAJq6n4MQ0SyKBNRWCJkvibNXj5U7QwcDj+Ef4v1gDAV 90fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BQVnG714Kn4j7kGsUMAy9Ya/MWVCVOEAKJXkZw2BpyE=; b=GREBvKLfMy36jVGxKm+InYc/JOaxKJ4EMQAtUHox0uxdAnk01ar2+5y2aVMwycE4P7 ySR18okeZDOBqYuicy4cavVtChiCMcTbHfRqie9R+BRS/nKbs5y9yvTevgB7SbfUDnwe ViGldXrLQgMUnMCivyriFrMdy/Qp2IK0ztBAsdXRDyO2xrV5tXNCHN5lIKS1rOnyUvBs jWZIuhDsS8g/wHrkqfBPVnFZxbctNHTLKADwidESSut1TIZpSv9pOc9rE6HrajgIibck KKLRk/TSbQYJ+lXvvYTDiuIxsmVPmRuVdDKWlO84M0eBRaAoMnmQVNTYx5x5/mato+mU aAqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b="K/pq77Jv"; 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 w17si3407505ejn.590.2020.05.28.04.12.35; Thu, 28 May 2020 04:12:58 -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; dkim=pass header.i=@sargun.me header.s=google header.b="K/pq77Jv"; 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 S2388165AbgE1LJ2 (ORCPT + 99 others); Thu, 28 May 2020 07:09:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388111AbgE1LJZ (ORCPT ); Thu, 28 May 2020 07:09:25 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C57DAC08C5C5 for ; Thu, 28 May 2020 04:09:24 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id n15so2980606pjt.4 for ; Thu, 28 May 2020 04:09:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BQVnG714Kn4j7kGsUMAy9Ya/MWVCVOEAKJXkZw2BpyE=; b=K/pq77Jvp7GPjybsFJjhftJfXp6Vy4i0MOjKAb7BXQP8k4eWloz5D2uq7aMoUCnB74 Esm2Q4yy/m4zB9nQ1BrVLqLEGjE7MS3mBXqJnCbRZ/3AVRhjiEjmLFhA4CNAJlW0NLGX HqR9XTswsQ72zmQ/mgbNn4FhO4SWKRP0nOPPM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BQVnG714Kn4j7kGsUMAy9Ya/MWVCVOEAKJXkZw2BpyE=; b=SgbvNSyEXW1PdYgcSXgpooIAouITfcisAf9uhUsEPvvlmBKxstqun+ywfLAZXcMEKC jo3X65Q4LhTJAsAUgQRepqK/rgbNVYcn+MbUsgAr0Wmfn3aMMdoMz6BNvTyDl2kc1p3z Q2p3aEi2Ovd60LKLKCwVRto04TbQJdaYkAAvKG6ORrBp1j9vZG0ehRxP1IPy190ucmM5 WOCNqByVl04d/n/DVqBuhB9xaS5tHBKnS/BN4vZ1I5Q130RXtSqD6eBbuTPMACAE22ZE tFwsw4S29Gd+lhNuyV/Dxkip2GeqeK74JBjSF2Oy/dmqEVzM0REe5+yOQdYE5xWwCv0t eFjg== X-Gm-Message-State: AOAM530Kx0W0vMwJOunRbhAhdckPebyEhBopLaMmX9MEoTuG10vbMg2z RCTVy/m4aC8yvEkQDng2ilcJjg== X-Received: by 2002:a17:902:d711:: with SMTP id w17mr3092593ply.122.1590664164095; Thu, 28 May 2020 04:09:24 -0700 (PDT) Received: from ubuntu.netflix.com (203.20.25.136.in-addr.arpa. [136.25.20.203]) by smtp.gmail.com with ESMTPSA id j26sm4565759pfr.215.2020.05.28.04.09.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2020 04:09:23 -0700 (PDT) From: Sargun Dhillon To: keescook@chromium.org Cc: Sargun Dhillon , christian.brauner@ubuntu.com, containers@lists.linux-foundation.org, cyphar@cyphar.com, jannh@google.com, jeffv@google.com, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, palmer@google.com, rsesek@google.com, tycho@tycho.ws, Matt Denton , Kees Cook Subject: [PATCH v2 1/3] seccomp: Add find_notification helper Date: Thu, 28 May 2020 04:08:56 -0700 Message-Id: <20200528110858.3265-2-sargun@sargun.me> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200528110858.3265-1-sargun@sargun.me> References: <20200528110858.3265-1-sargun@sargun.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 51 ++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/kernel/seccomp.c b/kernel/seccomp.c index 55a6184f5990..94ae4c7502cc 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; @@ -1101,7 +1110,7 @@ static long seccomp_notify_send(struct seccomp_filter *filter, void __user *buf) { struct seccomp_notif_resp resp = {}; - struct seccomp_knotif *knotif = NULL, *cur; + struct seccomp_knotif *knotif; long ret; if (copy_from_user(&resp, buf, sizeof(resp))) @@ -1118,13 +1127,7 @@ static long seccomp_notify_send(struct seccomp_filter *filter, if (ret < 0) return ret; - list_for_each_entry(cur, &filter->notif->notifications, list) { - if (cur->id == resp.id) { - knotif = cur; - break; - } - } - + knotif = find_notification(filter, resp.id); if (!knotif) { ret = -ENOENT; goto out; @@ -1150,7 +1153,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; @@ -1161,16 +1164,12 @@ static long seccomp_notify_id_valid(struct seccomp_filter *filter, if (ret < 0) 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; + else + ret = -ENOENT; -out: mutex_unlock(&filter->notify_lock); return ret; } -- 2.25.1