Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82CAC3CAE7F for ; Tue, 25 Aug 2026 19:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787687290; cv=none; b=DRql4DRttlm4umwyFoR6BuoLLUCEcNN/0AvsV7N5ISTEVjNK5YlIO6vC/trtuvCXy2K5mNSsTlbEihjFJz8k+T6vdJ66NZtrcte19vckFI1YDZTcO2LjVSRC2GbOcEiiJ5M1IId3fx24jAAnOXDxXJFu2myIn2IZwQL9HL2N6iw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787687290; c=relaxed/simple; bh=Kj9CSTHJUsHdHqos5bB9+55r05LeesHMHFYZNu/P/rE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qWoYW1ev+3fc/FYa9yMcq2PUYarjVo3vSvmoXb6oWqwrtjBoQ54JDAU7dSkUY2Z0D/08F2MkCTw3Zgx58BdUqmLXk8PqDgMX7bx/TvCsmgNqh+GZ/hPBc5rGTbZFoxARD6mjgcbmYb6cC+lxkyVABDkkV9rHSiNiD7VGtjbmw0I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N7tvhM6b; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="N7tvhM6b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86DC91F00A3F; Tue, 25 Aug 2026 19:48:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1787687289; bh=bWamUMw5fzMSn4dYKYr6XCIy4PfsI0NKF9KZ+SprNbM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=N7tvhM6btV+to7N4XH8OEaAAOVgAZKuE1Rei2kxU8A7KzEKUiT3tzOSOhN5mvSTjI 9yKYdnoTB+g1He+33y4N4jpRgjmDsuxnHXeqsOlqurzHH0mVjAHXNSxUy1v42xAjHu AQKhyOpuvjgidyNRaO+szcy8KYHnElOcMYCqDt0dc7uR9qc0/ltkhGJnoAedh/mzNz dORz6N1UfkjZsOW/2C1zqsG+6/vNLvrfAHHzNSRl+55PzfoYduz2DPBp7Q91IpDy+e TrMVGMisECguEsUGCW7nNtwkB3c0zwmO0OIBnqPvYOtoxKwzW4vwGX+RREyBGL60/2 OdGAX46roEXjQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Bryam Vargas , Stephen Smalley , Paul Moore , Sasha Levin Subject: [PATCH 5.15.y 4/4] selinux: require a class's permission values to cover its permission count Date: Tue, 25 Aug 2026 15:48:04 -0400 Message-ID: <20260825194804.376152-4-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260825194804.376152-1-sashal@kernel.org> References: <2026082023-agreeing-overbook-66d4@gregkh> <20260825194804.376152-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Bryam Vargas [ Upstream commit b98a8ac50775540f3804397ed08f61ef9910bcab ] security_get_permissions() sizes an array by the class's permissions.nprim and fills it at value - 1, from the inherited common's permission table and then the class's own. A value no permission defines leaves a NULL that sel_make_perm_files() passes to d_alloc_name(), an oops inside sel_write_load() that strands selinux_state.policy_mutex and leaves every later load in uninterruptible sleep; two permissions sharing a value overwrite the first kstrdup(). Bounding each value by nprim catches neither, and neither would a count: the symbol table is keyed on the permission name, so duplicates pass. Track the values each permission table claims and require them to cover exactly what its count declares, rejecting a count no value can reach. Conforming policies are unaffected. Cc: stable@vger.kernel.org Fixes: 55fcf09b3fe4 ("selinux: add support for querying object classes and permissions from the running policy") Signed-off-by: Bryam Vargas Acked-by: Stephen Smalley Signed-off-by: Paul Moore [ dropped the two `hash_eval()` context lines since 5.15 has only the 2-arg DEBUG_HASHES variant ] Signed-off-by: Sasha Levin --- security/selinux/ss/policydb.c | 51 ++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c index 2a73aa201ca5e..7cc112939d9b5 100644 --- a/security/selinux/ss/policydb.c +++ b/security/selinux/ss/policydb.c @@ -1114,7 +1114,18 @@ static int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len) return 0; } -static int perm_read(struct policydb *p, struct symtab *s, struct policy_file *fp) +/* + * Bitmap of the permission values a symtab has claimed. Values are 1-based + * and bounded by SEL_VEC_MAX, the width of an access vector, so the whole set + * fits in a u32 and the callers reject an nprim past that width. + */ +static u32 perm_claimed_mask(u32 nprim) +{ + return nprim ? U32_MAX >> (SEL_VEC_MAX - nprim) : 0; +} + +static int perm_read(struct policydb *p, struct symtab *s, + struct policy_file *fp, u32 *claimed) { char *key = NULL; struct perm_datum *perdatum; @@ -1138,6 +1149,10 @@ static int perm_read(struct policydb *p, struct symtab *s, struct policy_file *f /* indexes an nprim-sized array in security_get_permissions() */ if (perdatum->value > s->nprim) goto bad; + /* two permissions cannot share one slot of that array */ + if (*claimed & (1U << (perdatum->value - 1))) + goto bad; + *claimed |= 1U << (perdatum->value - 1); rc = str_read(&key, GFP_KERNEL, fp, len); if (rc) @@ -1158,7 +1173,7 @@ static int common_read(struct policydb *p, struct symtab *s, struct policy_file char *key = NULL; struct common_datum *comdatum; __le32 buf[4]; - u32 i, len, nel; + u32 i, len, nel, claimed = 0; int rc; comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL); @@ -1180,17 +1195,28 @@ static int common_read(struct policydb *p, struct symtab *s, struct policy_file if (rc) goto bad; comdatum->permissions.nprim = le32_to_cpu(buf[2]); + /* no permission value can reach a slot past SEL_VEC_MAX */ + rc = -EINVAL; + if (comdatum->permissions.nprim > SEL_VEC_MAX) + goto bad; rc = str_read(&key, GFP_KERNEL, fp, len); if (rc) goto bad; for (i = 0; i < nel; i++) { - rc = perm_read(p, &comdatum->permissions, fp); + rc = perm_read(p, &comdatum->permissions, fp, &claimed); if (rc) goto bad; } + rc = -EINVAL; + if (claimed != perm_claimed_mask(comdatum->permissions.nprim)) { + pr_err("SELinux: common %s does not define every permission it declares\n", + key); + goto bad; + } + rc = symtab_insert(s, key, comdatum); if (rc) goto bad; @@ -1326,7 +1352,7 @@ static int class_read(struct policydb *p, struct symtab *s, struct policy_file * char *key = NULL; struct class_datum *cladatum; __le32 buf[6]; - u32 i, len, len2, ncons, nel, val; + u32 i, len, len2, ncons, nel, val, claimed = 0, inherited = 0; int rc; cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL); @@ -1354,6 +1380,10 @@ static int class_read(struct policydb *p, struct symtab *s, struct policy_file * if (rc) goto bad; cladatum->permissions.nprim = le32_to_cpu(buf[3]); + /* no permission value can reach a slot past SEL_VEC_MAX */ + rc = -EINVAL; + if (cladatum->permissions.nprim > SEL_VEC_MAX) + goto bad; ncons = le32_to_cpu(buf[5]); @@ -1388,11 +1418,22 @@ static int class_read(struct policydb *p, struct symtab *s, struct policy_file * } } for (i = 0; i < nel; i++) { - rc = perm_read(p, &cladatum->permissions, fp); + rc = perm_read(p, &cladatum->permissions, fp, &claimed); if (rc) goto bad; } + /* the class's own permissions must claim the slots the common leaves */ + if (cladatum->comdatum) + inherited = cladatum->comdatum->permissions.nprim; + rc = -EINVAL; + if (claimed != (perm_claimed_mask(cladatum->permissions.nprim) & + ~perm_claimed_mask(inherited))) { + pr_err("SELinux: class %s does not define every permission it declares\n", + key); + goto bad; + } + rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp); if (rc) goto bad; -- 2.53.0