All of lore.kernel.org
 help / color / mirror / Atom feed
From: jboero <boeroboy@gmail.com>
To: selinux@vger.kernel.org
Cc: stephen.smalley.work@gmail.com, cgzones@googlemail.com,
	Johnny Boero <boeroboy@gmail.com>
Subject: [PATCH v1 1/3] policycoreutils/setfiles: reject invalid -T arguments
Date: Tue, 18 Aug 2026 14:23:31 -0500	[thread overview]
Message-ID: <20260818192340.119297-2-boeroboy@gmail.com> (raw)
In-Reply-To: <20260818192340.119297-1-boeroboy@gmail.com>

From: Johnny Boero <boeroboy@gmail.com>

The -T option is parsed with strtoull(3), which happily accepts leading
whitespace, a sign and, in particular, negative values: "-T -1" wraps
around to SIZE_MAX threads, which then fails a calloc(3) of SIZE_MAX
pthread_t entries rather than reporting a usage error.

Factor the parsing out into a helper and reject anything that is not a
plain decimal thread count.

Signed-off-by: Johnny Boero <boeroboy@gmail.com>
---
 policycoreutils/setfiles/setfiles.c | 31 ++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/policycoreutils/setfiles/setfiles.c b/policycoreutils/setfiles/setfiles.c
index da5d2024..c86cc0c9 100644
--- a/policycoreutils/setfiles/setfiles.c
+++ b/policycoreutils/setfiles/setfiles.c
@@ -64,6 +64,32 @@ static void set_rootpath(const char *arg)
 	}
 }
 
+/*
+ * Parse a thread count, as given by the -T option.  Returns -1 on invalid
+ * input.
+ */
+static int parse_nthreads(const char *str, size_t *nthreads)
+{
+	unsigned long long value;
+	char *endptr;
+
+	/*
+	 * Reject anything strtoull(3) would silently accept but that is not
+	 * a plain thread count, most notably negative values, which would
+	 * otherwise wrap around to a huge number of threads.
+	 */
+	if (!isdigit((unsigned char)*str))
+		return -1;
+
+	errno = 0;
+	value = strtoull(str, &endptr, 10);
+	if (errno != 0 || *endptr != '\0' || value > SIZE_MAX)
+		return -1;
+
+	*nthreads = value;
+	return 0;
+}
+
 static int canoncon(char **contextp)
 {
 	char *context = *contextp, *tmpcon;
@@ -141,7 +167,7 @@ int main(int argc, char **argv)
 	int opt, i = 0;
 	const char *input_filename = NULL;
 	int use_input_file = 0;
-	char *buf = NULL, *endptr;
+	char *buf = NULL;
 	size_t buf_len = 0, nthreads = 1;
 	const char *base;
 	int errors = 0;
@@ -376,8 +402,7 @@ int main(int argc, char **argv)
 			r_opts.xdev = SELINUX_RESTORECON_XDEV;
 			break;
 		case 'T':
-			nthreads = strtoull(optarg, &endptr, 10);
-			if (*optarg == '\0' || *endptr != '\0')
+			if (parse_nthreads(optarg, &nthreads) < 0)
 				usage(argv[0]);
 			break;
 		case 'A':
-- 
2.55.0


  reply	other threads:[~2026-08-18 19:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-18 19:23 [PATCH v1 0/3] setfiles: control relabeling parallelism from the environment jboero
2026-08-18 19:23 ` jboero [this message]
2026-08-18 20:04   ` [PATCH v1 1/3] policycoreutils/setfiles: reject invalid -T arguments Stephen Smalley
2026-08-19 16:41     ` James Carter
2026-08-19 16:42       ` John Boero
2026-08-18 19:23 ` [PATCH v1 2/3] policycoreutils/setfiles: use all CPU cores by default jboero
2026-08-18 20:05   ` Stephen Smalley
2026-08-18 19:23 ` [PATCH v1 3/3] policycoreutils/setfiles: honor RESTORECON_THREADS jboero
2026-08-18 20:10   ` Stephen Smalley
2026-08-19 12:57 ` [PATCH v1 0/3] setfiles: control relabeling parallelism from the environment Stephen Smalley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260818192340.119297-2-boeroboy@gmail.com \
    --to=boeroboy@gmail.com \
    --cc=cgzones@googlemail.com \
    --cc=selinux@vger.kernel.org \
    --cc=stephen.smalley.work@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.