History

Merge tag 'keys-namespace-20190627' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs

Pull keyring namespacing from David Howells: "These patches help make keys and keyrings more namespace aware. Firstly some miscellaneous patches to make the process easier: - Simplify key index_key handling so that the word-sized chunks assoc_array requires don't have to be shifted about, making it easier to add more bits into the key. - Cache the hash value in the key so that we don't have to calculate on every key we examine during a search (it involves a bunch of multiplications). - Allow keying_search() to search non-recursively. Then the main patches: - Make it so that keyring names are per-user_namespace from the point of view of KEYCTL_JOIN_SESSION_KEYRING so that they're not accessible cross-user_namespace. keyctl_capabilities() shows KEYCTL_CAPS1_NS_KEYRING_NAME for this. - Move the user and user-session keyrings to the user_namespace rather than the user_struct. This prevents them propagating directly across user_namespaces boundaries (ie. the KEY_SPEC_* flags will only pick from the current user_namespace). - Make it possible to include the target namespace in which the key shall operate in the index_key. This will allow the possibility of multiple keys with the same description, but different target domains to be held in the same keyring. keyctl_capabilities() shows KEYCTL_CAPS1_NS_KEY_TAG for this. - Make it so that keys are implicitly invalidated by removal of a domain tag, causing them to be garbage collected. - Institute a network namespace domain tag that allows keys to be differentiated by the network namespace in which they operate. New keys that are of a type marked 'KEY_TYPE_NET_DOMAIN' are assigned the network domain in force when they are created. - Make it so that the desired network namespace can be handed down into the request_key() mechanism. This allows AFS, NFS, etc. to request keys specific to the network namespace of the superblock. This also means that the keys in the DNS record cache are thenceforth namespaced, provided network filesystems pass the appropriate network namespace down into dns_query(). For DNS, AFS and NFS are good, whilst CIFS and Ceph are not. Other cache keyrings, such as idmapper keyrings, also need to set the domain tag - for which they need access to the network namespace of the superblock" * tag 'keys-namespace-20190627' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs: keys: Pass the network namespace into request_key mechanism keys: Network namespace domain tag keys: Garbage collect keys for which the domain has been removed keys: Include target namespace in match criteria keys: Move the user and user-session keyrings to the user_namespace keys: Namespace keyring names keys: Add a 'recurse' flag for keyring searches keys: Cache the hash value to avoid lots of recalculation keys: Simplify key description management

Linus Torvalds committed 7 years ago

Commit c84ca912 parents c236b6d a58946c36 files · +589 −310Browse files

Showing the changes to include/linux (5 of 36 files). Show all

Unified · Split

5 files
  1. include/linux/dns_resolver.h +2 −1
  2. include/linux/key-type.h +3 −0
  3. include/linux/key.h +75 −6
  4. include/linux/sched/user.h +0 −14
  5. include/linux/user_namespace.h +11 −1
