Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2194580iof; Tue, 7 Jun 2022 22:49:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdvncE5HuXYeKHIieNkg3omLDT7VJGjuGrVlGU8SC8lbnodq9MuyfToZ3t6aBryUbPOVxD X-Received: by 2002:a05:6a00:174f:b0:51b:d744:7df7 with SMTP id j15-20020a056a00174f00b0051bd7447df7mr28636614pfc.55.1654667384405; Tue, 07 Jun 2022 22:49:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667384; cv=none; d=google.com; s=arc-20160816; b=JqmCMchVl3oAhV+WYo8joUwGnk1kyOddkUn3JHKWK9CtsEb8yc5nMcGmFtU+tskvoT FyWgYmTn/953ed+jp4LvDyHuTrShMqf5QD2QCLkjxxW+ACY9CAEcUvl0yxRlTrlQ8kmQ 2Bfi6x828yR7RzwWTzRbHlBs4H/rQuEkqkVSVl2KVEBV4jt0E8KTK72Z7YvKS7s3ANf5 WlqAl4ja8zES84b+15lcwu5Ah2s68Pcvd1RpOaGl9CvppKrXSt3HDYIRFokGVgpGiP6W 9hDnv8hC7HCxYSJJFdM+Gikivc6vuUn1TCXiMyLXeie1/I87GtFaFZwEmJ6Ry186kEWY mmdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=shfmo+pX19DO3Q2nfy8if3WBA9Okd9dVKQJlY/Y9Mnk=; b=rC1ATXdbTT4cRmDerCP7RHCO1OzM4EZQArLnNR9U4gkpo5kndoe7Es9zzQHCHQMkeW EXzG+qaKbZL00DJex80OKEnxkEgQNyvznUdvLAKI8iZPLNzMIXZtU+Nblki8Hr7BT7Qe xCi3O2RMSP35U0eCQeV596kxSMSHLdRTUGLB10b/nXR8YRaowR4viYMtzKdA60FfXm4j YRZtIE0skKLimUOkVgyy0JPL/76qcyt53eN0/RWP/8+V346GIc//jAbIXzNpLO0vc3cI lFRyNX06Jot+icLZxrfB0MMBzPnX3YptAaofPfi8Ga/OjGlec1aOj3ngyn86BXTJHnpl 8RpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=F5jxDbSK; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id r134-20020a632b8c000000b003fe1c0a9903si3193346pgr.348.2022.06.07.22.49.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:49:44 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=F5jxDbSK; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8D22E4A8CEB; Tue, 7 Jun 2022 22:18:40 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1388698AbiFHAuy (ORCPT + 99 others); Tue, 7 Jun 2022 20:50:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1381813AbiFGWVt (ORCPT ); Tue, 7 Jun 2022 18:21:49 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39AA526A90C; Tue, 7 Jun 2022 12:22:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9ED83609D0; Tue, 7 Jun 2022 19:21:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA755C385A2; Tue, 7 Jun 2022 19:21:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629711; bh=JResAk/B+hthY8xeZahQu9QuVQDbRBIYLvFy6476UII=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F5jxDbSK9KcMjB2kTdT5ByCgM7vBGSKauRJu90rTZ55c0ks9z1CzCiDm90euJGhIv VcTMf/OS+jLM8OQIf40Hh1pWXM5tpYHvV8d9Wkqcl8c95V6QMXjjy3nDbfjr6e0MBO jy64hhea+FQZejsMCebA4sVmeVMPHWwYfcVPTmNg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paul Moore , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= Subject: [PATCH 5.18 789/879] landlock: Create find_rule() from unmask_layers() Date: Tue, 7 Jun 2022 19:05:07 +0200 Message-Id: <20220607165025.766953867@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mickaël Salaün commit 2cd7cd6eed88b8383cfddce589afe9c0ae1d19b4 upstream. This refactoring will be useful in a following commit. Reviewed-by: Paul Moore Link: https://lore.kernel.org/r/20220506161102.525323-4-mic@digikod.net Cc: stable@vger.kernel.org Signed-off-by: Mickaël Salaün Signed-off-by: Greg Kroah-Hartman --- security/landlock/fs.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -183,23 +183,36 @@ int landlock_append_fs_rule(struct landl /* Access-control management */ -static inline layer_mask_t -unmask_layers(const struct landlock_ruleset *const domain, - const struct path *const path, const access_mask_t access_request, - layer_mask_t layer_mask) +/* + * The lifetime of the returned rule is tied to @domain. + * + * Returns NULL if no rule is found or if @dentry is negative. + */ +static inline const struct landlock_rule * +find_rule(const struct landlock_ruleset *const domain, + const struct dentry *const dentry) { const struct landlock_rule *rule; const struct inode *inode; - size_t i; - if (d_is_negative(path->dentry)) - /* Ignore nonexistent leafs. */ - return layer_mask; - inode = d_backing_inode(path->dentry); + /* Ignores nonexistent leafs. */ + if (d_is_negative(dentry)) + return NULL; + + inode = d_backing_inode(dentry); rcu_read_lock(); rule = landlock_find_rule( domain, rcu_dereference(landlock_inode(inode)->object)); rcu_read_unlock(); + return rule; +} + +static inline layer_mask_t unmask_layers(const struct landlock_rule *const rule, + const access_mask_t access_request, + layer_mask_t layer_mask) +{ + size_t layer_level; + if (!rule) return layer_mask; @@ -210,8 +223,9 @@ unmask_layers(const struct landlock_rule * the remaining layers for each inode, from the first added layer to * the last one. */ - for (i = 0; i < rule->num_layers; i++) { - const struct landlock_layer *const layer = &rule->layers[i]; + for (layer_level = 0; layer_level < rule->num_layers; layer_level++) { + const struct landlock_layer *const layer = + &rule->layers[layer_level]; const layer_mask_t layer_bit = BIT_ULL(layer->level - 1); /* Checks that the layer grants access to the full request. */ @@ -269,8 +283,9 @@ static int check_access_path(const struc while (true) { struct dentry *parent_dentry; - layer_mask = unmask_layers(domain, &walker_path, access_request, - layer_mask); + layer_mask = + unmask_layers(find_rule(domain, walker_path.dentry), + access_request, layer_mask); if (layer_mask == 0) { /* Stops when a rule from each layer grants access. */ allowed = true;