From mboxrd@z Thu Jan 1 00:00:00 1970 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 6D9173515C1; Sat, 12 Sep 2026 15:43:47 +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=1789227828; cv=none; b=N8dtNeng7c0L68XkPLbmqiB8xPdqV5JB+WMUFv7LkAxRd2f/1fEmob/EUU9rh7OnZxHcvFYi43OdaVPweeRFYcQWICAJq9+L66iyohFCVQVvF+wDjXU823I9A7S0txXKsDZBrQJcYbwN4R0A24gn8wbWt8qN+a8C08LvEsCy0kA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789227828; c=relaxed/simple; bh=VkLDCHbfiyGSHLA3awG1smArRBBWO3s8GVNnPTGWZPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=anAcNSEYzxZU+T1P75cgWZt3YPl1b9bXwyplHpnWpe3glh8q20ofj2UP/UHd7b084EZ24RlY/jyL9js8jxKeG/mfozxwCCTAmqBF/f0O4gkbdXpYMYRzIlZ9gFDtJu6y89GkJq7qE/ZPe5molSqcRHFc/72KJU+TwPRVevDtg6E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=Pgq3w8+A; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="Pgq3w8+A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE50D1F000FF; Sat, 12 Sep 2026 15:43:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1789227827; bh=WAiRICuhJ2kGd1w5uyeoaGRgT8wO9jxOudlXSy2VnlE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Pgq3w8+AtP/WQJeRoBrtuYTiJS/nDF6oJ5q++UeDnwWH1Sn0C5EOXicl5x/vT44IA TXu0hMuUXQQEUdQha0SJJg49jqpI48IbtoUsli3AIyAilsltOrfhJAc/82Wc8BDe4u p7MsNGb7pgzmZ0i9JxuwJfr3jpWidY1K9AWCfo3M= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Bryam Vargas , Stephen Smalley , Paul Moore , Alexander Martyniuk , Sasha Levin Subject: [PATCH 6.1 0261/1191] selinux: reject an unclaimed class value in security_get_classes() Date: Sat, 12 Sep 2026 08:49:49 +0200 Message-ID: <20260912065554.082289447@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260912065548.086904252@linuxfoundation.org> References: <20260912065548.086904252@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Bryam Vargas commit 22b05fec62c0fe9864cfceb52f7d0f3a34d9b1dd upstream. security_get_classes() sizes an array by p_classes.nprim and fills it at value - 1, so a class value the policy never defines leaves a NULL. sel_make_classes() passes every entry to sel_make_dir(), reaching the same d_alloc_name() dereference as the permission array. The class symbol table is allowed to be sparse (policydb_class_isvalid() exists to absorb that), but this getter builds its own array straight from the hash table and has no such predicate. Fail the lookup when a value went unclaimed instead of handing out the NULL. Conforming policies define every class they declare and 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 Signed-off-by: Alexander Martyniuk Signed-off-by: Sasha Levin --- security/selinux/ss/services.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c index 80340bc0ee6a7..a1e3520596d60 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -3414,6 +3414,7 @@ int security_get_classes(struct selinux_policy *policy, char ***classes, u32 *nclasses) { struct policydb *policydb; + u32 i; int rc; policydb = &policy->policydb; @@ -3426,16 +3427,29 @@ int security_get_classes(struct selinux_policy *policy, rc = hashtab_map(&policydb->p_classes.table, get_classes_callback, *classes); - if (rc) { - u32 i; + if (rc) + goto err; - for (i = 0; i < *nclasses; i++) - kfree((*classes)[i]); - kfree(*classes); + /* + * The class symtab may be sparse, which policydb_class_isvalid() exists + * to absorb; the callback fills this array by value, so an unclaimed + * one leaves a NULL that sel_make_classes() hands to sel_make_dir(). + */ + for (i = 0; i < *nclasses; i++) { + if (!(*classes)[i]) { + rc = -EINVAL; + goto err; + } } out: return rc; + +err: + for (i = 0; i < *nclasses; i++) + kfree((*classes)[i]); + kfree(*classes); + return rc; } static int get_permissions_callback(void *k, void *d, void *args) -- 2.53.0