summaryrefslogtreecommitdiff
path: root/init/builtins.cpp
diff options
context:
space:
mode:
authorTom Cherry <tomcherry@google.com>2017-05-04 17:40:33 -0700
committerTom Cherry <tomcherry@google.com>2017-05-05 14:37:01 -0700
commit517e1f17cfec2143d4d10a64b1496a550acf3ea2 (patch)
treeeda028c6359878b7f5d3ab56d2dc65a4ac1cad39 /init/builtins.cpp
parent3d7ee068e0a0ce707965835f58abce65189abda4 (diff)
init: Check DecodeUid() result and use error string
Check the result of DecodeUid() and return failure when uids/gids are unable to be decoded. Also, use an error string instead of logging directly such that more context can be added when decoding fails. Bug: 38038887 Test: Boot bullhead Test: Init unit tests Change-Id: I84c11aa5a8041bf5d2f754ee9af748344b789b37
Diffstat (limited to 'init/builtins.cpp')
-rw-r--r--init/builtins.cpp39
1 files changed, 28 insertions, 11 deletions
diff --git a/init/builtins.cpp b/init/builtins.cpp
index 3dadfd7dc..848dfdbf9 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -215,11 +215,19 @@ static int do_mkdir(const std::vector<std::string>& args) {
}
if (args.size() >= 4) {
- uid_t uid = decode_uid(args[3].c_str());
+ uid_t uid;
+ std::string decode_uid_err;
+ if (!DecodeUid(args[3], &uid, &decode_uid_err)) {
+ LOG(ERROR) << "Unable to find UID for '" << args[3] << "': " << decode_uid_err;
+ return -1;
+ }
gid_t gid = -1;
if (args.size() == 5) {
- gid = decode_uid(args[4].c_str());
+ if (!DecodeUid(args[4], &gid, &decode_uid_err)) {
+ LOG(ERROR) << "Unable to find GID for '" << args[3] << "': " << decode_uid_err;
+ return -1;
+ }
}
if (lchown(args[1].c_str(), uid, gid) == -1) {
@@ -655,17 +663,26 @@ static int do_copy(const std::vector<std::string>& args) {
}
static int do_chown(const std::vector<std::string>& args) {
- /* GID is optional. */
- if (args.size() == 3) {
- if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
- return -errno;
- } else if (args.size() == 4) {
- if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
- decode_uid(args[2].c_str())) == -1)
- return -errno;
- } else {
+ uid_t uid;
+ std::string decode_uid_err;
+ if (!DecodeUid(args[1], &uid, &decode_uid_err)) {
+ LOG(ERROR) << "Unable to find UID for '" << args[1] << "': " << decode_uid_err;
return -1;
}
+
+ // GID is optional and pushes the index of path out by one if specified.
+ const std::string& path = (args.size() == 4) ? args[3] : args[2];
+ gid_t gid = -1;
+
+ if (args.size() == 4) {
+ if (!DecodeUid(args[2], &gid, &decode_uid_err)) {
+ LOG(ERROR) << "Unable to find GID for '" << args[2] << "': " << decode_uid_err;
+ return -1;
+ }
+ }
+
+ if (lchown(path.c_str(), uid, gid) == -1) return -errno;
+
return 0;
}