The numeric form of the --owner argument was converted with atoi, which reports no error and has undefined behaviour when the value does not fit in an int. "cpio -o --owner=4294967297" silently recorded uid 1 instead of rejecting the value.
* src/userspec.c (parse_id): New function. Convert a decimal id with strtoul and reject overflow and trailing characters. (parse_user_spec0): Use it for the user and group ids. * tests/owner-id-range.at: New test. * tests/Makefile.am: Add it. * tests/testsuite.at: Include it. Signed-off-by: Mohammed Mustafa <[email protected]> --- src/userspec.c | 37 ++++++++++++++++++++++++++++++++++--- tests/Makefile.am | 3 ++- tests/owner-id-range.at | 44 +++++++++++++++++++++++++++++++++++++++++ tests/testsuite.at | 1 + 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 tests/owner-id-range.at diff --git a/src/userspec.c b/src/userspec.c index e45d5fd..1ca5161 100644 --- a/src/userspec.c +++ b/src/userspec.c @@ -37,12 +37,33 @@ static int isnumber_p (const char *str) { for (; *str; str++) if (!isdigit (*str)) return 0; return 1; } +/* Convert STR, a string of decimal digits, to a numeric user or group + ID. MAX is the largest value that fits in the target ID type. Return + true and store the value in *IDP on success; return false if STR is not + a valid number or if the value is out of range. */ + +static bool +parse_id (const char *str, unsigned long max, unsigned long *idp) +{ + unsigned long n; + char *end; + + if (!isdigit ((unsigned char) *str)) + return false; + errno = 0; + n = strtoul (str, &end, 10); + if (errno != 0 || *end != '\0' || n > max) + return false; + *idp = n; + return true; +} + static void store_string (char **bufptr, size_t *buflen, char *str) { @@ -124,7 +147,13 @@ parse_user_spec0 (char *spec, uid_t *uid, gid_t *gid, if (use_login_group) error_msg = _("cannot get the login group of a numeric UID"); else - *uid = atoi (u); + { + unsigned long id; + if (!parse_id (u, (uid_t) -1, &id)) + error_msg = _("invalid user"); + else + *uid = (uid_t) id; + } } } else @@ -168,7 +197,13 @@ parse_user_spec0 (char *spec, uid_t *uid, gid_t *gid, if (!isnumber_p (g)) error_msg = _("invalid group"); else - *gid = atoi (g); + { + unsigned long id; + if (!parse_id (g, (gid_t) -1, &id)) + error_msg = _("invalid group"); + else + *gid = (gid_t) id; + } } else *gid = grp->gr_gid; diff --git a/tests/Makefile.am b/tests/Makefile.am index e5096e5..ac0a145 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -58,7 +58,8 @@ TESTSUITE_AT = \ CVE-2019-14866.at\ linktime.at\ linktime01.at\ - symlink-tar-abs.at + symlink-tar-abs.at\ + owner-id-range.at TESTSUITE = $(srcdir)/testsuite diff --git a/tests/owner-id-range.at b/tests/owner-id-range.at new file mode 100644 index 0000000..a7a4d45 --- /dev/null +++ b/tests/owner-id-range.at @@ -0,0 +1,44 @@ +# Process this file with autom4te to create testsuite. -*- Autotest -*- +# Copyright (C) 2026 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +AT_SETUP([out-of-range owner id]) +AT_KEYWORDS([owner userspec integer overflow]) + +AT_CHECK([ +# Cpio versions up to 2.15 converted the numeric parts of the --owner +# argument with atoi, which silently truncates values that do not fit in +# an int. Such values must be diagnosed instead. +cat > experr <<EOF +cpio: 4294967297: invalid user +Try 'cpio --help' or 'cpio --usage' for more information. +EOF +echo file | cpio -o --owner=4294967297 +], +[2], +[], +[experr]) + +AT_CHECK([ +cat > experr <<EOF +cpio: 1:4294967297: invalid group +Try 'cpio --help' or 'cpio --usage' for more information. +EOF +echo file | cpio -o --owner=1:4294967297 +], +[2], +[], +[experr]) +AT_CLEANUP diff --git a/tests/testsuite.at b/tests/testsuite.at index 099c1e9..37dd21f 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -49,3 +49,4 @@ m4_include([linktime01.at]) m4_include([symlink-tar-abs.at]) +m4_include([owner-id-range.at]) -- 2.53.0
