1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci#ifndef LAPI_KEYCTL_H__ 7f08c3bdfSopenharmony_ci#define LAPI_KEYCTL_H__ 8f08c3bdfSopenharmony_ci 9f08c3bdfSopenharmony_ci#include "config.h" 10f08c3bdfSopenharmony_ci 11f08c3bdfSopenharmony_ci#if defined(HAVE_KEYUTILS_H) && defined(HAVE_LIBKEYUTILS) 12f08c3bdfSopenharmony_ci# include <keyutils.h> 13f08c3bdfSopenharmony_ci#else 14f08c3bdfSopenharmony_ci# ifdef HAVE_LINUX_KEYCTL_H 15f08c3bdfSopenharmony_ci# include <linux/keyctl.h> 16f08c3bdfSopenharmony_ci# endif /* HAVE_LINUX_KEYCTL_H */ 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci# include <stdarg.h> 19f08c3bdfSopenharmony_ci# include <stdint.h> 20f08c3bdfSopenharmony_ci# include "lapi/syscalls.h" 21f08c3bdfSopenharmony_citypedef int32_t key_serial_t; 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_cistatic inline key_serial_t add_key(const char *type, 24f08c3bdfSopenharmony_ci const char *description, 25f08c3bdfSopenharmony_ci const void *payload, 26f08c3bdfSopenharmony_ci size_t plen, 27f08c3bdfSopenharmony_ci key_serial_t ringid) 28f08c3bdfSopenharmony_ci{ 29f08c3bdfSopenharmony_ci return tst_syscall(__NR_add_key, 30f08c3bdfSopenharmony_ci type, description, payload, plen, ringid); 31f08c3bdfSopenharmony_ci} 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_cistatic inline key_serial_t request_key(const char *type, 34f08c3bdfSopenharmony_ci const char *description, 35f08c3bdfSopenharmony_ci const char *callout_info, 36f08c3bdfSopenharmony_ci key_serial_t destringid) 37f08c3bdfSopenharmony_ci{ 38f08c3bdfSopenharmony_ci return tst_syscall(__NR_request_key, 39f08c3bdfSopenharmony_ci type, description, callout_info, destringid); 40f08c3bdfSopenharmony_ci} 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cistatic inline long keyctl(int cmd, ...) 43f08c3bdfSopenharmony_ci{ 44f08c3bdfSopenharmony_ci va_list va; 45f08c3bdfSopenharmony_ci unsigned long arg2, arg3, arg4, arg5; 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci va_start(va, cmd); 48f08c3bdfSopenharmony_ci arg2 = va_arg(va, unsigned long); 49f08c3bdfSopenharmony_ci arg3 = va_arg(va, unsigned long); 50f08c3bdfSopenharmony_ci arg4 = va_arg(va, unsigned long); 51f08c3bdfSopenharmony_ci arg5 = va_arg(va, unsigned long); 52f08c3bdfSopenharmony_ci va_end(va); 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci return tst_syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5); 55f08c3bdfSopenharmony_ci} 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_cistatic inline key_serial_t keyctl_join_session_keyring(const char *name) { 58f08c3bdfSopenharmony_ci return keyctl(KEYCTL_JOIN_SESSION_KEYRING, name); 59f08c3bdfSopenharmony_ci} 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci#endif /* defined(HAVE_KEYUTILS_H) && defined(HAVE_LIBKEYUTILS) */ 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci/* special process keyring shortcut IDs */ 64f08c3bdfSopenharmony_ci#ifndef KEY_SPEC_THREAD_KEYRING 65f08c3bdfSopenharmony_ci# define KEY_SPEC_THREAD_KEYRING -1 66f08c3bdfSopenharmony_ci#endif 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci#ifndef KEY_SPEC_PROCESS_KEYRING 69f08c3bdfSopenharmony_ci# define KEY_SPEC_PROCESS_KEYRING -2 70f08c3bdfSopenharmony_ci#endif 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci#ifndef KEY_SPEC_SESSION_KEYRING 73f08c3bdfSopenharmony_ci# define KEY_SPEC_SESSION_KEYRING -3 74f08c3bdfSopenharmony_ci#endif 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci#ifndef KEY_SPEC_USER_KEYRING 77f08c3bdfSopenharmony_ci# define KEY_SPEC_USER_KEYRING -4 78f08c3bdfSopenharmony_ci#endif 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci#ifndef KEY_SPEC_USER_SESSION_KEYRING 82f08c3bdfSopenharmony_ci# define KEY_SPEC_USER_SESSION_KEYRING -5 83f08c3bdfSopenharmony_ci#endif 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci/* request-key default keyrings */ 86f08c3bdfSopenharmony_ci#ifndef KEY_REQKEY_DEFL_THREAD_KEYRING 87f08c3bdfSopenharmony_ci# define KEY_REQKEY_DEFL_THREAD_KEYRING 1 88f08c3bdfSopenharmony_ci#endif 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci#ifndef KEY_REQKEY_DEFL_SESSION_KEYRING 91f08c3bdfSopenharmony_ci# define KEY_REQKEY_DEFL_SESSION_KEYRING 3 92f08c3bdfSopenharmony_ci#endif 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci#ifndef KEY_REQKEY_DEFL_DEFAULT 95f08c3bdfSopenharmony_ci# define KEY_REQKEY_DEFL_DEFAULT 0 96f08c3bdfSopenharmony_ci#endif 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci/* keyctl commands */ 99f08c3bdfSopenharmony_ci#ifndef KEYCTL_GET_KEYRING_ID 100f08c3bdfSopenharmony_ci# define KEYCTL_GET_KEYRING_ID 0 101f08c3bdfSopenharmony_ci#endif 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_ci#ifndef KEYCTL_JOIN_SESSION_KEYRING 104f08c3bdfSopenharmony_ci# define KEYCTL_JOIN_SESSION_KEYRING 1 105f08c3bdfSopenharmony_ci#endif 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci#ifndef KEYCTL_UPDATE 108f08c3bdfSopenharmony_ci# define KEYCTL_UPDATE 2 109f08c3bdfSopenharmony_ci#endif 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci#ifndef KEYCTL_REVOKE 112f08c3bdfSopenharmony_ci# define KEYCTL_REVOKE 3 113f08c3bdfSopenharmony_ci#endif 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci#ifndef KEYCTL_SETPERM 116f08c3bdfSopenharmony_ci# define KEYCTL_SETPERM 5 117f08c3bdfSopenharmony_ci#endif 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci#ifndef KEYCTL_CLEAR 120f08c3bdfSopenharmony_ci# define KEYCTL_CLEAR 7 121f08c3bdfSopenharmony_ci#endif 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_ci#ifndef KEYCTL_UNLINK 124f08c3bdfSopenharmony_ci# define KEYCTL_UNLINK 9 125f08c3bdfSopenharmony_ci#endif 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_ci#ifndef KEYCTL_READ 128f08c3bdfSopenharmony_ci# define KEYCTL_READ 11 129f08c3bdfSopenharmony_ci#endif 130f08c3bdfSopenharmony_ci 131f08c3bdfSopenharmony_ci#ifndef KEYCTL_SET_REQKEY_KEYRING 132f08c3bdfSopenharmony_ci# define KEYCTL_SET_REQKEY_KEYRING 14 133f08c3bdfSopenharmony_ci#endif 134f08c3bdfSopenharmony_ci 135f08c3bdfSopenharmony_ci#ifndef KEYCTL_SET_TIMEOUT 136f08c3bdfSopenharmony_ci# define KEYCTL_SET_TIMEOUT 15 137f08c3bdfSopenharmony_ci#endif 138f08c3bdfSopenharmony_ci 139f08c3bdfSopenharmony_ci#ifndef KEYCTL_INVALIDATE 140f08c3bdfSopenharmony_ci# define KEYCTL_INVALIDATE 21 141f08c3bdfSopenharmony_ci#endif 142f08c3bdfSopenharmony_ci 143f08c3bdfSopenharmony_ci#ifndef KEYCTL_WATCH_KEY 144f08c3bdfSopenharmony_ci# define KEYCTL_WATCH_KEY 32 145f08c3bdfSopenharmony_ci#endif 146f08c3bdfSopenharmony_ci 147f08c3bdfSopenharmony_ci/* key permissions */ 148f08c3bdfSopenharmony_ci#ifndef KEY_POS_VIEW 149f08c3bdfSopenharmony_ci# define KEY_POS_VIEW 0x01000000 150f08c3bdfSopenharmony_ci# define KEY_POS_READ 0x02000000 151f08c3bdfSopenharmony_ci# define KEY_POS_WRITE 0x04000000 152f08c3bdfSopenharmony_ci# define KEY_POS_SEARCH 0x08000000 153f08c3bdfSopenharmony_ci# define KEY_POS_LINK 0x10000000 154f08c3bdfSopenharmony_ci# define KEY_POS_SETATTR 0x20000000 155f08c3bdfSopenharmony_ci# define KEY_POS_ALL 0x3f000000 156f08c3bdfSopenharmony_ci 157f08c3bdfSopenharmony_ci# define KEY_USR_VIEW 0x00010000 158f08c3bdfSopenharmony_ci# define KEY_USR_READ 0x00020000 159f08c3bdfSopenharmony_ci# define KEY_USR_WRITE 0x00040000 160f08c3bdfSopenharmony_ci# define KEY_USR_SEARCH 0x00080000 161f08c3bdfSopenharmony_ci# define KEY_USR_LINK 0x00100000 162f08c3bdfSopenharmony_ci# define KEY_USR_SETATTR 0x00200000 163f08c3bdfSopenharmony_ci# define KEY_USR_ALL 0x003f0000 164f08c3bdfSopenharmony_ci 165f08c3bdfSopenharmony_ci# define KEY_GRP_VIEW 0x00000100 166f08c3bdfSopenharmony_ci# define KEY_GRP_READ 0x00000200 167f08c3bdfSopenharmony_ci# define KEY_GRP_WRITE 0x00000400 168f08c3bdfSopenharmony_ci# define KEY_GRP_SEARCH 0x00000800 169f08c3bdfSopenharmony_ci# define KEY_GRP_LINK 0x00001000 170f08c3bdfSopenharmony_ci# define KEY_GRP_SETATTR 0x00002000 171f08c3bdfSopenharmony_ci# define KEY_GRP_ALL 0x00003f00 172f08c3bdfSopenharmony_ci 173f08c3bdfSopenharmony_ci# define KEY_OTH_VIEW 0x00000001 174f08c3bdfSopenharmony_ci# define KEY_OTH_READ 0x00000002 175f08c3bdfSopenharmony_ci# define KEY_OTH_WRITE 0x00000004 176f08c3bdfSopenharmony_ci# define KEY_OTH_SEARCH 0x00000008 177f08c3bdfSopenharmony_ci# define KEY_OTH_LINK 0x00000010 178f08c3bdfSopenharmony_ci# define KEY_OTH_SETATTR 0x00000020 179f08c3bdfSopenharmony_ci# define KEY_OTH_ALL 0x0000003f 180f08c3bdfSopenharmony_ci#endif /* !KEY_POS_VIEW */ 181f08c3bdfSopenharmony_ci 182f08c3bdfSopenharmony_ci#endif /* LAPI_KEYCTL_H__ */ 183