From nobody Tue Apr 7 07:08:24 2026 Received: from sender-of-o57.zoho.eu (sender-of-o57.zoho.eu [136.143.169.57]) (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 53D243750C7 for ; Sun, 15 Mar 2026 17:11:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.169.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773594676; cv=pass; b=gL6T6H5jA/TMoxfqDqavgWvXEdocZDAzNXjh2zoyaU7H3sRczsrNGaAZAaO+hPt7p/7DfHCy7QCKvMyA+ViSZo5ZZzeZGSYrbsjaumUh9YnijTLSkkDx2xw/k2uoPUElISXqEvEtR5wmTXFlxih/eO/xZVbWI8g4AfR46IFB4PI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773594676; c=relaxed/simple; bh=MPc6BFCGSACM368hqh07vzRexP5rxqkSUbTOB1V8GJE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kjknN3ncwxs24uCiwPjPAn7M4Ha7diMosAyWQ+MdFQbyxd/dk0Z0Fu1sTf0OGedgSpu4g2Jsk4Y0oh+4QWeFSVb5ydZbKzXj9hA+lh1diu7uuI+Na+ZJRrxCRLdOh2OHS5KXo+RTe8iYJ+yubfzhLsBMd+eV6i//GvpXWZV0AfI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=objecting.org; spf=pass smtp.mailfrom=objecting.org; dkim=pass (1024-bit key) header.d=objecting.org header.i=objecting@objecting.org header.b=iig0lktX; arc=pass smtp.client-ip=136.143.169.57 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=objecting.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=objecting.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=objecting.org header.i=objecting@objecting.org header.b="iig0lktX" ARC-Seal: i=1; a=rsa-sha256; t=1773594671; cv=none; d=zohomail.eu; s=zohoarc; b=c/a/BaqkZtlDyXznkcFrqBzjQKaeRZDRMRQkR09d57nn9P5SSonKVa5vpvK4xtmIa1a6w+mq9vggYPk/Fo/NH8xedcizK4OAaYOShFhPNDUyA8fOxZg2Rsbp2j1lNANEbx3QULnSiSAikBwT1e/jdWxSMrwzM+YfSQPrSSeCjpU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.eu; s=zohoarc; t=1773594671; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=2WBm7tkyxyrrzugqbtjBKtsFz7871CMH0LPPBAiszcQ=; b=dZeu69cbb9OOrntbkp9qAQQxXrahSl4mi6mt9ZUV6l/3oq4g8mFP9FlOE0VWXQBwCRSnMxGExE7oqGhVAQa+/XusjqCrBUKc10El+NhaaUgbvZI8CqUU+QtU2CQ2HmlP5zdOAOAOLO0UftVnLQhrU6akppUTbFAqSNenm0IUzKQ= ARC-Authentication-Results: i=1; mx.zohomail.eu; dkim=pass header.i=objecting.org; spf=pass smtp.mailfrom=objecting@objecting.org; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1773594671; s=zmail; d=objecting.org; i=objecting@objecting.org; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-Id:Message-Id:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Reply-To; bh=2WBm7tkyxyrrzugqbtjBKtsFz7871CMH0LPPBAiszcQ=; b=iig0lktXXzuu2hdugq+T/lA8PSlIixquwm8L50caDv3V44bR/fXTC/tZbCW2ddR5 gF4GjMR90Ll8ZmDJDmNgaKaoHpBaRvC3K3AVayZLAWqT/a8Clk8hjvhvp8KDoYAhHzM 5Io6Gg6/O3qTykW0d/odJIE/b109SZ8vUkir2kxM= Received: by mx.zoho.eu with SMTPS id 1773594668734874.9668942742104; Sun, 15 Mar 2026 18:11:08 +0100 (CET) From: Josh Law To: Andrew Morton , Josh Law Cc: linux-kernel@vger.kernel.org Subject: [PATCH 5/7] lib/glob: add glob_validate() for pattern syntax checking Date: Sun, 15 Mar 2026 17:11:02 +0000 Message-Id: <20260315171104.268944-6-objecting@objecting.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260315171104.268944-1-objecting@objecting.org> References: <20260315171104.268944-1-objecting@objecting.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External Content-Type: text/plain; charset="utf-8" Add glob_validate() which checks whether a glob pattern is syntactically well-formed before matching. It detects: - Unclosed character classes: a '[' with no matching ']' - Trailing backslash: a '\' at end of pattern with nothing to escape glob_match() already handles these gracefully (unclosed brackets are matched literally, a trailing backslash matches itself), but callers like ftrace filters or sysfs attribute stores that accept patterns from userspace may want to reject malformed input upfront with a clear error rather than silently falling back to literal matching. Signed-off-by: Josh Law --- include/linux/glob.h | 1 + lib/glob.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/linux/glob.h b/include/linux/glob.h index 36527ae89730..deceaa2e4a74 100644 --- a/include/linux/glob.h +++ b/include/linux/glob.h @@ -7,5 +7,6 @@ =20 bool __pure glob_match(char const *pat, char const *str); bool __pure glob_match_nocase(char const *pat, char const *str); +bool __pure glob_validate(char const *pat); =20 #endif /* _LINUX_GLOB_H */ diff --git a/lib/glob.c b/lib/glob.c index 9c71ccc15abc..800163ed4dbf 100644 --- a/lib/glob.c +++ b/lib/glob.c @@ -186,3 +186,47 @@ bool __pure glob_match_nocase(char const *pat, char co= nst *str) return __glob_match(pat, str, true); } EXPORT_SYMBOL(glob_match_nocase); + +/** + * glob_validate - Check whether a glob pattern is well-formed + * @pat: Shell-style pattern to validate. + * + * Return: true if @pat is a syntactically valid glob pattern, false + * if it contains malformed constructs. The following are considered + * invalid: + * + * - An opening '[' with no matching ']' (unclosed character class). + * - A trailing '\' with no character following it. + * + * Note that glob_match() handles these gracefully (an unclosed bracket + * is matched literally, a trailing backslash matches itself), but + * callers that accept patterns from user input may wish to reject + * malformed patterns early with a clear error. + */ +bool __pure glob_validate(char const *pat) +{ + while (*pat) { + switch (*pat++) { + case '\\': + if (*pat =3D=3D '\0') + return false; + pat++; + break; + case '[': { + if (*pat =3D=3D '!' || *pat =3D=3D '^') + pat++; + /* ] as first character is literal, not end of class */ + if (*pat =3D=3D ']') + pat++; + while (*pat && *pat !=3D ']') + pat++; + if (*pat =3D=3D '\0') + return false; + pat++; /* skip ']' */ + break; + } + } + } + return true; +} +EXPORT_SYMBOL(glob_validate); --=20 2.34.1