Skip to content

Commit c84ca91

Browse files
committed
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
2 parents c236b6d + a58946c commit c84ca91

36 files changed

Lines changed: 589 additions & 310 deletions

‎Documentation/security/keys/core.rst‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,26 +1102,42 @@ payload contents" for more information.
11021102
See also Documentation/security/keys/request-key.rst.
11031103

11041104

1105+
* To search for a key in a specific domain, call:
1106+
1107+
struct key *request_key_tag(const struct key_type *type,
1108+
const char *description,
1109+
struct key_tag *domain_tag,
1110+
const char *callout_info);
1111+
1112+
This is identical to request_key(), except that a domain tag may be
1113+
specifies that causes search algorithm to only match keys matching that
1114+
tag. The domain_tag may be NULL, specifying a global domain that is
1115+
separate from any nominated domain.
1116+
1117+
11051118
* To search for a key, passing auxiliary data to the upcaller, call::
11061119

11071120
struct key *request_key_with_auxdata(const struct key_type *type,
11081121
const char *description,
1122+
struct key_tag *domain_tag,
11091123
const void *callout_info,
11101124
size_t callout_len,
11111125
void *aux);
11121126
1113-
This is identical to request_key(), except that the auxiliary data is
1114-
passed to the key_type->request_key() op if it exists, and the callout_info
1115-
is a blob of length callout_len, if given (the length may be 0).
1127+
This is identical to request_key_tag(), except that the auxiliary data is
1128+
passed to the key_type->request_key() op if it exists, and the
1129+
callout_info is a blob of length callout_len, if given (the length may be
1130+
0).
11161131

11171132

11181133
* To search for a key under RCU conditions, call::
11191134

11201135
struct key *request_key_rcu(const struct key_type *type,
1121-
const char *description);
1136+
const char *description,
1137+
struct key_tag *domain_tag);
11221138
1123-
which is similar to request_key() except that it does not check for keys
1124-
that are under construction and it will not call out to userspace to
1139+
which is similar to request_key_tag() except that it does not check for
1140+
keys that are under construction and it will not call out to userspace to
11251141
construct a key if it can't find a match.
11261142

11271143

@@ -1162,11 +1178,13 @@ payload contents" for more information.
11621178

11631179
key_ref_t keyring_search(key_ref_t keyring_ref,
11641180
const struct key_type *type,
1165-
const char *description)
1181+
const char *description,
1182+
bool recurse)
11661183
1167-
This searches the keyring tree specified for a matching key. Error ENOKEY
1168-
is returned upon failure (use IS_ERR/PTR_ERR to determine). If successful,
1169-
the returned key will need to be released.
1184+
This searches the specified keyring only (recurse == false) or keyring tree
1185+
(recurse == true) specified for a matching key. Error ENOKEY is returned
1186+
upon failure (use IS_ERR/PTR_ERR to determine). If successful, the returned
1187+
key will need to be released.
11701188

11711189
The possession attribute from the keyring reference is used to control
11721190
access through the permissions mask and is propagated to the returned key

‎Documentation/security/keys/request-key.rst‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,27 @@ The process starts by either the kernel requesting a service by calling
1313
const char *description,
1414
const char *callout_info);
1515

16+
or::
17+
18+
struct key *request_key_tag(const struct key_type *type,
19+
const char *description,
20+
const struct key_tag *domain_tag,
21+
const char *callout_info);
22+
1623
or::
1724

1825
struct key *request_key_with_auxdata(const struct key_type *type,
1926
const char *description,
27+
const struct key_tag *domain_tag,
2028
const char *callout_info,
2129
size_t callout_len,
2230
void *aux);
2331

2432
or::
2533

2634
struct key *request_key_rcu(const struct key_type *type,
27-
const char *description);
35+
const char *description,
36+
const struct key_tag *domain_tag);
2837

2938
Or by userspace invoking the request_key system call::
3039

@@ -38,14 +47,18 @@ does not need to link the key to a keyring to prevent it from being immediately
3847
destroyed. The kernel interface returns a pointer directly to the key, and
3948
it's up to the caller to destroy the key.
4049

41-
The request_key_with_auxdata() calls is like the in-kernel request_key() call,
42-
except that they permit auxiliary data to be passed to the upcaller (the
43-
default is NULL). This is only useful for those key types that define their
44-
own upcall mechanism rather than using /sbin/request-key.
50+
The request_key_tag() call is like the in-kernel request_key(), except that it
51+
also takes a domain tag that allows keys to be separated by namespace and
52+
killed off as a group.
53+
54+
The request_key_with_auxdata() calls is like the request_key_tag() call, except
55+
that they permit auxiliary data to be passed to the upcaller (the default is
56+
NULL). This is only useful for those key types that define their own upcall
57+
mechanism rather than using /sbin/request-key.
4558

