From 1ce8bc6c2bb7dac0aedcb016c9f06a980d3c9282 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Mon, 2 Nov 2026 13:15:55 -0500 Subject: [PATCH 1/2] reject an empty entry in the -u list an empty entry like the first one in "-u ,99999", or a lone "^", was parsed as UID 0, so lsof listed or excluded root's files. report an error instead. --- Makefile.am | 1 + src/arg.c | 11 +++++++++++ tests/case-20-uid-empty-entry.bash | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100755 tests/case-20-uid-empty-entry.bash diff --git a/Makefile.am b/Makefile.am index 75086ca3..058098f6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -132,6 +132,7 @@ DIALECT_NEUTRAL_TESTS = tests/case-00-hello.bash \ tests/case-20-handle-missing-files.bash \ tests/case-20-offset-field.bash \ tests/case-20-repeat-count.bash \ + tests/case-20-uid-empty-entry.bash \ tests/case-21-exit-Q-status.bash \ tests/case-22-empty-process-name.bash \ tests/case-23-multi-address-hostname.bash \ diff --git a/src/arg.c b/src/arg.c index 9bb09b9f..da71fdc5 100644 --- a/src/arg.c +++ b/src/arg.c @@ -2063,6 +2063,17 @@ int enter_uid(struct lsof_context *ctx, /* context */ s++; if (j) continue; + if (!lnml) { + + /* + * An empty entry (e.g. from ",99" or "^") would otherwise be + * taken as UID 0. + */ + (void)fprintf(stderr, "%s: empty -u UID or login name: ", Pn); + safestrprt(us, stderr, 1); + err = 1; + continue; + } if (nn) { lnm[lnml++] = '\0'; if ((pw = getpwnam(lnm)) == NULL) { diff --git a/tests/case-20-uid-empty-entry.bash b/tests/case-20-uid-empty-entry.bash new file mode 100755 index 00000000..c67d9d8d --- /dev/null +++ b/tests/case-20-uid-empty-entry.bash @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# An empty entry in a -u list must not be taken as UID 0. +source tests/common.bash + +{ + for spec in ",99999" "^"; do + output=$($lsof -u "$spec" -F u 2>/dev/null) + status=$? + if echo "$output" | grep -q '^u0$'; then + echo "lsof -u $spec selected UID 0" >> $report + exit 1 + fi + if [ $status -eq 0 ]; then + echo "lsof -u $spec should fail" >> $report + exit 1 + fi + done + exit 0 +} > $report 2>&1 From 5a15d3156975eb8c88795ae4796075fa293bacfe Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Mon, 2 Nov 2026 19:27:22 -0500 Subject: [PATCH 2/2] reject an empty -u list too -u "" was accepted with no UIDs at all, so lsof just listed evrything, root's files included. treat it like a missing argument and report "no UIDs specified". --- src/arg.c | 2 +- tests/case-20-uid-empty-entry.bash | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/arg.c b/src/arg.c index da71fdc5..2005051c 100644 --- a/src/arg.c +++ b/src/arg.c @@ -2019,7 +2019,7 @@ int enter_uid(struct lsof_context *ctx, /* context */ char *s, *st; uid_t uid; - if (!us) { + if (!us || !*us) { (void)fprintf(stderr, "%s: no UIDs specified\n", Pn); return (1); } diff --git a/tests/case-20-uid-empty-entry.bash b/tests/case-20-uid-empty-entry.bash index c67d9d8d..0149cdcc 100755 --- a/tests/case-20-uid-empty-entry.bash +++ b/tests/case-20-uid-empty-entry.bash @@ -3,7 +3,7 @@ source tests/common.bash { - for spec in ",99999" "^"; do + for spec in "" ",99999" "^"; do output=$($lsof -u "$spec" -F u 2>/dev/null) status=$? if echo "$output" | grep -q '^u0$'; then