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 870AC32D43C; Sat, 12 Sep 2026 18:21:13 +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=1789237274; cv=none; b=gKyg9fEx9gOSQdI5UFEpHkbTUF52GQz3UIt3p5VqQNMWGATasPhsVr+reKv/jp4O1TIMXk+msgBT8aL83eVC9ZzW+HarSKqoQVtnCdOozqawls2kROuGm+LWjucxv6ibRsOJDu7NxW79l4PFMxmSxzfvq4GyH3d5Ry9qxiqz+Nw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789237274; c=relaxed/simple; bh=vTPbkcGgBLTbnSwiQYjymEfbzG0FKCc3kSaULkKwOB4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Hfo+T9CBE8eX0T/v6JUL+/Yhx3PWcUjWp26QEPHDj0w3YFGDMQ065bzyy8+bYQT0CUOLuy/2CXRxRy/mu6prD+xQe5cOUz7cpsaokBj5NyoI+7VOtYDQ4EbLeDQQUBAsgARg4LtHtC/Zz/R30QTUC8Cdz9AHEJKQrQMVmR4Lrdo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=TLfpbaBm; 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="TLfpbaBm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AEE01F000FF; Sat, 12 Sep 2026 18:21:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1789237273; bh=oH5CgAPzeST8Fjebf4sExlakokR7hBGVH5L90lzoZDg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TLfpbaBmY1KULR/7yGtLDMCUThn2SwZOjQyQxwTSKu8P2fzOp5i/mKHmpaEHFitq8 N9lyFFlQbR1evCUF1kGSXGdGhxLKdGPWMn4zpgn9FXxL8VNDQ1dtlEk4PZW6ISq9I0 GvsJy16e6Ys8HZcZiOaRxobIOlkZFxm8xeJQo2R0= 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 5.15 226/935] selinux: reject an unclaimed class value in security_get_classes() Date: Sat, 12 Sep 2026 08:54:16 +0200 Message-ID: <20260912065532.008302337@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260912065526.833703348@linuxfoundation.org> References: <20260912065526.833703348@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 5.15-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 7593e5ce6daaf..0f9f1295ad8df 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -3411,6 +3411,7 @@ int security_get_classes(struct selinux_policy *policy, char ***classes, u32 *nclasses) { struct policydb *policydb; + u32 i; int rc; policydb = &policy->policydb; @@ -3423,16 +3424,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