0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LINUX_KEY_H
0011 #define _LINUX_KEY_H
0012
0013 #include <linux/types.h>
0014 #include <linux/list.h>
0015 #include <linux/rbtree.h>
0016 #include <linux/rcupdate.h>
0017 #include <linux/sysctl.h>
0018 #include <linux/rwsem.h>
0019 #include <linux/atomic.h>
0020 #include <linux/assoc_array.h>
0021 #include <linux/refcount.h>
0022 #include <linux/time64.h>
0023
0024 #ifdef __KERNEL__
0025 #include <linux/uidgid.h>
0026
0027
0028 typedef int32_t key_serial_t;
0029
0030
0031 typedef uint32_t key_perm_t;
0032
0033 struct key;
0034 struct net;
0035
0036 #ifdef CONFIG_KEYS
0037
0038 #undef KEY_DEBUGGING
0039
0040 #define KEY_POS_VIEW 0x01000000
0041 #define KEY_POS_READ 0x02000000
0042 #define KEY_POS_WRITE 0x04000000
0043 #define KEY_POS_SEARCH 0x08000000
0044 #define KEY_POS_LINK 0x10000000
0045 #define KEY_POS_SETATTR 0x20000000
0046 #define KEY_POS_ALL 0x3f000000
0047
0048 #define KEY_USR_VIEW 0x00010000
0049 #define KEY_USR_READ 0x00020000
0050 #define KEY_USR_WRITE 0x00040000
0051 #define KEY_USR_SEARCH 0x00080000
0052 #define KEY_USR_LINK 0x00100000
0053 #define KEY_USR_SETATTR 0x00200000
0054 #define KEY_USR_ALL 0x003f0000
0055
0056 #define KEY_GRP_VIEW 0x00000100
0057 #define KEY_GRP_READ 0x00000200
0058 #define KEY_GRP_WRITE 0x00000400
0059 #define KEY_GRP_SEARCH 0x00000800
0060 #define KEY_GRP_LINK 0x00001000
0061 #define KEY_GRP_SETATTR 0x00002000
0062 #define KEY_GRP_ALL 0x00003f00
0063
0064 #define KEY_OTH_VIEW 0x00000001
0065 #define KEY_OTH_READ 0x00000002
0066 #define KEY_OTH_WRITE 0x00000004
0067 #define KEY_OTH_SEARCH 0x00000008
0068 #define KEY_OTH_LINK 0x00000010
0069 #define KEY_OTH_SETATTR 0x00000020
0070 #define KEY_OTH_ALL 0x0000003f
0071
0072 #define KEY_PERM_UNDEF 0xffffffff
0073
0074
0075
0076
0077 enum key_need_perm {
0078 KEY_NEED_UNSPECIFIED,
0079 KEY_NEED_VIEW,
0080 KEY_NEED_READ,
0081 KEY_NEED_WRITE,
0082 KEY_NEED_SEARCH,
0083 KEY_NEED_LINK,
0084 KEY_NEED_SETATTR,
0085 KEY_NEED_UNLINK,
0086 KEY_SYSADMIN_OVERRIDE,
0087 KEY_AUTHTOKEN_OVERRIDE,
0088 KEY_DEFER_PERM_CHECK,
0089 };
0090
0091 struct seq_file;
0092 struct user_struct;
0093 struct signal_struct;
0094 struct cred;
0095
0096 struct key_type;
0097 struct key_owner;
0098 struct key_tag;
0099 struct keyring_list;
0100 struct keyring_name;
0101
0102 struct key_tag {
0103 struct rcu_head rcu;
0104 refcount_t usage;
0105 bool removed;
0106 };
0107
0108 struct keyring_index_key {
0109
0110 unsigned long hash;
0111 union {
0112 struct {
0113 #ifdef __LITTLE_ENDIAN
0114 u16 desc_len;
0115 char desc[sizeof(long) - 2];
0116 #else
0117 char desc[sizeof(long) - 2];
0118 u16 desc_len;
0119 #endif
0120 };
0121 unsigned long x;
0122 };
0123 struct key_type *type;
0124 struct key_tag *domain_tag;
0125 const char *description;
0126 };
0127
0128 union key_payload {
0129 void __rcu *rcu_data0;
0130 void *data[4];
0131 };
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 typedef struct __key_reference_with_attributes *key_ref_t;
0148
0149 static inline key_ref_t make_key_ref(const struct key *key,
0150 bool possession)
0151 {
0152 return (key_ref_t) ((unsigned long) key | possession);
0153 }
0154
0155 static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
0156 {
0157 return (struct key *) ((unsigned long) key_ref & ~1UL);
0158 }
0159
0160 static inline bool is_key_possessed(const key_ref_t key_ref)
0161 {
0162 return (unsigned long) key_ref & 1UL;
0163 }
0164
0165 typedef int (*key_restrict_link_func_t)(struct key *dest_keyring,
0166 const struct key_type *type,
0167 const union key_payload *payload,
0168 struct key *restriction_key);
0169
0170 struct key_restriction {
0171 key_restrict_link_func_t check;
0172 struct key *key;
0173 struct key_type *keytype;
0174 };
0175
0176 enum key_state {
0177 KEY_IS_UNINSTANTIATED,
0178 KEY_IS_POSITIVE,
0179 };
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189 struct key {
0190 refcount_t usage;
0191 key_serial_t serial;
0192 union {
0193 struct list_head graveyard_link;
0194 struct rb_node serial_node;
0195 };
0196 #ifdef CONFIG_KEY_NOTIFICATIONS
0197 struct watch_list *watchers;
0198 #endif
0199 struct rw_semaphore sem;
0200 struct key_user *user;
0201 void *security;
0202 union {
0203 time64_t expiry;
0204 time64_t revoked_at;
0205 };
0206 time64_t last_used_at;
0207 kuid_t uid;
0208 kgid_t gid;
0209 key_perm_t perm;
0210 unsigned short quotalen;
0211 unsigned short datalen;
0212
0213
0214
0215 short state;
0216
0217 #ifdef KEY_DEBUGGING
0218 unsigned magic;
0219 #define KEY_DEBUG_MAGIC 0x18273645u
0220 #endif
0221
0222 unsigned long flags;
0223 #define KEY_FLAG_DEAD 0
0224 #define KEY_FLAG_REVOKED 1
0225 #define KEY_FLAG_IN_QUOTA 2
0226 #define KEY_FLAG_USER_CONSTRUCT 3
0227 #define KEY_FLAG_ROOT_CAN_CLEAR 4
0228 #define KEY_FLAG_INVALIDATED 5
0229 #define KEY_FLAG_BUILTIN 6
0230 #define KEY_FLAG_ROOT_CAN_INVAL 7
0231 #define KEY_FLAG_KEEP 8
0232 #define KEY_FLAG_UID_KEYRING 9
0233
0234
0235
0236
0237
0238
0239 union {
0240 struct keyring_index_key index_key;
0241 struct {
0242 unsigned long hash;
0243 unsigned long len_desc;
0244 struct key_type *type;
0245 struct key_tag *domain_tag;
0246 char *description;
0247 };
0248 };
0249
0250
0251
0252
0253
0254 union {
0255 union key_payload payload;
0256 struct {
0257
0258 struct list_head name_link;
0259 struct assoc_array keys;
0260 };
0261 };
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274 struct key_restriction *restrict_link;
0275 };
0276
0277 extern struct key *key_alloc(struct key_type *type,
0278 const char *desc,
0279 kuid_t uid, kgid_t gid,
0280 const struct cred *cred,
0281 key_perm_t perm,
0282 unsigned long flags,
0283 struct key_restriction *restrict_link);
0284
0285
0286 #define KEY_ALLOC_IN_QUOTA 0x0000
0287 #define KEY_ALLOC_QUOTA_OVERRUN 0x0001
0288 #define KEY_ALLOC_NOT_IN_QUOTA 0x0002
0289 #define KEY_ALLOC_BUILT_IN 0x0004
0290 #define KEY_ALLOC_BYPASS_RESTRICTION 0x0008
0291 #define KEY_ALLOC_UID_KEYRING 0x0010
0292 #define KEY_ALLOC_SET_KEEP 0x0020
0293
0294 extern void key_revoke(struct key *key);
0295 extern void key_invalidate(struct key *key);
0296 extern void key_put(struct key *key);
0297 extern bool key_put_tag(struct key_tag *tag);
0298 extern void key_remove_domain(struct key_tag *domain_tag);
0299
0300 static inline struct key *__key_get(struct key *key)
0301 {
0302 refcount_inc(&key->usage);
0303 return key;
0304 }
0305
0306 static inline struct key *key_get(struct key *key)
0307 {
0308 return key ? __key_get(key) : key;
0309 }
0310
0311 static inline void key_ref_put(key_ref_t key_ref)
0312 {
0313 key_put(key_ref_to_ptr(key_ref));
0314 }
0315
0316 extern struct key *request_key_tag(struct key_type *type,
0317 const char *description,
0318 struct key_tag *domain_tag,
0319 const char *callout_info);
0320
0321 extern struct key *request_key_rcu(struct key_type *type,
0322 const char *description,
0323 struct key_tag *domain_tag);
0324
0325 extern struct key *request_key_with_auxdata(struct key_type *type,
0326 const char *description,
0327 struct key_tag *domain_tag,
0328 const void *callout_info,
0329 size_t callout_len,
0330 void *aux);
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340 static inline struct key *request_key(struct key_type *type,
0341 const char *description,
0342 const char *callout_info)
0343 {
0344 return request_key_tag(type, description, NULL, callout_info);
0345 }
0346
0347 #ifdef CONFIG_NET
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363 #define request_key_net(type, description, net, callout_info) \
0364 request_key_tag(type, description, net->key_domain, callout_info)
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375 #define request_key_net_rcu(type, description, net) \
0376 request_key_rcu(type, description, net->key_domain)
0377 #endif
0378
0379 extern int wait_for_key_construction(struct key *key, bool intr);
0380
0381 extern int key_validate(const struct key *key);
0382
0383 extern key_ref_t key_create_or_update(key_ref_t keyring,
0384 const char *type,
0385 const char *description,
0386 const void *payload,
0387 size_t plen,
0388 key_perm_t perm,
0389 unsigned long flags);
0390
0391 extern int key_update(key_ref_t key,
0392 const void *payload,
0393 size_t plen);
0394
0395 extern int key_link(struct key *keyring,
0396 struct key *key);
0397
0398 extern int key_move(struct key *key,
0399 struct key *from_keyring,
0400 struct key *to_keyring,
0401 unsigned int flags);
0402
0403 extern int key_unlink(struct key *keyring,
0404 struct key *key);
0405
0406 extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
0407 const struct cred *cred,
0408 key_perm_t perm,
0409 unsigned long flags,
0410 struct key_restriction *restrict_link,
0411 struct key *dest);
0412
0413 extern int restrict_link_reject(struct key *keyring,
0414 const struct key_type *type,
0415 const union key_payload *payload,
0416 struct key *restriction_key);
0417
0418 extern int keyring_clear(struct key *keyring);
0419
0420 extern key_ref_t keyring_search(key_ref_t keyring,
0421 struct key_type *type,
0422 const char *description,
0423 bool recurse);
0424
0425 extern int keyring_add_key(struct key *keyring,
0426 struct key *key);
0427
0428 extern int keyring_restrict(key_ref_t keyring, const char *type,
0429 const char *restriction);
0430
0431 extern struct key *key_lookup(key_serial_t id);
0432
0433 static inline key_serial_t key_serial(const struct key *key)
0434 {
0435 return key ? key->serial : 0;
0436 }
0437
0438 extern void key_set_timeout(struct key *, unsigned);
0439
0440 extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
0441 enum key_need_perm need_perm);
0442 extern void key_free_user_ns(struct user_namespace *);
0443
0444 static inline short key_read_state(const struct key *key)
0445 {
0446
0447 return smp_load_acquire(&key->state);
0448 }
0449
0450
0451
0452
0453
0454
0455
0456
0457 static inline bool key_is_positive(const struct key *key)
0458 {
0459 return key_read_state(key) == KEY_IS_POSITIVE;
0460 }
0461
0462 static inline bool key_is_negative(const struct key *key)
0463 {
0464 return key_read_state(key) < 0;
0465 }
0466
0467 #define dereference_key_rcu(KEY) \
0468 (rcu_dereference((KEY)->payload.rcu_data0))
0469
0470 #define dereference_key_locked(KEY) \
0471 (rcu_dereference_protected((KEY)->payload.rcu_data0, \
0472 rwsem_is_locked(&((struct key *)(KEY))->sem)))
0473
0474 #define rcu_assign_keypointer(KEY, PAYLOAD) \
0475 do { \
0476 rcu_assign_pointer((KEY)->payload.rcu_data0, (PAYLOAD)); \
0477 } while (0)
0478
0479 #ifdef CONFIG_SYSCTL
0480 extern struct ctl_table key_sysctls[];
0481 #endif
0482
0483
0484
0485 extern int install_thread_keyring_to_cred(struct cred *cred);
0486 extern void key_fsuid_changed(struct cred *new_cred);
0487 extern void key_fsgid_changed(struct cred *new_cred);
0488 extern void key_init(void);
0489
0490 #else
0491
0492 #define key_validate(k) 0
0493 #define key_serial(k) 0
0494 #define key_get(k) ({ NULL; })
0495 #define key_revoke(k) do { } while(0)
0496 #define key_invalidate(k) do { } while(0)
0497 #define key_put(k) do { } while(0)
0498 #define key_ref_put(k) do { } while(0)
0499 #define make_key_ref(k, p) NULL
0500 #define key_ref_to_ptr(k) NULL
0501 #define is_key_possessed(k) 0
0502 #define key_fsuid_changed(c) do { } while(0)
0503 #define key_fsgid_changed(c) do { } while(0)
0504 #define key_init() do { } while(0)
0505 #define key_free_user_ns(ns) do { } while(0)
0506 #define key_remove_domain(d) do { } while(0)
0507
0508 #endif
0509 #endif
0510 #endif