diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-04 19:29:30 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-04 19:29:30 +0200 |
commit | 040ba77d44517335282cc41b0b5ddfe281289cfd (patch) | |
tree | 28b8278421987129254e14ee4df7174cb537df66 /Userland/chown.cpp | |
parent | 54cb1e36b6b91bec43f7fb96a2075d316f5fea6a (diff) | |
download | serenity-040ba77d44517335282cc41b0b5ddfe281289cfd.zip |
Userland: Fix null-pointer deref on unknown user/group in chown/chgrp
We can't just blindly dereference the result of getpwnam()/getgrnam()!
Fixes #1625.
Diffstat (limited to 'Userland/chown.cpp')
-rw-r--r-- | Userland/chown.cpp | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/Userland/chown.cpp b/Userland/chown.cpp index 54202c2f72..a7ea33dbc5 100644 --- a/Userland/chown.cpp +++ b/Userland/chown.cpp @@ -57,23 +57,23 @@ int main(int argc, char** argv) bool ok; new_uid = parts[0].to_uint(ok); if (!ok) { - new_uid = getpwnam(parts[0].characters())->pw_uid; - - if (!new_uid) { - fprintf(stderr, "Invalid uid: '%s'\n", parts[0].characters()); + auto* passwd = getpwnam(parts[0].characters()); + if (!passwd) { + fprintf(stderr, "Unknown user '%s'\n", parts[0].characters()); return 1; } + new_uid = passwd->pw_uid; } if (parts.size() == 2) { new_gid = parts[1].to_uint(ok); if (!ok) { - new_gid = getgrnam(parts[1].characters())->gr_gid; - + auto* group = getgrnam(parts[1].characters()); if (!new_gid) { - fprintf(stderr, "Invalid gid: '%s'\n", parts[1].characters()); + fprintf(stderr, "Unknown group '%s'\n", parts[1].characters()); return 1; } + new_gid = group->gr_gid; } } |