summaryrefslogtreecommitdiff
path: root/libkeyutils/mini_keyctl.cpp
diff options
context:
space:
mode:
authorVictor Hsieh <victorhsieh@google.com>2019-03-18 13:49:02 -0700
committerVictor Hsieh <victorhsieh@google.com>2019-03-18 13:57:05 -0700
commit0fb290bb8aa46b6cb5e8790be7fbcec0a00876a9 (patch)
tree1669eb88756706fd9e6936d932f110c4df429873 /libkeyutils/mini_keyctl.cpp
parent582c7b9b829b3fe5b2b28efe3cb729d4fca76900 (diff)
mini-keyctl: use ParseInt to parse keys
- Valid ID format examples: 0x90a, 123 - ID like 90a will not work now. Bug: None Test: mini-keyctl unlink 0x11d25c86 0x2873c96d Change-Id: I057bce0a49a60f475d54b23e28dc18db25124466
Diffstat (limited to 'libkeyutils/mini_keyctl.cpp')
-rw-r--r--libkeyutils/mini_keyctl.cpp17
1 files changed, 11 insertions, 6 deletions
diff --git a/libkeyutils/mini_keyctl.cpp b/libkeyutils/mini_keyctl.cpp
index 844f873bc5..e09c864e18 100644
--- a/libkeyutils/mini_keyctl.cpp
+++ b/libkeyutils/mini_keyctl.cpp
@@ -20,6 +20,7 @@
#include "mini_keyctl_utils.h"
+#include <error.h>
#include <stdio.h>
#include <unistd.h>
@@ -36,6 +37,14 @@ static void Usage(int exit_code) {
_exit(exit_code);
}
+static key_serial_t parseKeyOrDie(const char* str) {
+ key_serial_t key;
+ if (!android::base::ParseInt(str, &key)) {
+ error(1 /* exit code */, 0 /* errno */, "Unparsable key: '%s'\n", str);
+ }
+ return key;
+}
+
int main(int argc, const char** argv) {
if (argc < 2) Usage(1);
const std::string action = argv[1];
@@ -67,17 +76,13 @@ int main(int argc, const char** argv) {
return RestrictKeyring(keyring);
} else if (action == "unlink") {
if (argc != 4) Usage(1);
- key_serial_t key = std::stoi(argv[2], nullptr, 16);
+ key_serial_t key = parseKeyOrDie(argv[2]);
const std::string keyring = argv[3];
return Unlink(key, keyring);
} else if (action == "security") {
if (argc != 3) Usage(1);
const char* key_str = argv[2];
- key_serial_t key;
- if (!android::base::ParseInt(key_str, &key)) {
- fprintf(stderr, "Unparsable key: '%s'\n", key_str);
- return 1;
- }
+ key_serial_t key = parseKeyOrDie(key_str);
std::string context = RetrieveSecurityContext(key);
if (context.empty()) {
perror(key_str);