Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2257371iof; Wed, 8 Jun 2022 00:38:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx86JiiZ5fX1y9JG/RwXo2XWFXUvkPhHB21gwf6Vg14MVC2y/EuG2Mcmh5txxHT/bsPMCq+ X-Received: by 2002:a17:902:c403:b0:167:4a9f:2785 with SMTP id k3-20020a170902c40300b001674a9f2785mr25805242plk.67.1654673907880; Wed, 08 Jun 2022 00:38:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654673907; cv=none; d=google.com; s=arc-20160816; b=kdqOSSkWf550l9UzDnklH99qSxZyu0uwhj+557M7lUyct65euXALJ9z2n74r4oU+QR j9lyiLlKqYKlFXQDVx9jHG4+U5L1lVCSNSlNSftB73aAR/RAOP2LidWuhq5UrW2abhHA CnvYOUJNPy2s8qL+L6Ru5rML4AXAgv7zA5J9kvmMe6aMjbB1WRF/N2zeen9NaWY+VAIX VxFPrR7VxzMOE8UQ3g94r2v3jq4XwM1szRhgCjYaxMBELF1VlH1JeGlliWKONd/QsWDx HoJVRglZBSShftYh7G3+fRcviWn9QHlyGO+rKWmd6h1YPqZhBBge0UKBx4VS7/n7UhVI qswA== 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=NoM8lnH9XiiXlJcU+dl6DW6p0abBMBmvjFtBcnkj8Rs=; b=q4G5H3i2zBMZw/ypnnGbrwY1Z52yeKfdetTtDHekyqEQvZZWwTxVUil/kGFi8l4Us/ aH1hOauBONC+H/KO08Os+y2p3d4cceCwbtbHix0zp1rWbVeIfLMJ7BTeJSBQuHyfzXjN 3fm+QCq1f6QG8qs4bLIMcIWtVG86wYW/KWtpLOsDo4bSQjX6mi7j+IjRnfHONlTchKhm PsWRxvpiOGJxdI5qHiaw+fjksmy2VRMhjY/ivDv/47BWZU9EK11kb7Xvs8j3iZOXQJsy pCgMpi2LBZU5fzYD80mpwnuW3fmGiVTNBY09FLJwpEykFrmBDloGqxNL7D/oxS4bvkxv x5nA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0y7qU7Jp; 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 e2-20020a636902000000b003c6af783a2fsi26294605pgc.797.2022.06.08.00.38.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 00:38:27 -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=0y7qU7Jp; 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 3407D27145E; Wed, 8 Jun 2022 00:09:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1444103AbiFHCJT (ORCPT + 99 others); Tue, 7 Jun 2022 22:09:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442477AbiFHAye (ORCPT ); Tue, 7 Jun 2022 20:54:34 -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 9B56B267CEE; Tue, 7 Jun 2022 12:21:38 -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 A2AAC60906; Tue, 7 Jun 2022 19:21:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0CE7C385A2; Tue, 7 Jun 2022 19:21:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629697; bh=J9tuh9jTQAWUyz1dSmwcnH33w65DBg81WLoe1PgnrLs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0y7qU7Jp6OZoXmBApXquVt06HrwR5zUJwiJEpmL0yRTR6NRTiDbumFAxblhIwJlc5 /tLAbJxoU2ILXn2wQW3czC6MVJ8jFtPrgDcdjAGxqOkQgIzVhDXu8lQ/+JCSvWWymB d1V9Sz47xUeddW43dedAPXztqMe+6pwmyKhFbmy4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= Subject: [PATCH 5.18 784/879] landlock: Change landlock_add_rule(2) argument check ordering Date: Tue, 7 Jun 2022 19:05:02 +0200 Message-Id: <20220607165025.626272356@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 589172e5636c4d16c40b90e87543d43defe2d968 upstream. This makes more sense to first check the ruleset FD and then the rule attribute. It will be useful to factor out code for other rule types. Add inval_add_rule_arguments tests, extension of empty_path_beneath_attr tests, to also check error ordering for landlock_add_rule(2). Link: https://lore.kernel.org/r/20220506160820.524344-9-mic@digikod.net Cc: stable@vger.kernel.org Signed-off-by: Mickaël Salaün Signed-off-by: Greg Kroah-Hartman --- security/landlock/syscalls.c | 22 ++++++++++------- tools/testing/selftests/landlock/base_test.c | 34 +++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 11 deletions(-) --- a/security/landlock/syscalls.c +++ b/security/landlock/syscalls.c @@ -318,20 +318,24 @@ SYSCALL_DEFINE4(landlock_add_rule, const if (flags) return -EINVAL; - if (rule_type != LANDLOCK_RULE_PATH_BENEATH) - return -EINVAL; - - /* Copies raw user space buffer, only one type for now. */ - res = copy_from_user(&path_beneath_attr, rule_attr, - sizeof(path_beneath_attr)); - if (res) - return -EFAULT; - /* Gets and checks the ruleset. */ ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE); if (IS_ERR(ruleset)) return PTR_ERR(ruleset); + if (rule_type != LANDLOCK_RULE_PATH_BENEATH) { + err = -EINVAL; + goto out_put_ruleset; + } + + /* Copies raw user space buffer, only one type for now. */ + res = copy_from_user(&path_beneath_attr, rule_attr, + sizeof(path_beneath_attr)); + if (res) { + err = -EFAULT; + goto out_put_ruleset; + } + /* * Informs about useless rule: empty allowed_access (i.e. deny rules) * are ignored in path walks. --- a/tools/testing/selftests/landlock/base_test.c +++ b/tools/testing/selftests/landlock/base_test.c @@ -121,20 +121,50 @@ TEST(inval_create_ruleset_flags) ASSERT_EQ(EINVAL, errno); } -TEST(empty_path_beneath_attr) +/* Tests ordering of syscall argument checks. */ +TEST(add_rule_checks_ordering) { const struct landlock_ruleset_attr ruleset_attr = { .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE, }; + struct landlock_path_beneath_attr path_beneath_attr = { + .allowed_access = LANDLOCK_ACCESS_FS_EXECUTE, + .parent_fd = -1, + }; const int ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); ASSERT_LE(0, ruleset_fd); - /* Similar to struct landlock_path_beneath_attr.parent_fd = 0 */ + /* Checks invalid flags. */ + ASSERT_EQ(-1, landlock_add_rule(-1, 0, NULL, 1)); + ASSERT_EQ(EINVAL, errno); + + /* Checks invalid ruleset FD. */ + ASSERT_EQ(-1, landlock_add_rule(-1, 0, NULL, 0)); + ASSERT_EQ(EBADF, errno); + + /* Checks invalid rule type. */ + ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, 0, NULL, 0)); + ASSERT_EQ(EINVAL, errno); + + /* Checks invalid rule attr. */ ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, NULL, 0)); ASSERT_EQ(EFAULT, errno); + + /* Checks invalid path_beneath.parent_fd. */ + ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, + &path_beneath_attr, 0)); + ASSERT_EQ(EBADF, errno); + + /* Checks valid call. */ + path_beneath_attr.parent_fd = + open("/tmp", O_PATH | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC); + ASSERT_LE(0, path_beneath_attr.parent_fd); + ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, + &path_beneath_attr, 0)); + ASSERT_EQ(0, close(path_beneath_attr.parent_fd)); ASSERT_EQ(0, close(ruleset_fd)); }