Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp641053ybs; Sun, 24 May 2020 16:42:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxF2glkVYcPZMmQqO4jVpdWOfNwIMud9QuFiDE39MoNNAqcZTL6IpsXZRrn2EJxAIENV8Ln X-Received: by 2002:a17:906:f74c:: with SMTP id jp12mr17394044ejb.490.1590363778803; Sun, 24 May 2020 16:42:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590363778; cv=none; d=google.com; s=arc-20160816; b=Aneewi6K2WXOKBasS/wdEeFnfowjv+jM5M5FMoLA9g+mrPZCO6FazId9hduzrYEIzJ 0iHkV8aIv5qISga9BYWIOJaWX08OJLN4aVIfvJjDwKEFduDAbrYlBtcrVhSxX3g8j+Yt LmxDJxksrJPRZHNtGmVH1Z5if2UCSMoBVvWaVNtX8v1viCzqWcNZEDlYME8rs0PVRH/R KRBFOukVkC2MW1VrZvYDh2UfK0xCnjAQzZ5gcAZVN++tu6SEq33dc32cxSOLFzzLZDAX RjP2H36Xy2FOVyxuLe1v7fuQFBSJVNSh2J7nWRWEY0Nj8BNOjAC+FvVpmyq4HNhU6j9Z 1qqw== 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=xTPagZOTK2S3jx+U1S0sGHfFCrpHZOdN2FHjkt5W9h0=; b=vQGarh6bnVhpWDBMc1zah3716W9d5dltNhgNHoXYeUTy91MEWeMEzGThB9XaoI0j+M 4MxrowwlABDZ0CBOMWSEcB6WS6dFxNF3hulWhPpQm7e5Yjd+eTb6xmmPDAHUxQ7olkDq Xu7CB9ZPU/UFddCvgVf8R7KFc+XVL7R6rjUnDRBYl1yMSulMHy+ryRj8H9ErxdmLTD3l rihuzJrHfHvaOWodsD43ienK1jm4YVeNzLHi8iarumWRZcIFtJqodHbxoetmE2PkuU56 3MKshbRpkRUxJsc+7Db8+A5/9s/ceQ/PvbowwVB/xgnfhGoJeiu01aQVHA3ib0lXN003 TGtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=rONxcso2; 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 f3si8173017eds.389.2020.05.24.16.42.36; Sun, 24 May 2020 16:42: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=rONxcso2; 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 S2388609AbgEXXju (ORCPT + 99 others); Sun, 24 May 2020 19:39:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388600AbgEXXjt (ORCPT ); Sun, 24 May 2020 19:39:49 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C2C8C05BD43 for ; Sun, 24 May 2020 16:39:49 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id bg4so1522203plb.3 for ; Sun, 24 May 2020 16:39:49 -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=xTPagZOTK2S3jx+U1S0sGHfFCrpHZOdN2FHjkt5W9h0=; b=rONxcso2Qpa31SDJFqeSUaNb156ofU+GfeVBBF+GldVsK9lgC0ia8mzKU+9jw5POyg HF7tm01nWoK0wZxoM5plbkrLAjjC24jjlVQXKvvjsULJoXQIvNGI/4O4OG1ciF9VGvuO OIqKD+VXmCxXJnoPOt6SPHX8rfh8h/84NhRU0= 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=xTPagZOTK2S3jx+U1S0sGHfFCrpHZOdN2FHjkt5W9h0=; b=YBEEO+N96BtFz/Z07NyWb5rph/0cJmlkdhdc4479qMitOQ34VCdi1yhuUaxRVbvP7/ 8TuxtQmT5/6z+i7O3bDQqpDoFTUQm4QoL226Jc6YtyVIwxhcxawCdePIUOaLFEzv6a1E VzzfML9OR6BSEfT+OPO9vun4BjeEWaQR4IFjdDNlFAAMbjPyaMPb6eDvkfmyYCOKmb2W ojQDJWTxMVBM9pcyhiO0pFI9F7oZA6prbCsQrfogg/+kpGPkOlZd9gvwHWoRfL4OHz7e BUNh/4ZlPh28yQfGr4+VaNPcZYzoFNzi2ttyfDm5yNdz1ar5Er7iXbOHoqWc5RtB4ZmO mUaw== X-Gm-Message-State: AOAM531cMww/0qV+vdTgEyMkNE0+6a+yhR1ohfPGlXRznLgSZF9xPZxp fEmP1QuPOZeYEhkdJ7Sk3Ch6YklGzzHIM8WS X-Received: by 2002:a17:90a:a588:: with SMTP id b8mr13701654pjq.100.1590363588694; Sun, 24 May 2020 16:39:48 -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 b16sm11633177pfi.74.2020.05.24.16.39.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 May 2020 16:39:48 -0700 (PDT) From: Sargun Dhillon To: linux-kernel@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org Cc: Sargun Dhillon , christian.brauner@ubuntu.com, 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: [PATCH 1/5] seccomp: Add find_notification helper Date: Sun, 24 May 2020 16:39:38 -0700 Message-Id: <20200524233942.8702-2-sargun@sargun.me> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200524233942.8702-1-sargun@sargun.me> References: <20200524233942.8702-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 | 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; -out: mutex_unlock(&filter->notify_lock); return ret; } -- 2.25.1