xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_fuid.c (revision 80aae8a3f8aa70712930664572be9e6885dc0be7)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/dmu.h>
28 #include <sys/avl.h>
29 #include <sys/zap.h>
30 #include <sys/nvpair.h>
31 #include <sys/sid.h>
32 #ifdef _KERNEL
33 #include <sys/zfs_vfsops.h>
34 #include <sys/zfs_znode.h>
35 #endif
36 #include <sys/zfs_fuid.h>
37 
38 /*
39  * FUID Domain table(s).
40  *
41  * The FUID table is stored as a packed nvlist of an array
42  * of nvlists which contain an index, domain string and offset
43  *
44  * During file system initialization the nvlist(s) are read and
45  * two AVL trees are created.  One tree is keyed by the index number
46  * and the other by the domain string.  Nodes are never removed from
47  * trees, but new entries may be added.  If a new entry is added then
48  * the zfsvfs->z_fuid_dirty flag is set to true and the caller will then
49  * be responsible for calling zfs_fuid_sync() to sync the changes to disk.
50  *
51  */
52 
53 #define	FUID_IDX	"fuid_idx"
54 #define	FUID_DOMAIN	"fuid_domain"
55 #define	FUID_OFFSET	"fuid_offset"
56 #define	FUID_NVP_ARRAY	"fuid_nvlist"
57 
58 typedef struct fuid_domain {
59 	avl_node_t	f_domnode;
60 	avl_node_t	f_idxnode;
61 	ksiddomain_t	*f_ksid;
62 	uint64_t	f_idx;
63 } fuid_domain_t;
64 
65 static const char *const nulldomain = "";
66 
67 /*
68  * Compare two indexes.
69  */
70 static int
idx_compare(const void * arg1,const void * arg2)71 idx_compare(const void *arg1, const void *arg2)
72 {
73 	const fuid_domain_t *node1 = (const fuid_domain_t *)arg1;
74 	const fuid_domain_t *node2 = (const fuid_domain_t *)arg2;
75 
76 	return (TREE_CMP(node1->f_idx, node2->f_idx));
77 }
78 
79 /*
80  * Compare two domain strings.
81  */
82 static int
domain_compare(const void * arg1,const void * arg2)83 domain_compare(const void *arg1, const void *arg2)
84 {
85 	const fuid_domain_t *node1 = (const fuid_domain_t *)arg1;
86 	const fuid_domain_t *node2 = (const fuid_domain_t *)arg2;
87 	return (TREE_ISIGN(strcmp(node1->f_ksid->kd_name,
88 	    node2->f_ksid->kd_name)));
89 }
90 
91 void
zfs_fuid_avl_tree_create(avl_tree_t * idx_tree,avl_tree_t * domain_tree)92 zfs_fuid_avl_tree_create(avl_tree_t *idx_tree, avl_tree_t *domain_tree)
93 {
94 	avl_create(idx_tree, idx_compare,
95 	    sizeof (fuid_domain_t), offsetof(fuid_domain_t, f_idxnode));
96 	avl_create(domain_tree, domain_compare,
97 	    sizeof (fuid_domain_t), offsetof(fuid_domain_t, f_domnode));
98 }
99 
100 /*
101  * load initial fuid domain and idx trees.  This function is used by
102  * both the kernel and zdb.
103  */
104 uint64_t
zfs_fuid_table_load(objset_t * os,uint64_t fuid_obj,avl_tree_t * idx_tree,avl_tree_t * domain_tree)105 zfs_fuid_table_load(objset_t *os, uint64_t fuid_obj, avl_tree_t *idx_tree,
106     avl_tree_t *domain_tree)
107 {
108 	dmu_buf_t *db;
109 	uint64_t fuid_size;
110 
111 	ASSERT(fuid_obj != 0);
112 	VERIFY0(dmu_bonus_hold(os, fuid_obj, FTAG, &db));
113 	fuid_size = *(uint64_t *)db->db_data;
114 	dmu_buf_rele(db, FTAG);
115 
116 	if (fuid_size)  {
117 		nvlist_t **fuidnvp;
118 		nvlist_t *nvp = NULL;
119 		uint_t count;
120 		char *packed;
121 		int i;
122 
123 		packed = kmem_alloc(fuid_size, KM_SLEEP);
124 		VERIFY0(dmu_read(os, fuid_obj, 0,
125 		    fuid_size, packed, DMU_READ_PREFETCH));
126 		VERIFY0(nvlist_unpack(packed, fuid_size, &nvp, 0));
127 		VERIFY0(nvlist_lookup_nvlist_array(nvp, FUID_NVP_ARRAY,
128 		    &fuidnvp, &count));
129 
130 		for (i = 0; i != count; i++) {
131 			fuid_domain_t *domnode;
132 			const char *domain;
133 			uint64_t idx;
134 
135 			VERIFY0(nvlist_lookup_string(fuidnvp[i], FUID_DOMAIN,
136 			    &domain));
137 			VERIFY0(nvlist_lookup_uint64(fuidnvp[i], FUID_IDX,
138 			    &idx));
139 
140 			domnode = kmem_alloc(sizeof (fuid_domain_t), KM_SLEEP);
141 
142 			domnode->f_idx = idx;
143 			domnode->f_ksid = ksid_lookupdomain(domain);
144 			avl_add(idx_tree, domnode);
145 			avl_add(domain_tree, domnode);
146 		}
147 		nvlist_free(nvp);
148 		kmem_free(packed, fuid_size);
149 	}
150 	return (fuid_size);
151 }
152 
153 void
zfs_fuid_table_destroy(avl_tree_t * idx_tree,avl_tree_t * domain_tree)154 zfs_fuid_table_destroy(avl_tree_t *idx_tree, avl_tree_t *domain_tree)
155 {
156 	fuid_domain_t *domnode;
157 	void *cookie;
158 
159 	cookie = NULL;
160 	while ((domnode = avl_destroy_nodes(domain_tree, &cookie)))
161 		ksiddomain_rele(domnode->f_ksid);
162 
163 	avl_destroy(domain_tree);
164 	cookie = NULL;
165 	while ((domnode = avl_destroy_nodes(idx_tree, &cookie)))
166 		kmem_free(domnode, sizeof (fuid_domain_t));
167 	avl_destroy(idx_tree);
168 }
169 
170 const char *
zfs_fuid_idx_domain(avl_tree_t * idx_tree,uint32_t idx)171 zfs_fuid_idx_domain(avl_tree_t *idx_tree, uint32_t idx)
172 {
173 	fuid_domain_t searchnode, *findnode;
174 	avl_index_t loc;
175 
176 	searchnode.f_idx = idx;
177 
178 	findnode = avl_find(idx_tree, &searchnode, &loc);
179 
180 	return (findnode ? findnode->f_ksid->kd_name : nulldomain);
181 }
182 
183 #ifdef _KERNEL
184 /*
185  * Load the fuid table(s) into memory.
186  */
187 static void
zfs_fuid_init(zfsvfs_t * zfsvfs)188 zfs_fuid_init(zfsvfs_t *zfsvfs)
189 {
190 	rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
191 
192 	if (zfsvfs->z_fuid_loaded) {
193 		rw_exit(&zfsvfs->z_fuid_lock);
194 		return;
195 	}
196 
197 	zfs_fuid_avl_tree_create(&zfsvfs->z_fuid_idx, &zfsvfs->z_fuid_domain);
198 
199 	(void) zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
200 	    ZFS_FUID_TABLES, 8, 1, &zfsvfs->z_fuid_obj);
201 	if (zfsvfs->z_fuid_obj != 0) {
202 		zfsvfs->z_fuid_size = zfs_fuid_table_load(zfsvfs->z_os,
203 		    zfsvfs->z_fuid_obj, &zfsvfs->z_fuid_idx,
204 		    &zfsvfs->z_fuid_domain);
205 	}
206 
207 	zfsvfs->z_fuid_loaded = B_TRUE;
208 	rw_exit(&zfsvfs->z_fuid_lock);
209 }
210 
211 /*
212  * sync out AVL trees to persistent storage.
213  */
214 void
zfs_fuid_sync(zfsvfs_t * zfsvfs,dmu_tx_t * tx)215 zfs_fuid_sync(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
216 {
217 	nvlist_t *nvp;
218 	nvlist_t **fuids;
219 	size_t nvsize = 0;
220 	char *packed;
221 	dmu_buf_t *db;
222 	fuid_domain_t *domnode;
223 	int numnodes;
224 	int i;
225 
226 	if (!zfsvfs->z_fuid_dirty) {
227 		return;
228 	}
229 
230 	rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
231 
232 	/*
233 	 * First see if table needs to be created?
234 	 */
235 	if (zfsvfs->z_fuid_obj == 0) {
236 		zfsvfs->z_fuid_obj = dmu_object_alloc(zfsvfs->z_os,
237 		    DMU_OT_FUID, 1 << 14, DMU_OT_FUID_SIZE,
238 		    sizeof (uint64_t), tx);
239 		VERIFY(zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
240 		    ZFS_FUID_TABLES, sizeof (uint64_t), 1,
241 		    &zfsvfs->z_fuid_obj, tx) == 0);
242 	}
243 
244 	VERIFY0(nvlist_alloc(&nvp, NV_UNIQUE_NAME, KM_SLEEP));
245 
246 	numnodes = avl_numnodes(&zfsvfs->z_fuid_idx);
247 	fuids = kmem_alloc(numnodes * sizeof (void *), KM_SLEEP);
248 	for (i = 0, domnode = avl_first(&zfsvfs->z_fuid_domain); domnode; i++,
249 	    domnode = AVL_NEXT(&zfsvfs->z_fuid_domain, domnode)) {
250 		VERIFY0(nvlist_alloc(&fuids[i], NV_UNIQUE_NAME, KM_SLEEP));
251 		VERIFY0(nvlist_add_uint64(fuids[i], FUID_IDX,
252 		    domnode->f_idx));
253 		VERIFY0(nvlist_add_uint64(fuids[i], FUID_OFFSET, 0));
254 		VERIFY0(nvlist_add_string(fuids[i], FUID_DOMAIN,
255 		    domnode->f_ksid->kd_name));
256 	}
257 	fnvlist_add_nvlist_array(nvp, FUID_NVP_ARRAY,
258 	    (const nvlist_t * const *)fuids, numnodes);
259 	for (i = 0; i != numnodes; i++)
260 		nvlist_free(fuids[i]);
261 	kmem_free(fuids, numnodes * sizeof (void *));
262 	VERIFY0(nvlist_size(nvp, &nvsize, NV_ENCODE_XDR));
263 	packed = kmem_alloc(nvsize, KM_SLEEP);
264 	VERIFY0(nvlist_pack(nvp, &packed, &nvsize, NV_ENCODE_XDR, KM_SLEEP));
265 	nvlist_free(nvp);
266 	zfsvfs->z_fuid_size = nvsize;
267 	dmu_write(zfsvfs->z_os, zfsvfs->z_fuid_obj, 0,
268 	    zfsvfs->z_fuid_size, packed, tx, DMU_READ_NO_PREFETCH);
269 	kmem_free(packed, zfsvfs->z_fuid_size);
270 	VERIFY0(dmu_bonus_hold(zfsvfs->z_os, zfsvfs->z_fuid_obj, FTAG, &db));
271 	dmu_buf_will_dirty(db, tx);
272 	*(uint64_t *)db->db_data = zfsvfs->z_fuid_size;
273 	dmu_buf_rele(db, FTAG);
274 
275 	zfsvfs->z_fuid_dirty = B_FALSE;
276 	rw_exit(&zfsvfs->z_fuid_lock);
277 }
278 
279 /*
280  * Query domain table for a given domain.
281  *
282  * If domain isn't found and addok is set, it is added to AVL trees and
283  * the zfsvfs->z_fuid_dirty flag will be set to TRUE.  It will then be
284  * necessary for the caller or another thread to detect the dirty table
285  * and sync out the changes.
286  */
287 static int
zfs_fuid_find_by_domain(zfsvfs_t * zfsvfs,const char * domain,const char ** retdomain,boolean_t addok)288 zfs_fuid_find_by_domain(zfsvfs_t *zfsvfs, const char *domain,
289     const char **retdomain, boolean_t addok)
290 {
291 	fuid_domain_t searchnode, *findnode;
292 	avl_index_t loc;
293 	krw_t rw = RW_READER;
294 
295 	/*
296 	 * If the dummy "nobody" domain then return an index of 0
297 	 * to cause the created FUID to be a standard POSIX id
298 	 * for the user nobody.
299 	 */
300 	if (domain[0] == '\0') {
301 		if (retdomain)
302 			*retdomain = nulldomain;
303 		return (0);
304 	}
305 
306 	searchnode.f_ksid = ksid_lookupdomain(domain);
307 	if (retdomain)
308 		*retdomain = searchnode.f_ksid->kd_name;
309 	if (!zfsvfs->z_fuid_loaded)
310 		zfs_fuid_init(zfsvfs);
311 
312 retry:
313 	rw_enter(&zfsvfs->z_fuid_lock, rw);
314 	findnode = avl_find(&zfsvfs->z_fuid_domain, &searchnode, &loc);
315 
316 	if (findnode) {
317 		rw_exit(&zfsvfs->z_fuid_lock);
318 		ksiddomain_rele(searchnode.f_ksid);
319 		return (findnode->f_idx);
320 	} else if (addok) {
321 		fuid_domain_t *domnode;
322 		uint64_t retidx;
323 
324 		if (rw == RW_READER && !rw_tryupgrade(&zfsvfs->z_fuid_lock)) {
325 			rw_exit(&zfsvfs->z_fuid_lock);
326 			rw = RW_WRITER;
327 			goto retry;
328 		}
329 
330 		domnode = kmem_alloc(sizeof (fuid_domain_t), KM_SLEEP);
331 		domnode->f_ksid = searchnode.f_ksid;
332 
333 		retidx = domnode->f_idx = avl_numnodes(&zfsvfs->z_fuid_idx) + 1;
334 
335 		avl_add(&zfsvfs->z_fuid_domain, domnode);
336 		avl_add(&zfsvfs->z_fuid_idx, domnode);
337 		zfsvfs->z_fuid_dirty = B_TRUE;
338 		rw_exit(&zfsvfs->z_fuid_lock);
339 		return (retidx);
340 	} else {
341 		rw_exit(&zfsvfs->z_fuid_lock);
342 		return (-1);
343 	}
344 }
345 
346 /*
347  * Query domain table by index, returning domain string
348  *
349  * Returns a pointer from an avl node of the domain string.
350  *
351  */
352 const char *
zfs_fuid_find_by_idx(zfsvfs_t * zfsvfs,uint32_t idx)353 zfs_fuid_find_by_idx(zfsvfs_t *zfsvfs, uint32_t idx)
354 {
355 	const char *domain;
356 
357 	if (idx == 0 || !zfsvfs->z_use_fuids)
358 		return (NULL);
359 
360 	if (!zfsvfs->z_fuid_loaded)
361 		zfs_fuid_init(zfsvfs);
362 
363 	rw_enter(&zfsvfs->z_fuid_lock, RW_READER);
364 
365 	if (zfsvfs->z_fuid_obj || zfsvfs->z_fuid_dirty)
366 		domain = zfs_fuid_idx_domain(&zfsvfs->z_fuid_idx, idx);
367 	else
368 		domain = nulldomain;
369 	rw_exit(&zfsvfs->z_fuid_lock);
370 
371 	ASSERT(domain);
372 	return (domain);
373 }
374 
375 void
zfs_fuid_map_ids(znode_t * zp,cred_t * cr,uid_t * uidp,uid_t * gidp)376 zfs_fuid_map_ids(znode_t *zp, cred_t *cr, uid_t *uidp, uid_t *gidp)
377 {
378 	*uidp = zfs_fuid_map_id(ZTOZSB(zp), KUID_TO_SUID(ZTOUID(zp)),
379 	    cr, ZFS_OWNER);
380 	*gidp = zfs_fuid_map_id(ZTOZSB(zp), KGID_TO_SGID(ZTOGID(zp)),
381 	    cr, ZFS_GROUP);
382 }
383 
384 #ifdef __FreeBSD__
385 uid_t
zfs_fuid_map_id(zfsvfs_t * zfsvfs,uint64_t fuid,cred_t * cr,zfs_fuid_type_t type)386 zfs_fuid_map_id(zfsvfs_t *zfsvfs, uint64_t fuid,
387     cred_t *cr, zfs_fuid_type_t type)
388 {
389 	uint32_t index = FUID_INDEX(fuid);
390 
391 	if (index == 0)
392 		return (fuid);
393 
394 	return (UID_NOBODY);
395 }
396 #elif defined(__linux__)
397 uid_t
zfs_fuid_map_id(zfsvfs_t * zfsvfs,uint64_t fuid,cred_t * cr,zfs_fuid_type_t type)398 zfs_fuid_map_id(zfsvfs_t *zfsvfs, uint64_t fuid,
399     cred_t *cr, zfs_fuid_type_t type)
400 {
401 	/*
402 	 * The Linux port only supports POSIX IDs, use the passed id.
403 	 */
404 	return (fuid);
405 }
406 
407 #else
408 uid_t
zfs_fuid_map_id(zfsvfs_t * zfsvfs,uint64_t fuid,cred_t * cr,zfs_fuid_type_t type)409 zfs_fuid_map_id(zfsvfs_t *zfsvfs, uint64_t fuid,
410     cred_t *cr, zfs_fuid_type_t type)
411 {
412 	uint32_t index = FUID_INDEX(fuid);
413 	const char *domain;
414 	uid_t id;
415 
416 	if (index == 0)
417 		return (fuid);
418 
419 	domain = zfs_fuid_find_by_idx(zfsvfs, index);
420 	ASSERT(domain != NULL);
421 
422 	if (type == ZFS_OWNER || type == ZFS_ACE_USER) {
423 		(void) kidmap_getuidbysid(crgetzone(cr), domain,
424 		    FUID_RID(fuid), &id);
425 	} else {
426 		(void) kidmap_getgidbysid(crgetzone(cr), domain,
427 		    FUID_RID(fuid), &id);
428 	}
429 	return (id);
430 }
431 #endif
432 
433 /*
434  * Add a FUID node to the list of fuid's being created for this
435  * ACL
436  *
437  * If ACL has multiple domains, then keep only one copy of each unique
438  * domain.
439  */
440 void
zfs_fuid_node_add(zfs_fuid_info_t ** fuidpp,const char * domain,uint32_t rid,uint64_t idx,uint64_t id,zfs_fuid_type_t type)441 zfs_fuid_node_add(zfs_fuid_info_t **fuidpp, const char *domain, uint32_t rid,
442     uint64_t idx, uint64_t id, zfs_fuid_type_t type)
443 {
444 	zfs_fuid_t *fuid;
445 	zfs_fuid_domain_t *fuid_domain;
446 	zfs_fuid_info_t *fuidp;
447 	uint64_t fuididx;
448 	boolean_t found = B_FALSE;
449 
450 	if (*fuidpp == NULL)
451 		*fuidpp = zfs_fuid_info_alloc();
452 
453 	fuidp = *fuidpp;
454 	/*
455 	 * First find fuid domain index in linked list
456 	 *
457 	 * If one isn't found then create an entry.
458 	 */
459 
460 	for (fuididx = 1, fuid_domain = list_head(&fuidp->z_domains);
461 	    fuid_domain; fuid_domain = list_next(&fuidp->z_domains,
462 	    fuid_domain), fuididx++) {
463 		if (idx == fuid_domain->z_domidx) {
464 			found = B_TRUE;
465 			break;
466 		}
467 	}
468 
469 	if (!found) {
470 		fuid_domain = kmem_alloc(sizeof (zfs_fuid_domain_t), KM_SLEEP);
471 		fuid_domain->z_domain = domain;
472 		fuid_domain->z_domidx = idx;
473 		list_insert_tail(&fuidp->z_domains, fuid_domain);
474 		fuidp->z_domain_str_sz += strlen(domain) + 1;
475 		fuidp->z_domain_cnt++;
476 	}
477 
478 	if (type == ZFS_ACE_USER || type == ZFS_ACE_GROUP) {
479 
480 		/*
481 		 * Now allocate fuid entry and add it on the end of the list
482 		 */
483 
484 		fuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
485 		fuid->z_id = id;
486 		fuid->z_domidx = idx;
487 		fuid->z_logfuid = FUID_ENCODE(fuididx, rid);
488 
489 		list_insert_tail(&fuidp->z_fuids, fuid);
490 		fuidp->z_fuid_cnt++;
491 	} else {
492 		if (type == ZFS_OWNER)
493 			fuidp->z_fuid_owner = FUID_ENCODE(fuididx, rid);
494 		else
495 			fuidp->z_fuid_group = FUID_ENCODE(fuididx, rid);
496 	}
497 }
498 
499 #ifdef HAVE_KSID
500 /*
501  * Create a file system FUID, based on information in the users cred
502  *
503  * If cred contains KSID_OWNER then it should be used to determine
504  * the uid otherwise cred's uid will be used. By default cred's gid
505  * is used unless it's an ephemeral ID in which case KSID_GROUP will
506  * be used if it exists.
507  */
508 uint64_t
zfs_fuid_create_cred(zfsvfs_t * zfsvfs,zfs_fuid_type_t type,cred_t * cr,zfs_fuid_info_t ** fuidp)509 zfs_fuid_create_cred(zfsvfs_t *zfsvfs, zfs_fuid_type_t type,
510     cred_t *cr, zfs_fuid_info_t **fuidp)
511 {
512 	uint64_t	idx;
513 	ksid_t		*ksid;
514 	uint32_t	rid;
515 	const char	*kdomain, *domain;
516 	uid_t		id;
517 
518 	VERIFY(type == ZFS_OWNER || type == ZFS_GROUP);
519 
520 	ksid = crgetsid(cr, (type == ZFS_OWNER) ? KSID_OWNER : KSID_GROUP);
521 
522 	if (!zfsvfs->z_use_fuids || (ksid == NULL)) {
523 		id = (type == ZFS_OWNER) ? crgetuid(cr) : crgetgid(cr);
524 
525 		if (IS_EPHEMERAL(id))
526 			return ((type == ZFS_OWNER) ? UID_NOBODY : GID_NOBODY);
527 
528 		return ((uint64_t)id);
529 	}
530 
531 	/*
532 	 * ksid is present and FUID is supported
533 	 */
534 	id = (type == ZFS_OWNER) ? ksid_getid(ksid) : crgetgid(cr);
535 
536 	if (!IS_EPHEMERAL(id))
537 		return ((uint64_t)id);
538 
539 	if (type == ZFS_GROUP)
540 		id = ksid_getid(ksid);
541 
542 	rid = ksid_getrid(ksid);
543 	domain = ksid_getdomain(ksid);
544 
545 	idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, B_TRUE);
546 
547 	zfs_fuid_node_add(fuidp, kdomain, rid, idx, id, type);
548 
549 	return (FUID_ENCODE(idx, rid));
550 }
551 #endif /* HAVE_KSID */
552 
553 /*
554  * Create a file system FUID for an ACL ace
555  * or a chown/chgrp of the file.
556  * This is similar to zfs_fuid_create_cred, except that
557  * we can't find the domain + rid information in the
558  * cred.  Instead we have to query Winchester for the
559  * domain and rid.
560  *
561  * During replay operations the domain+rid information is
562  * found in the zfs_fuid_info_t that the replay code has
563  * attached to the zfsvfs of the file system.
564  */
565 uint64_t
zfs_fuid_create(zfsvfs_t * zfsvfs,uint64_t id,cred_t * cr,zfs_fuid_type_t type,zfs_fuid_info_t ** fuidpp)566 zfs_fuid_create(zfsvfs_t *zfsvfs, uint64_t id, cred_t *cr,
567     zfs_fuid_type_t type, zfs_fuid_info_t **fuidpp)
568 {
569 #ifdef HAVE_KSID
570 	const char *domain, *kdomain;
571 	uint32_t fuid_idx = FUID_INDEX(id);
572 	uint32_t rid = 0;
573 	idmap_stat status;
574 	uint64_t idx = UID_NOBODY;
575 	zfs_fuid_t *zfuid = NULL;
576 	zfs_fuid_info_t *fuidp = NULL;
577 
578 	/*
579 	 * If POSIX ID, or entry is already a FUID then
580 	 * just return the id
581 	 *
582 	 * We may also be handed an already FUID'ized id via
583 	 * chmod.
584 	 */
585 
586 	if (!zfsvfs->z_use_fuids || !IS_EPHEMERAL(id) || fuid_idx != 0)
587 		return (id);
588 
589 	if (zfsvfs->z_replay) {
590 		fuidp = zfsvfs->z_fuid_replay;
591 
592 		/*
593 		 * If we are passed an ephemeral id, but no
594 		 * fuid_info was logged then return NOBODY.
595 		 * This is most likely a result of idmap service
596 		 * not being available.
597 		 */
598 		if (fuidp == NULL)
599 			return (UID_NOBODY);
600 
601 		VERIFY3U(type, >=, ZFS_OWNER);
602 		VERIFY3U(type, <=, ZFS_ACE_GROUP);
603 
604 		switch (type) {
605 		case ZFS_ACE_USER:
606 		case ZFS_ACE_GROUP:
607 			zfuid = list_head(&fuidp->z_fuids);
608 			rid = FUID_RID(zfuid->z_logfuid);
609 			idx = FUID_INDEX(zfuid->z_logfuid);
610 			break;
611 		case ZFS_OWNER:
612 			rid = FUID_RID(fuidp->z_fuid_owner);
613 			idx = FUID_INDEX(fuidp->z_fuid_owner);
614 			break;
615 		case ZFS_GROUP:
616 			rid = FUID_RID(fuidp->z_fuid_group);
617 			idx = FUID_INDEX(fuidp->z_fuid_group);
618 			break;
619 		}
620 		domain = fuidp->z_domain_table[idx - 1];
621 	} else {
622 		if (type == ZFS_OWNER || type == ZFS_ACE_USER)
623 			status = kidmap_getsidbyuid(crgetzone(cr), id,
624 			    &domain, &rid);
625 		else
626 			status = kidmap_getsidbygid(crgetzone(cr), id,
627 			    &domain, &rid);
628 
629 		if (status != 0) {
630 			/*
631 			 * When returning nobody we will need to
632 			 * make a dummy fuid table entry for logging
633 			 * purposes.
634 			 */
635 			rid = UID_NOBODY;
636 			domain = nulldomain;
637 		}
638 	}
639 
640 	idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, B_TRUE);
641 
642 	if (!zfsvfs->z_replay)
643 		zfs_fuid_node_add(fuidpp, kdomain,
644 		    rid, idx, id, type);
645 	else if (zfuid != NULL) {
646 		list_remove(&fuidp->z_fuids, zfuid);
647 		kmem_free(zfuid, sizeof (zfs_fuid_t));
648 	}
649 	return (FUID_ENCODE(idx, rid));
650 #else
651 	/*
652 	 * The Linux port only supports POSIX IDs, use the passed id.
653 	 */
654 	return (id);
655 #endif
656 }
657 
658 void
zfs_fuid_destroy(zfsvfs_t * zfsvfs)659 zfs_fuid_destroy(zfsvfs_t *zfsvfs)
660 {
661 	rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
662 	if (!zfsvfs->z_fuid_loaded) {
663 		rw_exit(&zfsvfs->z_fuid_lock);
664 		return;
665 	}
666 	zfs_fuid_table_destroy(&zfsvfs->z_fuid_idx, &zfsvfs->z_fuid_domain);
667 	rw_exit(&zfsvfs->z_fuid_lock);
668 }
669 
670 /*
671  * Allocate zfs_fuid_info for tracking FUIDs created during
672  * zfs_mknode, VOP_SETATTR() or VOP_SETSECATTR()
673  */
674 zfs_fuid_info_t *
zfs_fuid_info_alloc(void)675 zfs_fuid_info_alloc(void)
676 {
677 	zfs_fuid_info_t *fuidp;
678 
679 	fuidp = kmem_zalloc(sizeof (zfs_fuid_info_t), KM_SLEEP);
680 	list_create(&fuidp->z_domains, sizeof (zfs_fuid_domain_t),
681 	    offsetof(zfs_fuid_domain_t, z_next));
682 	list_create(&fuidp->z_fuids, sizeof (zfs_fuid_t),
683 	    offsetof(zfs_fuid_t, z_next));
684 	return (fuidp);
685 }
686 
687 /*
688  * Release all memory associated with zfs_fuid_info_t
689  */
690 void
zfs_fuid_info_free(zfs_fuid_info_t * fuidp)691 zfs_fuid_info_free(zfs_fuid_info_t *fuidp)
692 {
693 	zfs_fuid_t *zfuid;
694 	zfs_fuid_domain_t *zdomain;
695 
696 	while ((zfuid = list_remove_head(&fuidp->z_fuids)) != NULL)
697 		kmem_free(zfuid, sizeof (zfs_fuid_t));
698 
699 	if (fuidp->z_domain_table != NULL)
700 		kmem_free(fuidp->z_domain_table,
701 		    (sizeof (char *)) * fuidp->z_domain_cnt);
702 
703 	while ((zdomain = list_remove_head(&fuidp->z_domains)) != NULL)
704 		kmem_free(zdomain, sizeof (zfs_fuid_domain_t));
705 
706 	kmem_free(fuidp, sizeof (zfs_fuid_info_t));
707 }
708 
709 /*
710  * Check to see if id is a groupmember.  If cred
711  * has ksid info then sidlist is checked first
712  * and if still not found then POSIX groups are checked
713  *
714  * Will use a straight FUID compare when possible.
715  */
716 boolean_t
zfs_groupmember(zfsvfs_t * zfsvfs,uint64_t id,cred_t * cr)717 zfs_groupmember(zfsvfs_t *zfsvfs, uint64_t id, cred_t *cr)
718 {
719 	uid_t		gid;
720 
721 #ifdef illumos
722 	ksid_t		*ksid = crgetsid(cr, KSID_GROUP);
723 	ksidlist_t	*ksidlist = crgetsidlist(cr);
724 
725 	if (ksid && ksidlist) {
726 		int		i;
727 		ksid_t		*ksid_groups;
728 		uint32_t	idx = FUID_INDEX(id);
729 		uint32_t	rid = FUID_RID(id);
730 
731 		ksid_groups = ksidlist->ksl_sids;
732 
733 		for (i = 0; i != ksidlist->ksl_nsid; i++) {
734 			if (idx == 0) {
735 				if (id != IDMAP_WK_CREATOR_GROUP_GID &&
736 				    id == ksid_groups[i].ks_id) {
737 					return (B_TRUE);
738 				}
739 			} else {
740 				const char *domain;
741 
742 				domain = zfs_fuid_find_by_idx(zfsvfs, idx);
743 				ASSERT(domain != NULL);
744 
745 				if (strcmp(domain,
746 				    IDMAP_WK_CREATOR_SID_AUTHORITY) == 0)
747 					return (B_FALSE);
748 
749 				if ((strcmp(domain,
750 				    ksid_groups[i].ks_domain->kd_name) == 0) &&
751 				    rid == ksid_groups[i].ks_rid)
752 					return (B_TRUE);
753 			}
754 		}
755 	}
756 #endif /* illumos */
757 
758 	/*
759 	 * Not found in ksidlist, check posix groups
760 	 */
761 	gid = zfs_fuid_map_id(zfsvfs, id, cr, ZFS_GROUP);
762 	return (groupmember(gid, cr));
763 }
764 
765 void
zfs_fuid_txhold(zfsvfs_t * zfsvfs,dmu_tx_t * tx)766 zfs_fuid_txhold(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
767 {
768 	if (zfsvfs->z_fuid_obj == 0) {
769 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
770 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
771 		    FUID_SIZE_ESTIMATE(zfsvfs));
772 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
773 	} else {
774 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
775 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
776 		    FUID_SIZE_ESTIMATE(zfsvfs));
777 	}
778 }
779 
780 /*
781  * buf must be big enough (eg, 32 bytes)
782  */
783 int
zfs_id_to_fuidstr(zfsvfs_t * zfsvfs,const char * domain,uid_t rid,char * buf,size_t len,boolean_t addok)784 zfs_id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
785     char *buf, size_t len, boolean_t addok)
786 {
787 	uint64_t fuid;
788 	int domainid = 0;
789 
790 	if (domain && domain[0]) {
791 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
792 		if (domainid == -1)
793 			return (SET_ERROR(ENOENT));
794 	}
795 	fuid = FUID_ENCODE(domainid, rid);
796 	(void) snprintf(buf, len, "%llx", (longlong_t)fuid);
797 	return (0);
798 }
799 #endif
800