summaryrefslogtreecommitdiff
path: root/Userland/chown.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-04 19:29:30 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-04 19:29:30 +0200
commit040ba77d44517335282cc41b0b5ddfe281289cfd (patch)
tree28b8278421987129254e14ee4df7174cb537df66 /Userland/chown.cpp
parent54cb1e36b6b91bec43f7fb96a2075d316f5fea6a (diff)
downloadserenity-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.cpp14
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;
}
}