blob: bf34ddaa2ad741e1c4d7cc7836f3851483a973bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/idmap.c
3 *
4 * UID and GID to name mapping for clients.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Marius Aamodt Eriksen <marius@umich.edu>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
Trond Myklebust5cf36cf2011-02-22 15:44:31 -080036#include <linux/types.h>
Bryan Schumaker57e62322012-02-24 14:14:51 -050037#include <linux/parser.h>
38#include <linux/fs.h>
Bryan Schumaker57e62322012-02-24 14:14:51 -050039#include <net/net_namespace.h>
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050040#include <linux/sunrpc/rpc_pipe_fs.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050041#include <linux/nfs_fs.h>
Bryan Schumaker57e62322012-02-24 14:14:51 -050042#include <linux/nfs_fs_sb.h>
43#include <linux/key.h>
44#include <linux/keyctl.h>
45#include <linux/key-type.h>
46#include <keys/user-type.h>
David Howells822ad642019-02-14 16:20:25 +000047#include <keys/request_key_auth-type.h>
Bryan Schumaker57e62322012-02-24 14:14:51 -050048#include <linux/module.h>
49
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050050#include "internal.h"
Stanislav Kinsbursky17347d02012-01-26 15:11:41 +040051#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040052#include "nfs4idmap.h"
Trond Myklebust1f2d30b2013-08-13 11:34:01 -040053#include "nfs4trace.h"
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050054
55#define NFS_UINT_MAXLEN 11
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050056
Trond Myklebust17280172012-03-11 13:11:00 -040057static const struct cred *id_resolver_cache;
58static struct key_type key_type_id_resolver_legacy;
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050059
Bryan Schumakerc5066942012-08-09 14:05:49 -040060struct idmap_legacy_upcalldata {
61 struct rpc_pipe_msg pipe_msg;
62 struct idmap_msg idmap_msg;
David Howells822ad642019-02-14 16:20:25 +000063 struct key *authkey;
Bryan Schumakerc5066942012-08-09 14:05:49 -040064 struct idmap *idmap;
65};
66
Trond Myklebust0cac12022012-09-27 16:15:00 -040067struct idmap {
Trond Myklebust2127d822013-08-26 17:16:17 -040068 struct rpc_pipe_dir_object idmap_pdo;
Trond Myklebust0cac12022012-09-27 16:15:00 -040069 struct rpc_pipe *idmap_pipe;
70 struct idmap_legacy_upcalldata *idmap_upcall_data;
71 struct mutex idmap_mutex;
72};
73
Trond Myklebust6926afd2012-01-07 13:22:46 -050074/**
75 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
76 * @fattr: fully initialised struct nfs_fattr
77 * @owner_name: owner name string cache
78 * @group_name: group name string cache
79 */
80void nfs_fattr_init_names(struct nfs_fattr *fattr,
81 struct nfs4_string *owner_name,
82 struct nfs4_string *group_name)
83{
84 fattr->owner_name = owner_name;
85 fattr->group_name = group_name;
86}
87
88static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
89{
90 fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
91 kfree(fattr->owner_name->data);
92}
93
94static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
95{
96 fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
97 kfree(fattr->group_name->data);
98}
99
100static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
101{
102 struct nfs4_string *owner = fattr->owner_name;
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800103 kuid_t uid;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500104
105 if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
106 return false;
107 if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
108 fattr->uid = uid;
109 fattr->valid |= NFS_ATTR_FATTR_OWNER;
110 }
111 return true;
112}
113
114static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
115{
116 struct nfs4_string *group = fattr->group_name;
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800117 kgid_t gid;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500118
119 if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
120 return false;
121 if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
122 fattr->gid = gid;
123 fattr->valid |= NFS_ATTR_FATTR_GROUP;
124 }
125 return true;
126}
127
128/**
129 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
130 * @fattr: a fully initialised nfs_fattr structure
131 */
132void nfs_fattr_free_names(struct nfs_fattr *fattr)
133{
134 if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
135 nfs_fattr_free_owner_name(fattr);
136 if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
137 nfs_fattr_free_group_name(fattr);
138}
139
140/**
141 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
142 * @server: pointer to the filesystem nfs_server structure
143 * @fattr: a fully initialised nfs_fattr structure
144 *
145 * This helper maps the cached NFSv4 owner/group strings in fattr into
146 * their numeric uid/gid equivalents, and then frees the cached strings.
147 */
148void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
149{
150 if (nfs_fattr_map_owner_name(server, fattr))
151 nfs_fattr_free_owner_name(fattr);
152 if (nfs_fattr_map_group_name(server, fattr))
153 nfs_fattr_free_group_name(fattr);
154}
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800155
Tom Haynesd67ae822014-12-11 17:02:04 -0500156int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800157{
158 unsigned long val;
159 char buf[16];
160
161 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
162 return 0;
163 memcpy(buf, name, namelen);
164 buf[namelen] = '\0';
Daniel Walter7297cb62012-09-26 21:51:46 +0200165 if (kstrtoul(buf, 0, &val) != 0)
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800166 return 0;
167 *res = val;
168 return 1;
169}
Tom Haynesd67ae822014-12-11 17:02:04 -0500170EXPORT_SYMBOL_GPL(nfs_map_string_to_numeric);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Trond Myklebustf0b85162011-02-22 15:44:31 -0800172static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
173{
174 return snprintf(buf, buflen, "%u", id);
175}
176
Trond Myklebust17280172012-03-11 13:11:00 -0400177static struct key_type key_type_id_resolver = {
Bryan Schumaker955a8572010-09-29 15:41:49 -0400178 .name = "id_resolver",
David Howellsf9167782014-07-18 18:56:35 +0100179 .preparse = user_preparse,
180 .free_preparse = user_free_preparse,
181 .instantiate = generic_key_instantiate,
Bryan Schumaker955a8572010-09-29 15:41:49 -0400182 .revoke = user_revoke,
183 .destroy = user_destroy,
184 .describe = user_describe,
185 .read = user_read,
186};
187
Anna Schumakerfb2a5252015-07-13 14:01:29 -0400188int nfs_idmap_init(void)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400189{
190 struct cred *cred;
191 struct key *keyring;
192 int ret = 0;
193
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500194 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
195 key_type_id_resolver.name);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400196
197 cred = prepare_kernel_cred(NULL);
198 if (!cred)
199 return -ENOMEM;
200
Eric W. Biederman4e963d42013-02-01 03:03:16 -0800201 keyring = keyring_alloc(".id_resolver",
202 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
David Howellsf8aa23a2012-10-02 19:24:56 +0100203 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
204 KEY_USR_VIEW | KEY_USR_READ,
David Howells5ac7eac2016-04-06 16:14:24 +0100205 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400206 if (IS_ERR(keyring)) {
207 ret = PTR_ERR(keyring);
208 goto failed_put_cred;
209 }
210
Bryan Schumaker955a8572010-09-29 15:41:49 -0400211 ret = register_key_type(&key_type_id_resolver);
212 if (ret < 0)
213 goto failed_put_key;
214
David Howellsa427b9e2012-07-25 16:53:36 +0100215 ret = register_key_type(&key_type_id_resolver_legacy);
216 if (ret < 0)
217 goto failed_reg_legacy;
218
David Howells700920e2012-01-18 15:31:45 +0000219 set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400220 cred->thread_keyring = keyring;
221 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
222 id_resolver_cache = cred;
223 return 0;
224
David Howellsa427b9e2012-07-25 16:53:36 +0100225failed_reg_legacy:
226 unregister_key_type(&key_type_id_resolver);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400227failed_put_key:
228 key_put(keyring);
229failed_put_cred:
230 put_cred(cred);
231 return ret;
232}
233
Anna Schumakerfb2a5252015-07-13 14:01:29 -0400234void nfs_idmap_quit(void)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400235{
236 key_revoke(id_resolver_cache->thread_keyring);
237 unregister_key_type(&key_type_id_resolver);
David Howellsa427b9e2012-07-25 16:53:36 +0100238 unregister_key_type(&key_type_id_resolver_legacy);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400239 put_cred(id_resolver_cache);
240}
241
242/*
243 * Assemble the description to pass to request_key()
244 * This function will allocate a new string and update dest to point
245 * at it. The caller is responsible for freeing dest.
246 *
247 * On error 0 is returned. Otherwise, the length of dest is returned.
248 */
249static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
250 const char *type, size_t typelen, char **desc)
251{
252 char *cp;
253 size_t desclen = typelen + namelen + 2;
254
255 *desc = kmalloc(desclen, GFP_KERNEL);
Dan Carpenter8f0d97b2010-10-28 08:05:57 +0200256 if (!*desc)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400257 return -ENOMEM;
258
259 cp = *desc;
260 memcpy(cp, type, typelen);
261 cp += typelen;
262 *cp++ = ':';
263
264 memcpy(cp, name, namelen);
265 cp += namelen;
266 *cp = '\0';
267 return desclen;
268}
269
Bryan Schumakerffa57b92013-06-26 14:09:46 -0400270static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
271 const char *type, struct idmap *idmap)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400272{
Bryan Schumaker955a8572010-09-29 15:41:49 -0400273 char *desc;
Bryan Schumakerffa57b92013-06-26 14:09:46 -0400274 struct key *rkey;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400275 ssize_t ret;
276
277 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
Dan Carpentercdb2e532017-09-21 00:53:46 +0300278 if (ret < 0)
Bryan Schumakerffa57b92013-06-26 14:09:46 -0400279 return ERR_PTR(ret);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400280
Bryan Schumakerffa57b92013-06-26 14:09:46 -0400281 rkey = request_key(&key_type_id_resolver, desc, "");
282 if (IS_ERR(rkey)) {
283 mutex_lock(&idmap->idmap_mutex);
284 rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
285 desc, "", 0, idmap);
286 mutex_unlock(&idmap->idmap_mutex);
287 }
David Howells0c7774a2014-07-17 20:45:08 +0100288 if (!IS_ERR(rkey))
289 set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500290
Bryan Schumaker955a8572010-09-29 15:41:49 -0400291 kfree(desc);
Bryan Schumakerffa57b92013-06-26 14:09:46 -0400292 return rkey;
293}
294
295static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
296 const char *type, void *data,
297 size_t data_size, struct idmap *idmap)
298{
299 const struct cred *saved_cred;
300 struct key *rkey;
David Howells146aa8b2015-10-21 14:04:48 +0100301 const struct user_key_payload *payload;
Bryan Schumakerffa57b92013-06-26 14:09:46 -0400302 ssize_t ret;
303
304 saved_cred = override_creds(id_resolver_cache);
305 rkey = nfs_idmap_request_key(name, namelen, type, idmap);
306 revert_creds(saved_cred);
307
Bryan Schumaker955a8572010-09-29 15:41:49 -0400308 if (IS_ERR(rkey)) {
309 ret = PTR_ERR(rkey);
310 goto out;
311 }
312
313 rcu_read_lock();
314 rkey->perm |= KEY_USR_VIEW;
315
316 ret = key_validate(rkey);
317 if (ret < 0)
318 goto out_up;
319
David Howells0837e492017-03-01 15:11:23 +0000320 payload = user_key_payload_rcu(rkey);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400321 if (IS_ERR_OR_NULL(payload)) {
322 ret = PTR_ERR(payload);
323 goto out_up;
324 }
325
326 ret = payload->datalen;
327 if (ret > 0 && ret <= data_size)
328 memcpy(data, payload->data, ret);
329 else
330 ret = -EINVAL;
331
332out_up:
333 rcu_read_unlock();
334 key_put(rkey);
335out:
336 return ret;
337}
338
Bryan Schumaker955a8572010-09-29 15:41:49 -0400339/* ID -> Name */
Bryan Schumaker57e62322012-02-24 14:14:51 -0500340static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
341 size_t buflen, struct idmap *idmap)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400342{
343 char id_str[NFS_UINT_MAXLEN];
344 int id_len;
345 ssize_t ret;
346
Dave Wysochanskid6889482018-05-29 17:47:30 -0400347 id_len = nfs_map_numeric_to_string(id, id_str, sizeof(id_str));
Bryan Schumaker57e62322012-02-24 14:14:51 -0500348 ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400349 if (ret < 0)
350 return -EINVAL;
351 return ret;
352}
353
354/* Name -> ID */
Bryan Schumaker57e62322012-02-24 14:14:51 -0500355static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
356 __u32 *id, struct idmap *idmap)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400357{
358 char id_str[NFS_UINT_MAXLEN];
359 long id_long;
360 ssize_t data_size;
361 int ret = 0;
362
Bryan Schumaker57e62322012-02-24 14:14:51 -0500363 data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400364 if (data_size <= 0) {
365 ret = -EINVAL;
366 } else {
Daniel Walter7297cb62012-09-26 21:51:46 +0200367 ret = kstrtol(id_str, 10, &id_long);
Dan Carpenter4cd1ec92017-06-23 18:16:25 +0300368 if (!ret)
369 *id = (__u32)id_long;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400370 }
371 return ret;
372}
373
Bryan Schumakere6499c62012-01-26 16:54:23 -0500374/* idmap classic begins here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Bryan Schumaker57e62322012-02-24 14:14:51 -0500376enum {
377 Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
378};
379
380static const match_table_t nfs_idmap_tokens = {
381 { Opt_find_uid, "uid:%s" },
382 { Opt_find_gid, "gid:%s" },
383 { Opt_find_user, "user:%s" },
384 { Opt_find_group, "group:%s" },
385 { Opt_find_err, NULL }
386};
387
David Howells822ad642019-02-14 16:20:25 +0000388static int nfs_idmap_legacy_upcall(struct key *, void *);
Chuck Lever369af0f2007-12-20 14:54:35 -0500389static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
390 size_t);
Bryan Schumakerc5066942012-08-09 14:05:49 -0400391static void idmap_release_pipe(struct inode *);
Chuck Lever369af0f2007-12-20 14:54:35 -0500392static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Trond Myklebustb693ba42009-08-09 15:14:15 -0400394static const struct rpc_pipe_ops idmap_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400395 .upcall = rpc_pipe_generic_upcall,
Chuck Lever369af0f2007-12-20 14:54:35 -0500396 .downcall = idmap_pipe_downcall,
Bryan Schumakerc5066942012-08-09 14:05:49 -0400397 .release_pipe = idmap_release_pipe,
Chuck Lever369af0f2007-12-20 14:54:35 -0500398 .destroy_msg = idmap_pipe_destroy_msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
400
Trond Myklebust17280172012-03-11 13:11:00 -0400401static struct key_type key_type_id_resolver_legacy = {
David Howellsa427b9e2012-07-25 16:53:36 +0100402 .name = "id_legacy",
David Howellsf9167782014-07-18 18:56:35 +0100403 .preparse = user_preparse,
404 .free_preparse = user_free_preparse,
405 .instantiate = generic_key_instantiate,
Bryan Schumaker57e62322012-02-24 14:14:51 -0500406 .revoke = user_revoke,
407 .destroy = user_destroy,
408 .describe = user_describe,
409 .read = user_read,
410 .request_key = nfs_idmap_legacy_upcall,
411};
412
Trond Myklebust2127d822013-08-26 17:16:17 -0400413static void nfs_idmap_pipe_destroy(struct dentry *dir,
414 struct rpc_pipe_dir_object *pdo)
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400415{
Trond Myklebust2127d822013-08-26 17:16:17 -0400416 struct idmap *idmap = pdo->pdo_data;
417 struct rpc_pipe *pipe = idmap->idmap_pipe;
418
Trond Myklebustd7631252013-08-26 17:26:51 -0400419 if (pipe->dentry) {
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400420 rpc_unlink(pipe->dentry);
Trond Myklebustd7631252013-08-26 17:26:51 -0400421 pipe->dentry = NULL;
422 }
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400423}
424
Trond Myklebust2127d822013-08-26 17:16:17 -0400425static int nfs_idmap_pipe_create(struct dentry *dir,
426 struct rpc_pipe_dir_object *pdo)
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400427{
Trond Myklebust2127d822013-08-26 17:16:17 -0400428 struct idmap *idmap = pdo->pdo_data;
429 struct rpc_pipe *pipe = idmap->idmap_pipe;
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400430 struct dentry *dentry;
431
432 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
433 if (IS_ERR(dentry))
434 return PTR_ERR(dentry);
435 pipe->dentry = dentry;
436 return 0;
437}
438
Trond Myklebust2127d822013-08-26 17:16:17 -0400439static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
440 .create = nfs_idmap_pipe_create,
441 .destroy = nfs_idmap_pipe_destroy,
442};
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400443
David Howellsb7162792006-08-22 20:06:09 -0400444int
David Howellsadfa6f92006-08-22 20:06:08 -0400445nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct idmap *idmap;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300448 struct rpc_pipe *pipe;
David Howellsb7162792006-08-22 20:06:09 -0400449 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Chuck Lever369af0f2007-12-20 14:54:35 -0500451 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
452 if (idmap == NULL)
453 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Trond Myklebust2127d822013-08-26 17:16:17 -0400455 rpc_init_pipe_dir_object(&idmap->idmap_pdo,
456 &nfs_idmap_pipe_dir_object_ops,
457 idmap);
458
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300459 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
460 if (IS_ERR(pipe)) {
461 error = PTR_ERR(pipe);
Trond Myklebust2127d822013-08-26 17:16:17 -0400462 goto err;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300463 }
464 idmap->idmap_pipe = pipe;
Bryan Schumakerb1027432012-06-20 14:35:28 -0400465 mutex_init(&idmap->idmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Trond Myklebust2127d822013-08-26 17:16:17 -0400467 error = rpc_add_pipe_dir_object(clp->cl_net,
468 &clp->cl_rpcclient->cl_pipedir_objects,
469 &idmap->idmap_pdo);
470 if (error)
471 goto err_destroy_pipe;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400474 return 0;
Trond Myklebust2127d822013-08-26 17:16:17 -0400475err_destroy_pipe:
476 rpc_destroy_pipe_data(idmap->idmap_pipe);
477err:
478 kfree(idmap);
479 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482void
David Howellsadfa6f92006-08-22 20:06:08 -0400483nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct idmap *idmap = clp->cl_idmap;
486
487 if (!idmap)
488 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 clp->cl_idmap = NULL;
Trond Myklebust2127d822013-08-26 17:16:17 -0400490 rpc_remove_pipe_dir_object(clp->cl_net,
491 &clp->cl_rpcclient->cl_pipedir_objects,
492 &idmap->idmap_pdo);
493 rpc_destroy_pipe_data(idmap->idmap_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 kfree(idmap);
495}
496
Bryan Schumakerc5066942012-08-09 14:05:49 -0400497static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
498 struct idmap_msg *im,
Bryan Schumaker57e62322012-02-24 14:14:51 -0500499 struct rpc_pipe_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Bryan Schumaker57e62322012-02-24 14:14:51 -0500501 substring_t substr;
502 int token, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Bryan Schumaker57e62322012-02-24 14:14:51 -0500504 im->im_type = IDMAP_TYPE_GROUP;
505 token = match_token(desc, nfs_idmap_tokens, &substr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Bryan Schumaker57e62322012-02-24 14:14:51 -0500507 switch (token) {
508 case Opt_find_uid:
509 im->im_type = IDMAP_TYPE_USER;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500510 /* Fall through */
Bryan Schumaker57e62322012-02-24 14:14:51 -0500511 case Opt_find_gid:
512 im->im_conv = IDMAP_CONV_NAMETOID;
513 ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Bryan Schumaker57e62322012-02-24 14:14:51 -0500516 case Opt_find_user:
517 im->im_type = IDMAP_TYPE_USER;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500518 /* Fall through */
Bryan Schumaker57e62322012-02-24 14:14:51 -0500519 case Opt_find_group:
520 im->im_conv = IDMAP_CONV_IDTONAME;
521 ret = match_int(&substr, &im->im_id);
Chengguang Xu12b289c2018-06-28 11:09:45 +0800522 if (ret)
523 goto out;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500524 break;
Trond Myklebust685f50f2012-02-08 13:39:15 -0500525
Bryan Schumaker57e62322012-02-24 14:14:51 -0500526 default:
527 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto out;
529 }
530
Bryan Schumaker57e62322012-02-24 14:14:51 -0500531 msg->data = im;
532 msg->len = sizeof(struct idmap_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Bryan Schumaker57e62322012-02-24 14:14:51 -0500534out:
Chuck Lever369af0f2007-12-20 14:54:35 -0500535 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400538static bool
539nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
Trond Myklebust0cac12022012-09-27 16:15:00 -0400540 struct idmap_legacy_upcalldata *data)
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400541{
Trond Myklebust0cac12022012-09-27 16:15:00 -0400542 if (idmap->idmap_upcall_data != NULL) {
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400543 WARN_ON_ONCE(1);
544 return false;
545 }
Trond Myklebust0cac12022012-09-27 16:15:00 -0400546 idmap->idmap_upcall_data = data;
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400547 return true;
548}
549
550static void
551nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
552{
David Howells822ad642019-02-14 16:20:25 +0000553 struct key *authkey = idmap->idmap_upcall_data->authkey;
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400554
Trond Myklebust0cac12022012-09-27 16:15:00 -0400555 kfree(idmap->idmap_upcall_data);
556 idmap->idmap_upcall_data = NULL;
David Howells822ad642019-02-14 16:20:25 +0000557 complete_request_key(authkey, ret);
558 key_put(authkey);
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400559}
560
561static void
562nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
563{
Trond Myklebust0cac12022012-09-27 16:15:00 -0400564 if (idmap->idmap_upcall_data != NULL)
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400565 nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
566}
567
David Howells822ad642019-02-14 16:20:25 +0000568static int nfs_idmap_legacy_upcall(struct key *authkey, void *aux)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Bryan Schumakerc5066942012-08-09 14:05:49 -0400570 struct idmap_legacy_upcalldata *data;
David Howells822ad642019-02-14 16:20:25 +0000571 struct request_key_auth *rka = get_request_key_auth(authkey);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500572 struct rpc_pipe_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct idmap_msg *im;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500574 struct idmap *idmap = (struct idmap *)aux;
David Howells822ad642019-02-14 16:20:25 +0000575 struct key *key = rka->target_key;
Eric Biggers49686cb2018-01-19 15:15:34 -0800576 int ret = -ENOKEY;
577
578 if (!aux)
579 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Bryan Schumaker57e62322012-02-24 14:14:51 -0500581 /* msg and im are freed in idmap_pipe_destroy_msg */
Eric Biggers49686cb2018-01-19 15:15:34 -0800582 ret = -ENOMEM;
Bryan Schumaker57a51042012-08-09 14:05:51 -0400583 data = kzalloc(sizeof(*data), GFP_KERNEL);
Bryan Schumakerc5066942012-08-09 14:05:49 -0400584 if (!data)
Bryan Schumaker57e62322012-02-24 14:14:51 -0500585 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Bryan Schumakerc5066942012-08-09 14:05:49 -0400587 msg = &data->pipe_msg;
588 im = &data->idmap_msg;
589 data->idmap = idmap;
David Howells822ad642019-02-14 16:20:25 +0000590 data->authkey = key_get(authkey);
Bryan Schumakerc5066942012-08-09 14:05:49 -0400591
592 ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500593 if (ret < 0)
594 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400596 ret = -EAGAIN;
Trond Myklebust0cac12022012-09-27 16:15:00 -0400597 if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
Trond Myklebust0e24d842012-09-28 12:03:09 -0400598 goto out2;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500599
Bryan Schumaker11588f42012-03-12 11:33:00 -0400600 ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
601 if (ret < 0)
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400602 nfs_idmap_abort_pipe_upcall(idmap, ret);
Bryan Schumaker11588f42012-03-12 11:33:00 -0400603
604 return ret;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500605out2:
Bryan Schumakerc5066942012-08-09 14:05:49 -0400606 kfree(data);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500607out1:
David Howells822ad642019-02-14 16:20:25 +0000608 complete_request_key(authkey, ret);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500609 return ret;
610}
611
Trond Myklebustcf4ab532013-03-08 12:56:37 -0500612static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
Bryan Schumaker57e62322012-02-24 14:14:51 -0500613{
Trond Myklebustcf4ab532013-03-08 12:56:37 -0500614 return key_instantiate_and_link(key, data, datalen,
Bryan Schumaker57e62322012-02-24 14:14:51 -0500615 id_resolver_cache->thread_keyring,
616 authkey);
617}
618
Trond Myklebust0cac12022012-09-27 16:15:00 -0400619static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
620 struct idmap_msg *upcall,
621 struct key *key, struct key *authkey)
Bryan Schumaker57e62322012-02-24 14:14:51 -0500622{
623 char id_str[NFS_UINT_MAXLEN];
Trond Myklebustcf4ab532013-03-08 12:56:37 -0500624 size_t len;
Trond Myklebust0cac12022012-09-27 16:15:00 -0400625 int ret = -ENOKEY;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500626
Trond Myklebust0cac12022012-09-27 16:15:00 -0400627 /* ret = -ENOKEY */
628 if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
629 goto out;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500630 switch (im->im_conv) {
631 case IDMAP_CONV_NAMETOID:
Trond Myklebust0cac12022012-09-27 16:15:00 -0400632 if (strcmp(upcall->im_name, im->im_name) != 0)
633 break;
Trond Myklebustcf4ab532013-03-08 12:56:37 -0500634 /* Note: here we store the NUL terminator too */
Dave Wysochanskid6889482018-05-29 17:47:30 -0400635 len = 1 + nfs_map_numeric_to_string(im->im_id, id_str,
636 sizeof(id_str));
Trond Myklebustcf4ab532013-03-08 12:56:37 -0500637 ret = nfs_idmap_instantiate(key, authkey, id_str, len);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500638 break;
639 case IDMAP_CONV_IDTONAME:
Trond Myklebust0cac12022012-09-27 16:15:00 -0400640 if (upcall->im_id != im->im_id)
641 break;
Trond Myklebustcf4ab532013-03-08 12:56:37 -0500642 len = strlen(im->im_name);
643 ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500644 break;
Trond Myklebust0cac12022012-09-27 16:15:00 -0400645 default:
646 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Trond Myklebust0cac12022012-09-27 16:15:00 -0400648out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return ret;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static ssize_t
653idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
654{
David Howells822ad642019-02-14 16:20:25 +0000655 struct request_key_auth *rka;
Al Viro496ad9a2013-01-23 17:07:38 -0500656 struct rpc_inode *rpci = RPC_I(file_inode(filp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct idmap *idmap = (struct idmap *)rpci->private;
David Howells822ad642019-02-14 16:20:25 +0000658 struct key *authkey;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500659 struct idmap_msg im;
Chuck Leverd24aae42007-12-20 14:54:49 -0500660 size_t namelen_in;
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400661 int ret = -ENOKEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
David Howellsa427b9e2012-07-25 16:53:36 +0100663 /* If instantiation is successful, anyone waiting for key construction
664 * will have been woken up and someone else may now have used
665 * idmap_key_cons - so after this point we may no longer touch it.
666 */
Trond Myklebust0cac12022012-09-27 16:15:00 -0400667 if (idmap->idmap_upcall_data == NULL)
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400668 goto out_noupcall;
David Howellsa427b9e2012-07-25 16:53:36 +0100669
David Howells822ad642019-02-14 16:20:25 +0000670 authkey = idmap->idmap_upcall_data->authkey;
671 rka = get_request_key_auth(authkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Bryan Schumaker57e62322012-02-24 14:14:51 -0500673 if (mlen != sizeof(im)) {
674 ret = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 goto out;
676 }
677
Bryan Schumaker57e62322012-02-24 14:14:51 -0500678 if (copy_from_user(&im, src, mlen) != 0) {
679 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 goto out;
681 }
682
Bryan Schumaker57e62322012-02-24 14:14:51 -0500683 if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
Bryan Schumaker12dfd082012-08-09 14:05:50 -0400684 ret = -ENOKEY;
685 goto out;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500686 }
687
688 namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
689 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
690 ret = -EINVAL;
691 goto out;
Trond Myklebust0cac12022012-09-27 16:15:00 -0400692}
Bryan Schumaker57e62322012-02-24 14:14:51 -0500693
Trond Myklebust0cac12022012-09-27 16:15:00 -0400694 ret = nfs_idmap_read_and_verify_message(&im,
695 &idmap->idmap_upcall_data->idmap_msg,
David Howells822ad642019-02-14 16:20:25 +0000696 rka->target_key, authkey);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500697 if (ret >= 0) {
David Howells822ad642019-02-14 16:20:25 +0000698 key_set_timeout(rka->target_key, nfs_idmap_cache_timeout);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500699 ret = mlen;
700 }
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702out:
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400703 nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
704out_noupcall:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return ret;
706}
707
Adrian Bunk75c96f82005-05-05 16:16:09 -0700708static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
710{
Bryan Schumakerc5066942012-08-09 14:05:49 -0400711 struct idmap_legacy_upcalldata *data = container_of(msg,
712 struct idmap_legacy_upcalldata,
713 pipe_msg);
714 struct idmap *idmap = data->idmap;
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400715
716 if (msg->errno)
717 nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
Bryan Schumakerc5066942012-08-09 14:05:49 -0400718}
719
720static void
721idmap_release_pipe(struct inode *inode)
722{
723 struct rpc_inode *rpci = RPC_I(inode);
724 struct idmap *idmap = (struct idmap *)rpci->private;
Trond Myklebuste9ab41b62012-09-27 15:44:19 -0400725
726 nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800729int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800731 struct idmap *idmap = server->nfs_client->cl_idmap;
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800732 __u32 id = -1;
733 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800735 if (!nfs_map_string_to_numeric(name, namelen, &id))
736 ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
737 if (ret == 0) {
738 *uid = make_kuid(&init_user_ns, id);
739 if (!uid_valid(*uid))
740 ret = -ERANGE;
741 }
Trond Myklebust1f2d30b2013-08-13 11:34:01 -0400742 trace_nfs4_map_name_to_uid(name, namelen, id, ret);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800743 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800745
746int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
747{
748 struct idmap *idmap = server->nfs_client->cl_idmap;
749 __u32 id = -1;
750 int ret = 0;
751
752 if (!nfs_map_string_to_numeric(name, namelen, &id))
753 ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
754 if (ret == 0) {
755 *gid = make_kgid(&init_user_ns, id);
756 if (!gid_valid(*gid))
757 ret = -ERANGE;
758 }
Trond Myklebust1f2d30b2013-08-13 11:34:01 -0400759 trace_nfs4_map_group_to_gid(name, namelen, id, ret);
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800760 return ret;
761}
762
763int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800765 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800766 int ret = -EINVAL;
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800767 __u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800769 id = from_kuid(&init_user_ns, uid);
Trond Myklebustb064eca22011-02-22 15:44:32 -0800770 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800771 ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800772 if (ret < 0)
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800773 ret = nfs_map_numeric_to_string(id, buf, buflen);
Trond Myklebust1f2d30b2013-08-13 11:34:01 -0400774 trace_nfs4_map_uid_to_name(buf, ret, id, ret);
Eric W. Biederman9f309c82013-02-01 03:21:47 -0800775 return ret;
776}
777int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
778{
779 struct idmap *idmap = server->nfs_client->cl_idmap;
780 int ret = -EINVAL;
781 __u32 id;
782
783 id = from_kgid(&init_user_ns, gid);
784 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
785 ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
786 if (ret < 0)
787 ret = nfs_map_numeric_to_string(id, buf, buflen);
Trond Myklebust1f2d30b2013-08-13 11:34:01 -0400788 trace_nfs4_map_gid_to_group(buf, ret, id, ret);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800789 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}