Skip to content

Commit 9b24261

Browse files
committed
keys: Network namespace domain tag
Create key domain tags for network namespaces and make it possible to automatically tag keys that are used by networked services (e.g. AF_RXRPC, AFS, DNS) with the default network namespace if not set by the caller. This allows keys with the same description but in different namespaces to coexist within a keyring. Signed-off-by: David Howells <[email protected]> cc: [email protected] cc: [email protected] cc: [email protected] cc: [email protected]
1 parent 218e642 commit 9b24261

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
lines changed

include/linux/key-type.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ struct key_type {
7474
*/
7575
size_t def_datalen;
7676

77+
unsigned int flags;
78+
#define KEY_TYPE_NET_DOMAIN 0x00000001 /* Keys of this type have a net namespace domain */
79+
7780
/* vet a description */
7881
int (*vet_description)(const char *description);
7982

include/net/net_namespace.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ struct net {
7171
*/
7272
struct llist_node cleanup_list; /* namespaces on death row */
7373

74+
#ifdef CONFIG_KEYS
75+
struct key_tag *key_domain; /* Key domain of operation tag */
76+
#endif
7477
struct user_namespace *user_ns; /* Owning user namespace */
7578
struct ucounts *ucounts;
7679
spinlock_t nsid_lock;

net/core/net_namespace.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ EXPORT_SYMBOL_GPL(net_namespace_list);
3838
DECLARE_RWSEM(net_rwsem);
3939
EXPORT_SYMBOL_GPL(net_rwsem);
4040

41+
#ifdef CONFIG_KEYS
42+
static struct key_tag init_net_key_domain = { .usage = REFCOUNT_INIT(1) };
43+
#endif
44+
4145
struct net init_net = {
4246
.count = REFCOUNT_INIT(1),
4347
.dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
48+
#ifdef CONFIG_KEYS
49+
.key_domain = &init_net_key_domain,
50+
#endif
4451
};
4552
EXPORT_SYMBOL(init_net);
4653

@@ -386,10 +393,22 @@ static struct net *net_alloc(void)
386393
if (!net)
387394
goto out_free;
388395

396+
#ifdef CONFIG_KEYS
397+
net->key_domain = kzalloc(sizeof(struct key_tag), GFP_KERNEL);
398+
if (!net->key_domain)
399+
goto out_free_2;
400+
refcount_set(&net->key_domain->usage, 1);
401+
#endif
402+
389403
rcu_assign_pointer(net->gen, ng);
390404
out:
391405
return net;
392406

407+
#ifdef CONFIG_KEYS
408+
out_free_2:
409+
kmem_cache_free(net_cachep, net);
410+
net = NULL;
411+
#endif
393412
out_free:
394413
kfree(ng);
395414
goto out;
@@ -566,6 +585,7 @@ static void cleanup_net(struct work_struct *work)
566585
list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
567586
list_del_init(&net->exit_list);
568587
dec_net_namespaces(net->ucounts);
588+
key_remove_domain(net->key_domain);
569589
put_user_ns(net->user_ns);
570590
net_drop_ns(net);
571591
}

net/dns_resolver/dns_key.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ static long dns_resolver_read(const struct key *key,
314314

315315
struct key_type key_type_dns_resolver = {
316316
.name = "dns_resolver",
317+
.flags = KEY_TYPE_NET_DOMAIN,
317318
.preparse = dns_resolver_preparse,
318319
.free_preparse = dns_resolver_free_preparse,
319320
.instantiate = generic_key_instantiate,

net/rxrpc/key.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static long rxrpc_read(const struct key *, char __user *, size_t);
4343
*/
4444
struct key_type key_type_rxrpc = {
4545
.name = "rxrpc",
46+
.flags = KEY_TYPE_NET_DOMAIN,
4647
.preparse = rxrpc_preparse,
4748
.free_preparse = rxrpc_free_preparse,
4849
.instantiate = generic_key_instantiate,
@@ -58,6 +59,7 @@ EXPORT_SYMBOL(key_type_rxrpc);
5859
*/
5960
struct key_type key_type_rxrpc_s = {
6061
.name = "rxrpc_s",
62+
.flags = KEY_TYPE_NET_DOMAIN,
6163
.vet_description = rxrpc_vet_description_s,
6264
.preparse = rxrpc_preparse_s,
6365
.free_preparse = rxrpc_free_preparse_s,

security/keys/keyring.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#include <linux/seq_file.h>
1818
#include <linux/err.h>
1919
#include <linux/user_namespace.h>
20+
#include <linux/nsproxy.h>
2021
#include <keys/keyring-type.h>
2122
#include <keys/user-type.h>
2223
#include <linux/assoc_array_priv.h>
2324
#include <linux/uaccess.h>
25+
#include <net/net_namespace.h>
2426
#include "internal.h"
2527

2628
/*
@@ -220,7 +222,10 @@ void key_set_index_key(struct keyring_index_key *index_key)
220222

221223
memcpy(index_key->desc, index_key->description, n);
222224

223-
index_key->domain_tag = &default_domain_tag;
225+
if (index_key->type->flags & KEY_TYPE_NET_DOMAIN)
226+
index_key->domain_tag = current->nsproxy->net_ns->key_domain;
227+
else
228+
index_key->domain_tag = &default_domain_tag;
224229
hash_key_type_and_desc(index_key);
225230
}
226231

0 commit comments

Comments
 (0)