Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /* Authentication token and access key management internal defs
0003  *
0004  * Copyright (C) 2003-5, 2007 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #ifndef _INTERNAL_H
0009 #define _INTERNAL_H
0010 
0011 #include <linux/sched.h>
0012 #include <linux/wait_bit.h>
0013 #include <linux/cred.h>
0014 #include <linux/key-type.h>
0015 #include <linux/task_work.h>
0016 #include <linux/keyctl.h>
0017 #include <linux/refcount.h>
0018 #include <linux/watch_queue.h>
0019 #include <linux/compat.h>
0020 #include <linux/mm.h>
0021 #include <linux/vmalloc.h>
0022 
0023 struct iovec;
0024 
0025 #ifdef __KDEBUG
0026 #define kenter(FMT, ...) \
0027     printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
0028 #define kleave(FMT, ...) \
0029     printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
0030 #define kdebug(FMT, ...) \
0031     printk(KERN_DEBUG "   "FMT"\n", ##__VA_ARGS__)
0032 #else
0033 #define kenter(FMT, ...) \
0034     no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
0035 #define kleave(FMT, ...) \
0036     no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
0037 #define kdebug(FMT, ...) \
0038     no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
0039 #endif
0040 
0041 extern struct key_type key_type_dead;
0042 extern struct key_type key_type_user;
0043 extern struct key_type key_type_logon;
0044 
0045 /*****************************************************************************/
0046 /*
0047  * Keep track of keys for a user.
0048  *
0049  * This needs to be separate to user_struct to avoid a refcount-loop
0050  * (user_struct pins some keyrings which pin this struct).
0051  *
0052  * We also keep track of keys under request from userspace for this UID here.
0053  */
0054 struct key_user {
0055     struct rb_node      node;
0056     struct mutex        cons_lock;  /* construction initiation lock */
0057     spinlock_t      lock;
0058     refcount_t      usage;      /* for accessing qnkeys & qnbytes */
0059     atomic_t        nkeys;      /* number of keys */
0060     atomic_t        nikeys;     /* number of instantiated keys */
0061     kuid_t          uid;
0062     int         qnkeys;     /* number of keys allocated to this user */
0063     int         qnbytes;    /* number of bytes allocated to this user */
0064 };
0065 
0066 extern struct rb_root   key_user_tree;
0067 extern spinlock_t   key_user_lock;
0068 extern struct key_user  root_key_user;
0069 
0070 extern struct key_user *key_user_lookup(kuid_t uid);
0071 extern void key_user_put(struct key_user *user);
0072 
0073 /*
0074  * Key quota limits.
0075  * - root has its own separate limits to everyone else
0076  */
0077 extern unsigned key_quota_root_maxkeys;
0078 extern unsigned key_quota_root_maxbytes;
0079 extern unsigned key_quota_maxkeys;
0080 extern unsigned key_quota_maxbytes;
0081 
0082 #define KEYQUOTA_LINK_BYTES 4       /* a link in a keyring is worth 4 bytes */
0083 
0084 
0085 extern struct kmem_cache *key_jar;
0086 extern struct rb_root key_serial_tree;
0087 extern spinlock_t key_serial_lock;
0088 extern struct mutex key_construction_mutex;
0089 extern wait_queue_head_t request_key_conswq;
0090 
0091 extern void key_set_index_key(struct keyring_index_key *index_key);
0092 extern struct key_type *key_type_lookup(const char *type);
0093 extern void key_type_put(struct key_type *ktype);
0094 
0095 extern int __key_link_lock(struct key *keyring,
0096                const struct keyring_index_key *index_key);
0097 extern int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
0098                const struct keyring_index_key *index_key);
0099 extern int __key_link_begin(struct key *keyring,
0100                 const struct keyring_index_key *index_key,
0101                 struct assoc_array_edit **_edit);
0102 extern int __key_link_check_live_key(struct key *keyring, struct key *key);
0103 extern void __key_link(struct key *keyring, struct key *key,
0104                struct assoc_array_edit **_edit);
0105 extern void __key_link_end(struct key *keyring,
0106                const struct keyring_index_key *index_key,
0107                struct assoc_array_edit *edit);
0108 
0109 extern key_ref_t find_key_to_update(key_ref_t keyring_ref,
0110                     const struct keyring_index_key *index_key);
0111 
0112 extern struct key *keyring_search_instkey(struct key *keyring,
0113                       key_serial_t target_id);
0114 
0115 extern int iterate_over_keyring(const struct key *keyring,
0116                 int (*func)(const struct key *key, void *data),
0117                 void *data);
0118 
0119 struct keyring_search_context {
0120     struct keyring_index_key index_key;
0121     const struct cred   *cred;
0122     struct key_match_data   match_data;
0123     unsigned        flags;
0124 #define KEYRING_SEARCH_NO_STATE_CHECK   0x0001  /* Skip state checks */
0125 #define KEYRING_SEARCH_DO_STATE_CHECK   0x0002  /* Override NO_STATE_CHECK */
0126 #define KEYRING_SEARCH_NO_UPDATE_TIME   0x0004  /* Don't update times */
0127 #define KEYRING_SEARCH_NO_CHECK_PERM    0x0008  /* Don't check permissions */
0128 #define KEYRING_SEARCH_DETECT_TOO_DEEP  0x0010  /* Give an error on excessive depth */
0129 #define KEYRING_SEARCH_SKIP_EXPIRED 0x0020  /* Ignore expired keys (intention to replace) */
0130 #define KEYRING_SEARCH_RECURSE      0x0040  /* Search child keyrings also */
0131 
0132     int (*iterator)(const void *object, void *iterator_data);
0133 
0134     /* Internal stuff */
0135     int         skipped_ret;
0136     bool            possessed;
0137     key_ref_t       result;
0138     time64_t        now;
0139 };
0140 
0141 extern bool key_default_cmp(const struct key *key,
0142                 const struct key_match_data *match_data);
0143 extern key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
0144                     struct keyring_search_context *ctx);
0145 
0146 extern key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx);
0147 extern key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx);
0148 
0149 extern struct key *find_keyring_by_name(const char *name, bool uid_keyring);
0150 
0151 extern int look_up_user_keyrings(struct key **, struct key **);
0152 extern struct key *get_user_session_keyring_rcu(const struct cred *);
0153 extern int install_thread_keyring_to_cred(struct cred *);
0154 extern int install_process_keyring_to_cred(struct cred *);
0155 extern int install_session_keyring_to_cred(struct cred *, struct key *);
0156 
0157 extern struct key *request_key_and_link(struct key_type *type,
0158                     const char *description,
0159                     struct key_tag *domain_tag,
0160                     const void *callout_info,
0161                     size_t callout_len,
0162                     void *aux,
0163                     struct key *dest_keyring,
0164                     unsigned long flags);
0165 
0166 extern bool lookup_user_key_possessed(const struct key *key,
0167                       const struct key_match_data *match_data);
0168 #define KEY_LOOKUP_CREATE   0x01
0169 #define KEY_LOOKUP_PARTIAL  0x02
0170 
0171 extern long join_session_keyring(const char *name);
0172 extern void key_change_session_keyring(struct callback_head *twork);
0173 
0174 extern struct work_struct key_gc_work;
0175 extern unsigned key_gc_delay;
0176 extern void keyring_gc(struct key *keyring, time64_t limit);
0177 extern void keyring_restriction_gc(struct key *keyring,
0178                    struct key_type *dead_type);
0179 extern void key_schedule_gc(time64_t gc_at);
0180 extern void key_schedule_gc_links(void);
0181 extern void key_gc_keytype(struct key_type *ktype);
0182 
0183 extern int key_task_permission(const key_ref_t key_ref,
0184                    const struct cred *cred,
0185                    enum key_need_perm need_perm);
0186 
0187 static inline void notify_key(struct key *key,
0188                   enum key_notification_subtype subtype, u32 aux)
0189 {
0190 #ifdef CONFIG_KEY_NOTIFICATIONS
0191     struct key_notification n = {
0192         .watch.type = WATCH_TYPE_KEY_NOTIFY,
0193         .watch.subtype  = subtype,
0194         .watch.info = watch_sizeof(n),
0195         .key_id     = key_serial(key),
0196         .aux        = aux,
0197     };
0198 
0199     post_watch_notification(key->watchers, &n.watch, current_cred(),
0200                 n.key_id);
0201 #endif
0202 }
0203 
0204 /*
0205  * Check to see whether permission is granted to use a key in the desired way.
0206  */
0207 static inline int key_permission(const key_ref_t key_ref,
0208                  enum key_need_perm need_perm)
0209 {
0210     return key_task_permission(key_ref, current_cred(), need_perm);
0211 }
0212 
0213 extern struct key_type key_type_request_key_auth;
0214 extern struct key *request_key_auth_new(struct key *target,
0215                     const char *op,
0216                     const void *callout_info,
0217                     size_t callout_len,
0218                     struct key *dest_keyring);
0219 
0220 extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
0221 
0222 /*
0223  * Determine whether a key is dead.
0224  */
0225 static inline bool key_is_dead(const struct key *key, time64_t limit)
0226 {
0227     return
0228         key->flags & ((1 << KEY_FLAG_DEAD) |
0229                   (1 << KEY_FLAG_INVALIDATED)) ||
0230         (key->expiry > 0 && key->expiry <= limit) ||
0231         key->domain_tag->removed;
0232 }
0233 
0234 /*
0235  * keyctl() functions
0236  */
0237 extern long keyctl_get_keyring_ID(key_serial_t, int);
0238 extern long keyctl_join_session_keyring(const char __user *);
0239 extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
0240 extern long keyctl_revoke_key(key_serial_t);
0241 extern long keyctl_keyring_clear(key_serial_t);
0242 extern long keyctl_keyring_link(key_serial_t, key_serial_t);
0243 extern long keyctl_keyring_move(key_serial_t, key_serial_t, key_serial_t, unsigned int);
0244 extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
0245 extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
0246 extern long keyctl_keyring_search(key_serial_t, const char __user *,
0247                   const char __user *, key_serial_t);
0248 extern long keyctl_read_key(key_serial_t, char __user *, size_t);
0249 extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
0250 extern long keyctl_setperm_key(key_serial_t, key_perm_t);
0251 extern long keyctl_instantiate_key(key_serial_t, const void __user *,
0252                    size_t, key_serial_t);
0253 extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
0254 extern long keyctl_set_reqkey_keyring(int);
0255 extern long keyctl_set_timeout(key_serial_t, unsigned);
0256 extern long keyctl_assume_authority(key_serial_t);
0257 extern long keyctl_get_security(key_serial_t keyid, char __user *buffer,
0258                 size_t buflen);
0259 extern long keyctl_session_to_parent(void);
0260 extern long keyctl_reject_key(key_serial_t, unsigned, unsigned, key_serial_t);
0261 extern long keyctl_instantiate_key_iov(key_serial_t,
0262                        const struct iovec __user *,
0263                        unsigned, key_serial_t);
0264 extern long keyctl_invalidate_key(key_serial_t);
0265 extern long keyctl_restrict_keyring(key_serial_t id,
0266                     const char __user *_type,
0267                     const char __user *_restriction);
0268 #ifdef CONFIG_PERSISTENT_KEYRINGS
0269 extern long keyctl_get_persistent(uid_t, key_serial_t);
0270 extern unsigned persistent_keyring_expiry;
0271 #else
0272 static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)
0273 {
0274     return -EOPNOTSUPP;
0275 }
0276 #endif
0277 
0278 #ifdef CONFIG_KEY_DH_OPERATIONS
0279 extern long keyctl_dh_compute(struct keyctl_dh_params __user *, char __user *,
0280                   size_t, struct keyctl_kdf_params __user *);
0281 extern long __keyctl_dh_compute(struct keyctl_dh_params __user *, char __user *,
0282                 size_t, struct keyctl_kdf_params *);
0283 #ifdef CONFIG_COMPAT
0284 extern long compat_keyctl_dh_compute(struct keyctl_dh_params __user *params,
0285                 char __user *buffer, size_t buflen,
0286                 struct compat_keyctl_kdf_params __user *kdf);
0287 #endif
0288 #define KEYCTL_KDF_MAX_OUTPUT_LEN   1024    /* max length of KDF output */
0289 #define KEYCTL_KDF_MAX_OI_LEN       64  /* max length of otherinfo */
0290 #else
0291 static inline long keyctl_dh_compute(struct keyctl_dh_params __user *params,
0292                      char __user *buffer, size_t buflen,
0293                      struct keyctl_kdf_params __user *kdf)
0294 {
0295     return -EOPNOTSUPP;
0296 }
0297 
0298 #ifdef CONFIG_COMPAT
0299 static inline long compat_keyctl_dh_compute(
0300                 struct keyctl_dh_params __user *params,
0301                 char __user *buffer, size_t buflen,
0302                 struct keyctl_kdf_params __user *kdf)
0303 {
0304     return -EOPNOTSUPP;
0305 }
0306 #endif
0307 #endif
0308 
0309 #ifdef CONFIG_ASYMMETRIC_KEY_TYPE
0310 extern long keyctl_pkey_query(key_serial_t,
0311                   const char __user *,
0312                   struct keyctl_pkey_query __user *);
0313 
0314 extern long keyctl_pkey_verify(const struct keyctl_pkey_params __user *,
0315                    const char __user *,
0316                    const void __user *, const void __user *);
0317 
0318 extern long keyctl_pkey_e_d_s(int,
0319                   const struct keyctl_pkey_params __user *,
0320                   const char __user *,
0321                   const void __user *, void __user *);
0322 #else
0323 static inline long keyctl_pkey_query(key_serial_t id,
0324                      const char __user *_info,
0325                      struct keyctl_pkey_query __user *_res)
0326 {
0327     return -EOPNOTSUPP;
0328 }
0329 
0330 static inline long keyctl_pkey_verify(const struct keyctl_pkey_params __user *params,
0331                       const char __user *_info,
0332                       const void __user *_in,
0333                       const void __user *_in2)
0334 {
0335     return -EOPNOTSUPP;
0336 }
0337 
0338 static inline long keyctl_pkey_e_d_s(int op,
0339                      const struct keyctl_pkey_params __user *params,
0340                      const char __user *_info,
0341                      const void __user *_in,
0342                      void __user *_out)
0343 {
0344     return -EOPNOTSUPP;
0345 }
0346 #endif
0347 
0348 extern long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen);
0349 
0350 #ifdef CONFIG_KEY_NOTIFICATIONS
0351 extern long keyctl_watch_key(key_serial_t, int, int);
0352 #else
0353 static inline long keyctl_watch_key(key_serial_t key_id, int watch_fd, int watch_id)
0354 {
0355     return -EOPNOTSUPP;
0356 }
0357 #endif
0358 
0359 /*
0360  * Debugging key validation
0361  */
0362 #ifdef KEY_DEBUGGING
0363 extern void __key_check(const struct key *);
0364 
0365 static inline void key_check(const struct key *key)
0366 {
0367     if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
0368         __key_check(key);
0369 }
0370 
0371 #else
0372 
0373 #define key_check(key) do {} while(0)
0374 
0375 #endif
0376 #endif /* _INTERNAL_H */