include/linux/dns_resolver.h+2−1
@@ -26,7 +26,8 @@ · 25 unmodified lines
2626
2727 #include <uapi/linux/dns_resolver.h>
2828
29−extern int dns_query(const char *type, const char *name, size_t namelen,
29+struct net;
30+extern int dns_query(struct net *net, const char *type, const char *name, size_t namelen,
3031 const char *options, char **_result, time64_t *_expiry,
3132 bool invalidate);
3233
include/linux/key-type.h+3−0
@@ -70,6 +70,9 @@ struct key_type { · 69 unmodified lines
7070 */
7171 size_t def_datalen;
7272
73+ unsigned int flags;
74+#define KEY_TYPE_NET_DOMAIN 0x00000001 /* Keys of this type have a net namespace domain */
75+
7376 /* vet a description */
7477 int (*vet_description)(const char *description);
7578
include/linux/key.h+75−6
@@ -31,6 +31,7 @@ typedef int32_t key_serial_t; · 30 unmodified lines
3131 typedef uint32_t key_perm_t;
3232
3333 struct key;
34+struct net;
3435
3536 #ifdef CONFIG_KEYS
3637
@@ -77,13 +78,34 @@ struct cred; · 40 unmodified lines
7778
7879 struct key_type;
7980 struct key_owner;
81+struct key_tag;
8082 struct keyring_list;
8183 struct keyring_name;
8284
85+struct key_tag {
86+ struct rcu_head rcu;
87+ refcount_t usage;
88+ bool removed; /* T when subject removed */
89+};
90+
8391 struct keyring_index_key {
92+ /* [!] If this structure is altered, the union in struct key must change too! */
93+ unsigned long hash; /* Hash value */
94+ union {
95+ struct {
96+#ifdef __LITTLE_ENDIAN /* Put desc_len at the LSB of x */
97+ u8 desc_len;
98+ char desc[sizeof(long) - 1]; /* First few chars of description */
99+#else
100+ char desc[sizeof(long) - 1]; /* First few chars of description */
101+ u8 desc_len;
102+#endif
103+ };
104+ unsigned long x;
105+ };
84106 struct key_type *type;
107+ struct key_tag *domain_tag; /* Domain of operation */
85108 const char *description;
86− size_t desc_len;
87109 };
88110
89111 union key_payload {
@@ -197,7 +219,10 @@ struct key { · 107 unmodified lines
197219 union {
198220 struct keyring_index_key index_key;
199221 struct {
222+ unsigned long hash;
223+ unsigned long len_desc;
200224 struct key_type *type; /* type of key */
225+ struct key_tag *domain_tag; /* Domain of operation */
201226 char *description;
202227 };
203228 };
@@ -248,6 +273,8 @@ extern struct key *key_alloc(struct key_type *type, · 44 unmodified lines
248273 extern void key_revoke(struct key *key);
249274 extern void key_invalidate(struct key *key);
250275 extern void key_put(struct key *key);
276+extern bool key_put_tag(struct key_tag *tag);
277+extern void key_remove_domain(struct key_tag *domain_tag);
251278
252279 static inline struct key *__key_get(struct key *key)
253280 {
@@ -265,19 +292,57 @@ static inline void key_ref_put(key_ref_t key_ref) · 11 unmodified lines
265292 key_put(key_ref_to_ptr(key_ref));
266293 }
267294
268−extern struct key *request_key(struct key_type *type,
269− const char *description,
270− const char *callout_info);
295+extern struct key *request_key_tag(struct key_type *type,
296+ const char *description,
297+ struct key_tag *domain_tag,
298+ const char *callout_info);
271299
272300 extern struct key *request_key_rcu(struct key_type *type,
273− const char *description);
301+ const char *description,
302+ struct key_tag *domain_tag);
274303
275304 extern struct key *request_key_with_auxdata(struct key_type *type,
276305 const char *description,
306+ struct key_tag *domain_tag,
277307 const void *callout_info,
278308 size_t callout_len,
279309 void *aux);
280310
311+/**
312+ * request_key - Request a key and wait for construction
313+ * @type: Type of key.
314+ * @description: The searchable description of the key.
315+ * @callout_info: The data to pass to the instantiation upcall (or NULL).
316+ *
317+ * As for request_key_tag(), but with the default global domain tag.
318+ */
319+static inline struct key *request_key(struct key_type *type,
320+ const char *description,
321+ const char *callout_info)
322+{
323+ return request_key_tag(type, description, NULL, callout_info);
324+}
325+
326+#ifdef CONFIG_NET
327+/*
328+ * request_key_net - Request a key for a net namespace and wait for construction
329+ * @type: Type of key.
330+ * @description: The searchable description of the key.
331+ * @net: The network namespace that is the key's domain of operation.
332+ * @callout_info: The data to pass to the instantiation upcall (or NULL).
333+ *
334+ * As for request_key() except that it does not add the returned key to a
335+ * keyring if found, new keys are always allocated in the user's quota, the
336+ * callout_info must be a NUL-terminated string and no auxiliary data can be
337+ * passed. Only keys that operate the specified network namespace are used.
338+ *
339+ * Furthermore, it then works as wait_for_key_construction() to wait for the
340+ * completion of keys undergoing construction with a non-interruptible wait.
341+ */
342+#define request_key_net(type, description, net, callout_info) \
343+ request_key_tag(type, description, net->key_domain, callout_info);
344+#endif /* CONFIG_NET */
345+
281346 extern int wait_for_key_construction(struct key *key, bool intr);
282347
283348 extern int key_validate(const struct key *key);
@@ -321,7 +386,8 @@ extern int keyring_clear(struct key *keyring); · 37 unmodified lines
321386
322387 extern key_ref_t keyring_search(key_ref_t keyring,
323388 struct key_type *type,
324− const char *description);
389+ const char *description,
390+ bool recurse);
325391
326392 extern int keyring_add_key(struct key *keyring,
327393 struct key *key);
@@ -340,6 +406,7 @@ extern void key_set_timeout(struct key *, unsigned); · 12 unmodified lines
340406
341407 extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
342408 key_perm_t perm);
409+extern void key_free_user_ns(struct user_namespace *);
343410
344411 /*
345412 * The permissions required on a key that we're looking up.
@@ -413,6 +480,8 @@ extern void key_init(void); · 67 unmodified lines
413480 #define key_fsuid_changed(c) do { } while(0)
414481 #define key_fsgid_changed(c) do { } while(0)
415482 #define key_init() do { } while(0)
483+#define key_free_user_ns(ns) do { } while(0)
484+#define key_remove_domain(d) do { } while(0)
416485
417486 #endif /* CONFIG_KEYS */
418487 #endif /* __KERNEL__ */
include/linux/sched/user.h+0−14
@@ -7,8 +7,6 @@ · 6 unmodified lines
77 #include <linux/refcount.h>
88 #include <linux/ratelimit.h>
99
10−struct key;
11−
1210 /*
1311 * Some day this will be a full-fledged user tracking system..
1412 */
@@ -30,18 +28,6 @@ struct user_struct { · 15 unmodified lines
3028 unsigned long unix_inflight; /* How many files in flight in unix sockets */
3129 atomic_long_t pipe_bufs; /* how many pages are allocated in pipe buffers */
3230
33−#ifdef CONFIG_KEYS
34− /*
35− * These pointers can only change from NULL to a non-NULL value once.
36− * Writes are protected by key_user_keyring_mutex.
37− * Unlocked readers should use READ_ONCE() unless they know that
38− * install_user_keyrings() has been called successfully (which sets
39− * these members to non-NULL values, preventing further modifications).
40− */
41− struct key *uid_keyring; /* UID specific keyring */
42− struct key *session_keyring; /* UID's default session keyring */
43−#endif
44−
4531 /* Hash table maintenance information */
4632 struct hlist_node uidhash_node;
4733 kuid_t uid;
include/linux/user_namespace.h+11−1
@@ -64,10 +64,20 @@ struct user_namespace { · 63 unmodified lines
6464 struct ns_common ns;
6565 unsigned long flags;
6666
67+#ifdef CONFIG_KEYS
68+ /* List of joinable keyrings in this namespace. Modification access of
69+ * these pointers is controlled by keyring_sem. Once
70+ * user_keyring_register is set, it won't be changed, so it can be
71+ * accessed directly with READ_ONCE().
72+ */
73+ struct list_head keyring_name_list;
74+ struct key *user_keyring_register;
75+ struct rw_semaphore keyring_sem;
76+#endif
77+
6778 /* Register of per-UID persistent keyrings for this namespace */
6879 #ifdef CONFIG_PERSISTENT_KEYRINGS
6980 struct key *persistent_keyring_register;
70− struct rw_semaphore persistent_keyring_register_sem;
7181 #endif
7282 struct work_struct work;
7383 #ifdef CONFIG_SYSCTL

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t