1973c9f4fSDavid Howells /* Authentication token and access key management internal defs 21da177e4SLinus Torvalds * 376181c13SDavid Howells * Copyright (C) 2003-5, 2007 Red Hat, Inc. All Rights Reserved. 41da177e4SLinus Torvalds * Written by David Howells (dhowells@redhat.com) 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 71da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 81da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 91da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #ifndef _INTERNAL_H 131da177e4SLinus Torvalds #define _INTERNAL_H 141da177e4SLinus Torvalds 15d84f4f99SDavid Howells #include <linux/sched.h> 1676181c13SDavid Howells #include <linux/key-type.h> 17413cd3d9SOleg Nesterov #include <linux/task_work.h> 181da177e4SLinus Torvalds 1976181c13SDavid Howells #ifdef __KDEBUG 2076181c13SDavid Howells #define kenter(FMT, ...) \ 21dd6f953aSHarvey Harrison printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) 2276181c13SDavid Howells #define kleave(FMT, ...) \ 23dd6f953aSHarvey Harrison printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) 2476181c13SDavid Howells #define kdebug(FMT, ...) \ 25d84f4f99SDavid Howells printk(KERN_DEBUG " "FMT"\n", ##__VA_ARGS__) 263e30148cSDavid Howells #else 2776181c13SDavid Howells #define kenter(FMT, ...) \ 28dd6f953aSHarvey Harrison no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) 2976181c13SDavid Howells #define kleave(FMT, ...) \ 30dd6f953aSHarvey Harrison no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) 3176181c13SDavid Howells #define kdebug(FMT, ...) \ 3276181c13SDavid Howells no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__) 333e30148cSDavid Howells #endif 343e30148cSDavid Howells 350c061b57SDavid Howells extern struct key_type key_type_dead; 361da177e4SLinus Torvalds extern struct key_type key_type_user; 379f6ed2caSJeff Layton extern struct key_type key_type_logon; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds /*****************************************************************************/ 401da177e4SLinus Torvalds /* 41973c9f4fSDavid Howells * Keep track of keys for a user. 42973c9f4fSDavid Howells * 43973c9f4fSDavid Howells * This needs to be separate to user_struct to avoid a refcount-loop 44973c9f4fSDavid Howells * (user_struct pins some keyrings which pin this struct). 45973c9f4fSDavid Howells * 46973c9f4fSDavid Howells * We also keep track of keys under request from userspace for this UID here. 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds struct key_user { 491da177e4SLinus Torvalds struct rb_node node; 5076181c13SDavid Howells struct mutex cons_lock; /* construction initiation lock */ 511da177e4SLinus Torvalds spinlock_t lock; 521da177e4SLinus Torvalds atomic_t usage; /* for accessing qnkeys & qnbytes */ 531da177e4SLinus Torvalds atomic_t nkeys; /* number of keys */ 541da177e4SLinus Torvalds atomic_t nikeys; /* number of instantiated keys */ 559a56c2dbSEric W. Biederman kuid_t uid; 561da177e4SLinus Torvalds int qnkeys; /* number of keys allocated to this user */ 571da177e4SLinus Torvalds int qnbytes; /* number of bytes allocated to this user */ 581da177e4SLinus Torvalds }; 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds extern struct rb_root key_user_tree; 611da177e4SLinus Torvalds extern spinlock_t key_user_lock; 621da177e4SLinus Torvalds extern struct key_user root_key_user; 631da177e4SLinus Torvalds 649a56c2dbSEric W. Biederman extern struct key_user *key_user_lookup(kuid_t uid); 651da177e4SLinus Torvalds extern void key_user_put(struct key_user *user); 661da177e4SLinus Torvalds 670b77f5bfSDavid Howells /* 68973c9f4fSDavid Howells * Key quota limits. 690b77f5bfSDavid Howells * - root has its own separate limits to everyone else 700b77f5bfSDavid Howells */ 710b77f5bfSDavid Howells extern unsigned key_quota_root_maxkeys; 720b77f5bfSDavid Howells extern unsigned key_quota_root_maxbytes; 730b77f5bfSDavid Howells extern unsigned key_quota_maxkeys; 740b77f5bfSDavid Howells extern unsigned key_quota_maxbytes; 750b77f5bfSDavid Howells 760b77f5bfSDavid Howells #define KEYQUOTA_LINK_BYTES 4 /* a link in a keyring is worth 4 bytes */ 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds 798bc16deaSDavid Howells extern struct kmem_cache *key_jar; 801da177e4SLinus Torvalds extern struct rb_root key_serial_tree; 811da177e4SLinus Torvalds extern spinlock_t key_serial_lock; 8276181c13SDavid Howells extern struct mutex key_construction_mutex; 831da177e4SLinus Torvalds extern wait_queue_head_t request_key_conswq; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds 86e9e349b0SDavid Howells extern struct key_type *key_type_lookup(const char *type); 87e9e349b0SDavid Howells extern void key_type_put(struct key_type *ktype); 88e9e349b0SDavid Howells 89f70e2e06SDavid Howells extern int __key_link_begin(struct key *keyring, 90f70e2e06SDavid Howells const struct key_type *type, 91f70e2e06SDavid Howells const char *description, 92ceb73c12SDavid Howells unsigned long *_prealloc); 93f70e2e06SDavid Howells extern int __key_link_check_live_key(struct key *keyring, struct key *key); 94f70e2e06SDavid Howells extern void __key_link(struct key *keyring, struct key *key, 95ceb73c12SDavid Howells unsigned long *_prealloc); 96f70e2e06SDavid Howells extern void __key_link_end(struct key *keyring, 97f70e2e06SDavid Howells struct key_type *type, 98ceb73c12SDavid Howells unsigned long prealloc); 991da177e4SLinus Torvalds 100664cceb0SDavid Howells extern key_ref_t __keyring_search_one(key_ref_t keyring_ref, 1011da177e4SLinus Torvalds const struct key_type *type, 1021da177e4SLinus Torvalds const char *description, 1031da177e4SLinus Torvalds key_perm_t perm); 1041da177e4SLinus Torvalds 1053e30148cSDavid Howells extern struct key *keyring_search_instkey(struct key *keyring, 1063e30148cSDavid Howells key_serial_t target_id); 1073e30148cSDavid Howells 1081da177e4SLinus Torvalds typedef int (*key_match_func_t)(const struct key *, const void *); 1091da177e4SLinus Torvalds 110664cceb0SDavid Howells extern key_ref_t keyring_search_aux(key_ref_t keyring_ref, 111d84f4f99SDavid Howells const struct cred *cred, 1121da177e4SLinus Torvalds struct key_type *type, 1131da177e4SLinus Torvalds const void *description, 11478b7280cSDavid Howells key_match_func_t match, 11578b7280cSDavid Howells bool no_state_check); 1161da177e4SLinus Torvalds 117927942aaSDavid Howells extern key_ref_t search_my_process_keyrings(struct key_type *type, 118927942aaSDavid Howells const void *description, 119927942aaSDavid Howells key_match_func_t match, 12078b7280cSDavid Howells bool no_state_check, 121927942aaSDavid Howells const struct cred *cred); 122664cceb0SDavid Howells extern key_ref_t search_process_keyrings(struct key_type *type, 1231da177e4SLinus Torvalds const void *description, 1243e30148cSDavid Howells key_match_func_t match, 125d84f4f99SDavid Howells const struct cred *cred); 1261da177e4SLinus Torvalds 12769664cf1SDavid Howells extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check); 1281da177e4SLinus Torvalds 1298bbf4976SDavid Howells extern int install_user_keyrings(void); 130d84f4f99SDavid Howells extern int install_thread_keyring_to_cred(struct cred *); 131d84f4f99SDavid Howells extern int install_process_keyring_to_cred(struct cred *); 132685bfd2cSOleg Nesterov extern int install_session_keyring_to_cred(struct cred *, struct key *); 1333e30148cSDavid Howells 1343e30148cSDavid Howells extern struct key *request_key_and_link(struct key_type *type, 1353e30148cSDavid Howells const char *description, 1364a38e122SDavid Howells const void *callout_info, 1374a38e122SDavid Howells size_t callout_len, 1384e54f085SDavid Howells void *aux, 1397e047ef5SDavid Howells struct key *dest_keyring, 1407e047ef5SDavid Howells unsigned long flags); 1413e30148cSDavid Howells 142927942aaSDavid Howells extern int lookup_user_key_possessed(const struct key *key, const void *target); 1435593122eSDavid Howells extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags, 144e9e349b0SDavid Howells key_perm_t perm); 1455593122eSDavid Howells #define KEY_LOOKUP_CREATE 0x01 1465593122eSDavid Howells #define KEY_LOOKUP_PARTIAL 0x02 1475593122eSDavid Howells #define KEY_LOOKUP_FOR_UNLINK 0x04 148e9e349b0SDavid Howells 149e9e349b0SDavid Howells extern long join_session_keyring(const char *name); 15067d12145SAl Viro extern void key_change_session_keyring(struct callback_head *twork); 151e9e349b0SDavid Howells 1520c061b57SDavid Howells extern struct work_struct key_gc_work; 1535d135440SDavid Howells extern unsigned key_gc_delay; 1545d135440SDavid Howells extern void keyring_gc(struct key *keyring, time_t limit); 155fd75815fSDavid Howells extern void key_schedule_gc(time_t gc_at); 156fd75815fSDavid Howells extern void key_schedule_gc_links(void); 1570c061b57SDavid Howells extern void key_gc_keytype(struct key_type *ktype); 1585d135440SDavid Howells 159e9e349b0SDavid Howells extern int key_task_permission(const key_ref_t key_ref, 160d84f4f99SDavid Howells const struct cred *cred, 161e9e349b0SDavid Howells key_perm_t perm); 162e9e349b0SDavid Howells 163973c9f4fSDavid Howells /* 164973c9f4fSDavid Howells * Check to see whether permission is granted to use a key in the desired way. 165973c9f4fSDavid Howells */ 166e9e349b0SDavid Howells static inline int key_permission(const key_ref_t key_ref, key_perm_t perm) 167e9e349b0SDavid Howells { 168d84f4f99SDavid Howells return key_task_permission(key_ref, current_cred(), perm); 169e9e349b0SDavid Howells } 170e9e349b0SDavid Howells 171e9e349b0SDavid Howells /* required permissions */ 172e9e349b0SDavid Howells #define KEY_VIEW 0x01 /* require permission to view attributes */ 173e9e349b0SDavid Howells #define KEY_READ 0x02 /* require permission to read content */ 174e9e349b0SDavid Howells #define KEY_WRITE 0x04 /* require permission to update / modify */ 175e9e349b0SDavid Howells #define KEY_SEARCH 0x08 /* require permission to search (keyring) or find (key) */ 176e9e349b0SDavid Howells #define KEY_LINK 0x10 /* require permission to link */ 177e9e349b0SDavid Howells #define KEY_SETATTR 0x20 /* require permission to change attributes */ 178e9e349b0SDavid Howells #define KEY_ALL 0x3f /* all the above permissions */ 179e9e349b0SDavid Howells 1803e30148cSDavid Howells /* 181973c9f4fSDavid Howells * Authorisation record for request_key(). 1823e30148cSDavid Howells */ 1833e30148cSDavid Howells struct request_key_auth { 1843e30148cSDavid Howells struct key *target_key; 1858bbf4976SDavid Howells struct key *dest_keyring; 186d84f4f99SDavid Howells const struct cred *cred; 1874a38e122SDavid Howells void *callout_info; 1884a38e122SDavid Howells size_t callout_len; 1893e30148cSDavid Howells pid_t pid; 1903e30148cSDavid Howells }; 1913e30148cSDavid Howells 1923e30148cSDavid Howells extern struct key_type key_type_request_key_auth; 1933e30148cSDavid Howells extern struct key *request_key_auth_new(struct key *target, 1944a38e122SDavid Howells const void *callout_info, 1958bbf4976SDavid Howells size_t callout_len, 1968bbf4976SDavid Howells struct key *dest_keyring); 1973e30148cSDavid Howells 1983e30148cSDavid Howells extern struct key *key_get_instantiation_authkey(key_serial_t target_id); 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* 201fd75815fSDavid Howells * Determine whether a key is dead. 202fd75815fSDavid Howells */ 203fd75815fSDavid Howells static inline bool key_is_dead(struct key *key, time_t limit) 204fd75815fSDavid Howells { 205fd75815fSDavid Howells return 206fd75815fSDavid Howells key->flags & ((1 << KEY_FLAG_DEAD) | 207fd75815fSDavid Howells (1 << KEY_FLAG_INVALIDATED)) || 208fd75815fSDavid Howells (key->expiry > 0 && key->expiry <= limit); 209fd75815fSDavid Howells } 210fd75815fSDavid Howells 211fd75815fSDavid Howells /* 212973c9f4fSDavid Howells * keyctl() functions 2131da177e4SLinus Torvalds */ 2141da177e4SLinus Torvalds extern long keyctl_get_keyring_ID(key_serial_t, int); 2151da177e4SLinus Torvalds extern long keyctl_join_session_keyring(const char __user *); 2161da177e4SLinus Torvalds extern long keyctl_update_key(key_serial_t, const void __user *, size_t); 2171da177e4SLinus Torvalds extern long keyctl_revoke_key(key_serial_t); 2181da177e4SLinus Torvalds extern long keyctl_keyring_clear(key_serial_t); 2191da177e4SLinus Torvalds extern long keyctl_keyring_link(key_serial_t, key_serial_t); 2201da177e4SLinus Torvalds extern long keyctl_keyring_unlink(key_serial_t, key_serial_t); 2211da177e4SLinus Torvalds extern long keyctl_describe_key(key_serial_t, char __user *, size_t); 2221da177e4SLinus Torvalds extern long keyctl_keyring_search(key_serial_t, const char __user *, 2231da177e4SLinus Torvalds const char __user *, key_serial_t); 2241da177e4SLinus Torvalds extern long keyctl_read_key(key_serial_t, char __user *, size_t); 2251da177e4SLinus Torvalds extern long keyctl_chown_key(key_serial_t, uid_t, gid_t); 2261da177e4SLinus Torvalds extern long keyctl_setperm_key(key_serial_t, key_perm_t); 2271da177e4SLinus Torvalds extern long keyctl_instantiate_key(key_serial_t, const void __user *, 2281da177e4SLinus Torvalds size_t, key_serial_t); 2291da177e4SLinus Torvalds extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t); 2303e30148cSDavid Howells extern long keyctl_set_reqkey_keyring(int); 231017679c4SDavid Howells extern long keyctl_set_timeout(key_serial_t, unsigned); 232b5f545c8SDavid Howells extern long keyctl_assume_authority(key_serial_t); 23370a5bb72SDavid Howells extern long keyctl_get_security(key_serial_t keyid, char __user *buffer, 23470a5bb72SDavid Howells size_t buflen); 235ee18d64cSDavid Howells extern long keyctl_session_to_parent(void); 236fdd1b945SDavid Howells extern long keyctl_reject_key(key_serial_t, unsigned, unsigned, key_serial_t); 237ee009e4aSDavid Howells extern long keyctl_instantiate_key_iov(key_serial_t, 238ee009e4aSDavid Howells const struct iovec __user *, 239ee009e4aSDavid Howells unsigned, key_serial_t); 240fd75815fSDavid Howells extern long keyctl_invalidate_key(key_serial_t); 241ee009e4aSDavid Howells 242ee009e4aSDavid Howells extern long keyctl_instantiate_key_common(key_serial_t, 243423b9788SDavid Howells const struct iovec *, 244ee009e4aSDavid Howells unsigned, size_t, key_serial_t); 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds /* 247973c9f4fSDavid Howells * Debugging key validation 2481da177e4SLinus Torvalds */ 2491da177e4SLinus Torvalds #ifdef KEY_DEBUGGING 2501da177e4SLinus Torvalds extern void __key_check(const struct key *); 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds static inline void key_check(const struct key *key) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC)) 2551da177e4SLinus Torvalds __key_check(key); 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds #else 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds #define key_check(key) do {} while(0) 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds #endif 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds #endif /* _INTERNAL_H */ 265