46-
The request_key_rcu() call is like the in-kernel request_key() call, except
47-
that it doesn't check for keys that are under construction and doesn't attempt
48-
to construct missing keys.
59+
The request_key_rcu() call is like the request_key_tag() call, except that it
60+
doesn't check for keys that are under construction and doesn't attempt to
61+
construct missing keys.
4962

5063
The userspace interface links the key to a keyring associated with the process
5164
to prevent the key from going away, and returns the serial number of the key to

‎certs/blacklist.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
124124
*p = 0;
125125

126126
kref = keyring_search(make_key_ref(blacklist_keyring, true),
127-
&key_type_blacklist, buffer);
127+
&key_type_blacklist, buffer, false);
128128
if (!IS_ERR(kref)) {
129129
key_ref_put(kref);
130130
ret = -EKEYREJECTED;

‎crypto/asymmetric_keys/asymmetric_type.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct key *find_asymmetric_key(struct key *keyring,
8383
pr_debug("Look up: \"%s\"\n", req);
8484

8585
ref = keyring_search(make_key_ref(keyring, 1),
86-
&key_type_asymmetric, req);
86+
&key_type_asymmetric, req, true);
8787
if (IS_ERR(ref))
8888
pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
8989
kfree(req);

‎fs/afs/addr_list.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ struct afs_vlserver_list *afs_dns_query(struct afs_cell *cell, time64_t *_expiry
246246

247247
_enter("%s", cell->name);
248248

249-
ret = dns_query("afsdb", cell->name, cell->name_len, "srv=1",
250-
&result, _expiry, true);
249+
ret = dns_query(cell->net->net, "afsdb", cell->name, cell->name_len,
250+
"srv=1", &result, _expiry, true);
251251
if (ret < 0) {
252252
_leave(" = %d [dns]", ret);
253253
return ERR_PTR(ret);

‎fs/afs/dynroot.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const struct file_operations afs_dynroot_file_operations = {
2424
static int afs_probe_cell_name(struct dentry *dentry)
2525
{
2626
struct afs_cell *cell;
27+
struct afs_net *net = afs_d2net(dentry);
2728
const char *name = dentry->d_name.name;
2829
size_t len = dentry->d_name.len;
2930
int ret;
@@ -36,13 +37,14 @@ static int afs_probe_cell_name(struct dentry *dentry)
3637
len--;
3738
}
3839

39-
cell = afs_lookup_cell_rcu(afs_d2net(dentry), name, len);
40+
cell = afs_lookup_cell_rcu(net, name, len);
4041
if (!IS_ERR(cell)) {
41-
afs_put_cell(afs_d2net(dentry), cell);
42+
afs_put_cell(net, cell);
4243
return 0;
4344
}
4445

45-
ret = dns_query("afsdb", name, len, "srv=1", NULL, NULL, false);
46+
ret = dns_query(net->net, "afsdb", name, len, "srv=1",
47+
NULL, NULL, false);
4648
if (ret == -ENODATA)
4749
ret = -EDESTADDRREQ;
4850
return ret;

‎fs/cifs/dns_resolve.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
7777
goto name_is_IP_address;
7878

7979
/* Perform the upcall */
80-
rc = dns_query(NULL, hostname, len, NULL, ip_addr, NULL, false);
80+
rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len,
81+
NULL, ip_addr, NULL, false);
8182
if (rc < 0)
8283
cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
8384
__func__, len, len, hostname);

‎fs/nfs/dns_resolve.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ ssize_t nfs_dns_resolve_name(struct net *net, char *name, size_t namelen,
2222
char *ip_addr = NULL;
2323
int ip_len;
2424

25-
ip_len = dns_query(NULL, name, namelen, NULL, &ip_addr, NULL, false);
25+
ip_len = dns_query(net, NULL, name, namelen, NULL, &ip_addr, NULL,
26+
false);
2627
if (ip_len > 0)
2728
ret = rpc_pton(net, ip_addr, ip_len, sa, salen);
2829
else

‎fs/nfs/nfs4idmap.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
291291
if (IS_ERR(rkey)) {
292292
mutex_lock(&idmap->idmap_mutex);
293293
rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
294-
desc, "", 0, idmap);
294+
desc, NULL, "", 0, idmap);
295295
mutex_unlock(&idmap->idmap_mutex);
296296
}
297297
if (!IS_ERR(rkey))

‎include/linux/dns_resolver.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
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

0 commit comments

Comments
 (0)