Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /* Authentication token and access key management
0003  *
0004  * Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  *
0007  * See Documentation/security/keys/core.rst for information on keys/keyrings.
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 /* key handle serial number */
0028 typedef int32_t key_serial_t;
0029 
0030 /* key handle permissions mask */
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  /* possessor can view a key's attributes */
0041 #define KEY_POS_READ    0x02000000  /* possessor can read key payload / view keyring */
0042 #define KEY_POS_WRITE   0x04000000  /* possessor can update key payload / add link to keyring */
0043 #define KEY_POS_SEARCH  0x08000000  /* possessor can find a key in search / search a keyring */
0044 #define KEY_POS_LINK    0x10000000  /* possessor can create a link to a key/keyring */
0045 #define KEY_POS_SETATTR 0x20000000  /* possessor can set key attributes */
0046 #define KEY_POS_ALL 0x3f000000
0047 
0048 #define KEY_USR_VIEW    0x00010000  /* user permissions... */
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  /* group permissions... */
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  /* third party permissions... */
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  * The permissions required on a key that we're looking up.
0076  */
0077 enum key_need_perm {
0078     KEY_NEED_UNSPECIFIED,   /* Needed permission unspecified */
0079     KEY_NEED_VIEW,      /* Require permission to view attributes */
0080     KEY_NEED_READ,      /* Require permission to read content */
0081     KEY_NEED_WRITE,     /* Require permission to update / modify */
0082     KEY_NEED_SEARCH,    /* Require permission to search (keyring) or find (key) */
0083     KEY_NEED_LINK,      /* Require permission to link */
0084     KEY_NEED_SETATTR,   /* Require permission to change attributes */
0085     KEY_NEED_UNLINK,    /* Require permission to unlink key */
0086     KEY_SYSADMIN_OVERRIDE,  /* Special: override by CAP_SYS_ADMIN */
0087     KEY_AUTHTOKEN_OVERRIDE, /* Special: override by possession of auth token */
0088     KEY_DEFER_PERM_CHECK,   /* Special: permission check is deferred */
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;    /* T when subject removed */
0106 };
0107 
0108 struct keyring_index_key {
0109     /* [!] If this structure is altered, the union in struct key must change too! */
0110     unsigned long       hash;           /* Hash value */
0111     union {
0112         struct {
0113 #ifdef __LITTLE_ENDIAN /* Put desc_len at the LSB of x */
0114             u16 desc_len;
0115             char    desc[sizeof(long) - 2]; /* First few chars of description */
0116 #else
0117             char    desc[sizeof(long) - 2]; /* First few chars of description */
0118             u16 desc_len;
0119 #endif
0120         };
0121         unsigned long x;
0122     };
0123     struct key_type     *type;
0124     struct key_tag      *domain_tag;    /* Domain of operation */
0125     const char      *description;
0126 };
0127 
0128 union key_payload {
0129     void __rcu      *rcu_data0;
0130     void            *data[4];
0131 };
0132 
0133 /*****************************************************************************/
0134 /*
0135  * key reference with possession attribute handling
0136  *
0137  * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
0138  * defined. This is because we abuse the bottom bit of the reference to carry a
0139  * flag to indicate whether the calling process possesses that key in one of
0140  * its keyrings.
0141  *
0142  * the key_ref_t has been made a separate type so that the compiler can reject
0143  * attempts to dereference it without proper conversion.
0144  *
0145  * the three functions are used to assemble and disassemble references
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,        /* Positively instantiated */
0179 };
0180 
0181 /*****************************************************************************/
0182 /*
0183  * authentication token / access credential / keyring
0184  * - types of key include:
0185  *   - keyrings
0186  *   - disk encryption IDs
0187  *   - Kerberos TGTs and tickets
0188  */
0189 struct key {
0190     refcount_t      usage;      /* number of references */
0191     key_serial_t        serial;     /* key serial number */
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;  /* Entities watching this key for changes */
0198 #endif
0199     struct rw_semaphore sem;        /* change vs change sem */
0200     struct key_user     *user;      /* owner of this key */
0201     void            *security;  /* security data for this key */
0202     union {
0203         time64_t    expiry;     /* time at which key expires (or 0) */
0204         time64_t    revoked_at; /* time at which key was revoked */
0205     };
0206     time64_t        last_used_at;   /* last time used for LRU keyring discard */
0207     kuid_t          uid;
0208     kgid_t          gid;
0209     key_perm_t      perm;       /* access permissions */
0210     unsigned short      quotalen;   /* length added to quota */
0211     unsigned short      datalen;    /* payload data length
0212                          * - may not match RCU dereferenced payload
0213                          * - payload should contain own length
0214                          */
0215     short           state;      /* Key state (+) or rejection error (-) */
0216 
0217 #ifdef KEY_DEBUGGING
0218     unsigned        magic;
0219 #define KEY_DEBUG_MAGIC     0x18273645u
0220 #endif
0221 
0222     unsigned long       flags;      /* status flags (change with bitops) */
0223 #define KEY_FLAG_DEAD       0   /* set if key type has been deleted */
0224 #define KEY_FLAG_REVOKED    1   /* set if key had been revoked */
0225 #define KEY_FLAG_IN_QUOTA   2   /* set if key consumes quota */
0226 #define KEY_FLAG_USER_CONSTRUCT 3   /* set if key is being constructed in userspace */
0227 #define KEY_FLAG_ROOT_CAN_CLEAR 4   /* set if key can be cleared by root without permission */
0228 #define KEY_FLAG_INVALIDATED    5   /* set if key has been invalidated */
0229 #define KEY_FLAG_BUILTIN    6   /* set if key is built in to the kernel */
0230 #define KEY_FLAG_ROOT_CAN_INVAL 7   /* set if key can be invalidated by root without permission */
0231 #define KEY_FLAG_KEEP       8   /* set if key should not be removed */
0232 #define KEY_FLAG_UID_KEYRING    9   /* set if key is a user or user session keyring */
0233 
0234     /* the key type and key description string
0235      * - the desc is used to match a key against search criteria
0236      * - it should be a printable string
0237      * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
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;      /* type of key */
0245             struct key_tag  *domain_tag;    /* Domain of operation */
0246             char        *description;
0247         };
0248     };
0249 
0250     /* key data
0251      * - this is used to hold the data actually used in cryptography or
0252      *   whatever
0253      */
0254     union {
0255         union key_payload payload;
0256         struct {
0257             /* Keyring bits */
0258             struct list_head name_link;
0259             struct assoc_array keys;
0260         };
0261     };
0262 
0263     /* This is set on a keyring to restrict the addition of a link to a key
0264      * to it.  If this structure isn't provided then it is assumed that the
0265      * keyring is open to any addition.  It is ignored for non-keyring
0266      * keys. Only set this value using keyring_restrict(), keyring_alloc(),
0267      * or key_alloc().
0268      *
0269      * This is intended for use with rings of trusted keys whereby addition
0270      * to the keyring needs to be controlled.  KEY_ALLOC_BYPASS_RESTRICTION
0271      * overrides this, allowing the kernel to add extra keys without
0272      * restriction.
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  /* add to quota, reject if would overrun */
0287 #define KEY_ALLOC_QUOTA_OVERRUN     0x0001  /* add to quota, permit even if overrun */
0288 #define KEY_ALLOC_NOT_IN_QUOTA      0x0002  /* not in quota */
0289 #define KEY_ALLOC_BUILT_IN      0x0004  /* Key is built into kernel */
0290 #define KEY_ALLOC_BYPASS_RESTRICTION    0x0008  /* Override the check on restricted keyrings */
0291 #define KEY_ALLOC_UID_KEYRING       0x0010  /* allocating a user or user session keyring */
0292 #define KEY_ALLOC_SET_KEEP      0x0020  /* Set the KEEP flag on the key/keyring */
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  * request_key - Request a key and wait for construction
0334  * @type: Type of key.
0335  * @description: The searchable description of the key.
0336  * @callout_info: The data to pass to the instantiation upcall (or NULL).
0337  *
0338  * As for request_key_tag(), but with the default global domain tag.
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  * request_key_net - Request a key for a net namespace and wait for construction
0350  * @type: Type of key.
0351  * @description: The searchable description of the key.
0352  * @net: The network namespace that is the key's domain of operation.
0353  * @callout_info: The data to pass to the instantiation upcall (or NULL).
0354  *
0355  * As for request_key() except that it does not add the returned key to a
0356  * keyring if found, new keys are always allocated in the user's quota, the
0357  * callout_info must be a NUL-terminated string and no auxiliary data can be
0358  * passed.  Only keys that operate the specified network namespace are used.
0359  *
0360  * Furthermore, it then works as wait_for_key_construction() to wait for the
0361  * completion of keys undergoing construction with a non-interruptible wait.
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  * request_key_net_rcu - Request a key for a net namespace under RCU conditions
0368  * @type: Type of key.
0369  * @description: The searchable description of the key.
0370  * @net: The network namespace that is the key's domain of operation.
0371  *
0372  * As for request_key_rcu() except that only keys that operate the specified
0373  * network namespace are used.
0374  */
0375 #define request_key_net_rcu(type, description, net) \
0376     request_key_rcu(type, description, net->key_domain)
0377 #endif /* CONFIG_NET */
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     /* Barrier versus mark_key_instantiated(). */
0447     return smp_load_acquire(&key->state);
0448 }
0449 
0450 /**
0451  * key_is_positive - Determine if a key has been positively instantiated
0452  * @key: The key to check.
0453  *
0454  * Return true if the specified key has been positively instantiated, false
0455  * otherwise.
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  * the userspace interface
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 /* CONFIG_KEYS */
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 /* CONFIG_KEYS */
0509 #endif /* __KERNEL__ */
0510 #endif /* _LINUX_KEY_H */