diff options
| author | Elliott Hughes <enh@google.com> | 2017-05-09 17:09:06 -0700 |
|---|---|---|
| committer | Elliott Hughes <enh@google.com> | 2017-05-10 14:53:28 -0700 |
| commit | 1eeee966763d05c9710188542d1ca84e08fbb397 (patch) | |
| tree | 04160262a8d8bddb31e014e251deac8dcff17a87 /libkeyutils | |
| parent | bca78df5a4d555d9d413de92b0e295c15d8f381c (diff) | |
| download | system_core-1eeee966763d05c9710188542d1ca84e08fbb397.tar.gz system_core-1eeee966763d05c9710188542d1ca84e08fbb397.tar.bz2 system_core-1eeee966763d05c9710188542d1ca84e08fbb397.zip | |
Add libkeyutils.
Also move init over to it.
Bug: http://b/37991155
Test: builds+boots
Change-Id: I5113a9d96a5ce0a0f3bad71134d6cc4f7b41a57e
Diffstat (limited to 'libkeyutils')
| l--------- | libkeyutils/.clang-format | 1 | ||||
| -rw-r--r-- | libkeyutils/Android.bp | 16 | ||||
| -rw-r--r-- | libkeyutils/include/keyutils.h | 56 | ||||
| -rw-r--r-- | libkeyutils/keyutils.cpp | 71 | ||||
| -rw-r--r-- | libkeyutils/keyutils_test.cpp | 46 |
5 files changed, 190 insertions, 0 deletions
diff --git a/libkeyutils/.clang-format b/libkeyutils/.clang-format new file mode 120000 index 000000000..fd0645fdf --- /dev/null +++ b/libkeyutils/.clang-format @@ -0,0 +1 @@ +../.clang-format-2
\ No newline at end of file diff --git a/libkeyutils/Android.bp b/libkeyutils/Android.bp new file mode 100644 index 000000000..028525925 --- /dev/null +++ b/libkeyutils/Android.bp @@ -0,0 +1,16 @@ +cc_library { + name: "libkeyutils", + cflags: ["-Werror"], + defaults: ["linux_bionic_supported"], + export_include_dirs: ["include/"], + local_include_dirs: ["include/"], + srcs: ["keyutils.cpp"], + stl: "none", +} + +cc_test { + name: "libkeyutils-tests", + cflags: ["-Werror"], + shared_libs: ["libkeyutils"], + srcs: ["keyutils_test.cpp"], +} diff --git a/libkeyutils/include/keyutils.h b/libkeyutils/include/keyutils.h new file mode 100644 index 000000000..585767d7a --- /dev/null +++ b/libkeyutils/include/keyutils.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _KEYUTILS_H_ +#define _KEYUTILS_H_ + +#include <linux/keyctl.h> +#include <stdint.h> +#include <sys/cdefs.h> + +__BEGIN_DECLS + +typedef int32_t key_serial_t; + +key_serial_t add_key(const char* type, const char* description, const void* payload, + size_t payload_length, key_serial_t ring_id); + +key_serial_t keyctl_get_keyring_ID(key_serial_t id, int create); + +long keyctl_revoke(key_serial_t id); /* TODO: remove this */ + +long keyctl_search(key_serial_t ring_id, const char* type, const char* description, + key_serial_t dest_ring_id); + +long keyctl_setperm(key_serial_t id, int permissions); + +long keyctl_unlink(key_serial_t key, key_serial_t keyring); + +__END_DECLS + +#endif diff --git a/libkeyutils/keyutils.cpp b/libkeyutils/keyutils.cpp new file mode 100644 index 000000000..58a2a17b0 --- /dev/null +++ b/libkeyutils/keyutils.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <keyutils.h> + +#include <stdarg.h> +#include <sys/syscall.h> +#include <unistd.h> + +// Deliberately not exposed. Callers should use the typed APIs instead. +static long keyctl(int cmd, ...) { + va_list va; + va_start(va, cmd); + unsigned long arg2 = va_arg(va, unsigned long); + unsigned long arg3 = va_arg(va, unsigned long); + unsigned long arg4 = va_arg(va, unsigned long); + unsigned long arg5 = va_arg(va, unsigned long); + va_end(va); + return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5); +} + +key_serial_t add_key(const char* type, const char* description, const void* payload, + size_t payload_length, key_serial_t ring_id) { + return syscall(__NR_add_key, type, description, payload, payload_length, ring_id); +} + +key_serial_t keyctl_get_keyring_ID(key_serial_t id, int create) { + return keyctl(KEYCTL_GET_KEYRING_ID, id, create); +} + +long keyctl_revoke(key_serial_t id) { + return keyctl(KEYCTL_REVOKE, id); +} + +long keyctl_search(key_serial_t ring_id, const char* type, const char* description, + key_serial_t dest_ring_id) { + return keyctl(KEYCTL_SEARCH, ring_id, type, description, dest_ring_id); +} + +long keyctl_setperm(key_serial_t id, int permissions) { + return keyctl(KEYCTL_SETPERM, id, permissions); +} + +long keyctl_unlink(key_serial_t key, key_serial_t keyring) { + return keyctl(KEYCTL_UNLINK, key, keyring); +} diff --git a/libkeyutils/keyutils_test.cpp b/libkeyutils/keyutils_test.cpp new file mode 100644 index 000000000..d41c91b68 --- /dev/null +++ b/libkeyutils/keyutils_test.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <keyutils.h> + +#include <dlfcn.h> + +#include <gtest/gtest.h> + +TEST(keyutils, smoke) { + // Check that the exported type is sane. + ASSERT_EQ(4U, sizeof(key_serial_t)); + + // Check that all the functions actually exist. + ASSERT_TRUE(dlsym(nullptr, "add_key") != nullptr); + ASSERT_TRUE(dlsym(nullptr, "keyctl_get_keyring_ID") != nullptr); + ASSERT_TRUE(dlsym(nullptr, "keyctl_revoke") != nullptr); + ASSERT_TRUE(dlsym(nullptr, "keyctl_search") != nullptr); + ASSERT_TRUE(dlsym(nullptr, "keyctl_setperm") != nullptr); + ASSERT_TRUE(dlsym(nullptr, "keyctl_unlink") != nullptr); +} |
