2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * hfs_attrlist.c - HFS attribute list processing
32 * Copyright (c) 1998-2002, Apple Inc. All Rights Reserved.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
41 #include <sys/unistd.h>
43 #include <sys/kauth.h>
44 #include <sys/fsctl.h>
46 #include <kern/locks.h>
49 #include "hfs_cnode.h"
50 #include "hfs_mount.h"
52 #include "hfs_attrlist.h"
53 #include "hfs_btreeio.h"
54 #include "hfs_cprotect.h"
56 /* Packing routines: */
58 static void packnameattr(struct attrblock
*abp
, struct vnode
*vp
,
59 const u_int8_t
*name
, int namelen
);
61 static void packcommonattr(struct attrblock
*abp
, struct hfsmount
*hfsmp
,
62 struct vnode
*vp
, struct cat_desc
* cdp
,
63 struct cat_attr
* cap
, struct vfs_context
*ctx
);
65 static void packfileattr(struct attrblock
*abp
, struct hfsmount
*hfsmp
,
66 struct cat_attr
*cattrp
, struct cat_fork
*datafork
,
67 struct cat_fork
*rsrcfork
, struct vnode
*vp
);
69 static void packdirattr(struct attrblock
*abp
, struct hfsmount
*hfsmp
,
70 struct vnode
*vp
, struct cat_desc
* descp
,
71 struct cat_attr
* cattrp
);
73 static u_int32_t
hfs_real_user_access(vnode_t vp
, vfs_context_t ctx
);
75 static void get_vattr_data_for_attrs(struct attrlist
*, struct vnode_attr
*,
76 struct hfsmount
*, struct vnode
*, struct cat_desc
*, struct cat_attr
*,
77 struct cat_fork
*, struct cat_fork
*, vfs_context_t
);
79 static void vattr_data_for_common_attrs(struct attrlist
*, struct vnode_attr
*,
80 struct hfsmount
*, struct vnode
*, struct cat_desc
*, struct cat_attr
*,
83 static void vattr_data_for_dir_attrs(struct attrlist
*, struct vnode_attr
*,
84 struct hfsmount
*, struct vnode
*, struct cat_desc
*, struct cat_attr
*);
86 static void vattr_data_for_file_attrs(struct attrlist
*, struct vnode_attr
*,
87 struct hfsmount
*, struct cat_attr
*, struct cat_fork
*, struct cat_fork
*,
90 static int hfs_readdirattr_internal(struct vnode
*, struct attrlist
*,
91 struct vnode_attr
*, uio_t
, uint64_t, int, uint32_t *, int *, int *,
95 * readdirattr operation will return attributes for the items in the
96 * directory specified.
98 * It does not do . and .. entries. The problem is if you are at the root of the
99 * hfs directory and go to .. you could be crossing a mountpoint into a
100 * different (ufs) file system. The attributes that apply for it may not
101 * apply for the file system you are doing the readdirattr on. To make life
102 * simpler, this call will only return entries in its directory, hfs like.
105 hfs_vnop_readdirattr(ap
)
106 struct vnop_readdirattr_args
/* {
108 struct attrlist *a_alist;
114 u_long *a_actualcount;
115 vfs_context_t a_context;
119 struct attrlist
*alist
= ap
->a_alist
;
121 /* Check for invalid options and buffer space. */
122 if (((ap
->a_options
& ~(FSOPT_NOINMEMUPDATE
| FSOPT_NOFOLLOW
)) != 0) ||
123 (ap
->a_maxcount
<= 0)) {
127 * Reject requests for unsupported attributes.
129 if ((alist
->bitmapcount
!= ATTR_BIT_MAP_COUNT
) ||
130 (alist
->commonattr
& ~HFS_ATTR_CMN_VALID
) ||
131 (alist
->volattr
!= 0) ||
132 (alist
->dirattr
& ~HFS_ATTR_DIR_VALID
) ||
133 (alist
->fileattr
& ~HFS_ATTR_FILE_VALID
) ||
134 (alist
->forkattr
!= 0)) {
138 error
= hfs_readdirattr_internal(ap
->a_vp
, alist
, NULL
, ap
->a_uio
,
139 (uint64_t)ap
->a_options
, ap
->a_maxcount
, ap
->a_newstate
,
140 ap
->a_eofflag
, (int *)ap
->a_actualcount
, ap
->a_context
);
147 * getattrlistbulk, like readdirattr, will return attributes for the items in
148 * the directory specified.
150 * It does not do . and .. entries. The problem is if you are at the root of the
151 * hfs directory and go to .. you could be crossing a mountpoint into a
152 * different (ufs) file system. The attributes that apply for it may not
153 * apply for the file system you are doing the readdirattr on. To make life
154 * simpler, this call will only return entries in its directory, hfs like.
157 hfs_vnop_getattrlistbulk(ap
)
158 struct vnop_getattrlistbulk_args
/* {
159 struct vnodeop_desc *a_desc;
161 struct attrlist *a_alist;
162 struct vnode_attr *a_vap;
167 int32_t *a_actualcount;
168 vfs_context_t a_context;
173 error
= hfs_readdirattr_internal(ap
->a_vp
, ap
->a_alist
, ap
->a_vap
,
174 ap
->a_uio
, (uint64_t)ap
->a_options
, 0, NULL
, ap
->a_eofflag
,
175 (int *)ap
->a_actualcount
, ap
->a_context
);
181 * Common function for both hfs_vnop_readdirattr and hfs_vnop_getattrlistbulk.
182 * This either fills in a vnode_attr structure or fills in an attrbute buffer
183 * Currently the difference in behaviour required for the two vnops is keyed
184 * on whether the passed in vnode_attr pointer is null or not. If the pointer
185 * is null we fill in buffer passed and if it is not null we fill in the fields
186 * of the vnode_attr structure.
189 hfs_readdirattr_internal(struct vnode
*dvp
, struct attrlist
*alist
,
190 struct vnode_attr
*vap
, uio_t uio
, uint64_t options
, int maxcount
,
191 uint32_t *newstate
, int *eofflag
, int *actualcount
, vfs_context_t ctx
)
194 struct hfsmount
* hfsmp
;
195 u_int32_t fixedblocksize
;
196 u_int32_t maxattrblocksize
= 0;
197 u_int32_t currattrbufsize
;
198 void *attrbufptr
= NULL
;
199 void *attrptr
= NULL
;
201 caddr_t namebuf
= NULL
;
202 struct attrblock attrblk
;
206 struct cat_desc
*lastdescp
= NULL
;
207 struct cat_entrylist
*ce_list
= NULL
;
208 directoryhint_t
*dirhint
= NULL
;
212 u_int32_t dirchg
= 0;
218 if ((uio_resid(uio
) <= 0) || (uio_iovcnt(uio
) > 1))
221 if (VTOC(dvp
)->c_bsdflags
& UF_COMPRESSED
) {
222 int compressed
= hfs_file_is_compressed(VTOC(dvp
), 0); /* 0 == take the cnode lock */
225 error
= check_for_dataless_file(dvp
, NAMESPACE_HANDLER_READ_OP
);
233 * Take an exclusive directory lock since we manipulate the directory hints
235 if ((error
= hfs_lock(VTOC(dvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
241 dirchg
= dcp
->c_dirchangecnt
;
243 /* Extract directory index and tag (sequence number) from uio_offset */
244 index
= uio_offset(uio
) & HFS_INDEX_MASK
;
245 tag
= uio_offset(uio
) & ~HFS_INDEX_MASK
;
248 * We can't just use the valence as an optimization to avoid
249 * going to the catalog. It might be wrong (== 0), and that would
250 * cause us to avoid iterating the directory when it might actually have
251 * contents. Instead, use the catalog to tell us when we've hit EOF
255 /* Get a buffer to hold packed attributes. */
256 fixedblocksize
= (sizeof(u_int32_t
) + hfs_attrblksize(alist
)); /* 4 bytes for length */
259 maxattrblocksize
= fixedblocksize
;
260 if (alist
->commonattr
& ATTR_CMN_NAME
)
261 maxattrblocksize
+= kHFSPlusMaxFileNameBytes
+ 1;
263 attrbufptr
= hfs_malloc(maxattrblocksize
);
264 attrptr
= attrbufptr
;
265 varptr
= (char *)attrbufptr
+ fixedblocksize
; /* Point to variable-length storage */
267 if ((alist
->commonattr
& ATTR_CMN_NAME
) && !vap
->va_name
) {
268 namebuf
= hfs_malloc(MAXPATHLEN
);
273 vap
->va_name
= namebuf
;
276 /* Get a detached directory hint (cnode must be locked exclusive) */
277 dirhint
= hfs_getdirhint(dcp
, ((index
- 1) & HFS_INDEX_MASK
) | tag
, TRUE
);
279 /* Hide tag from catalog layer. */
280 dirhint
->dh_index
&= HFS_INDEX_MASK
;
281 if (dirhint
->dh_index
== HFS_INDEX_MASK
) {
282 dirhint
->dh_index
= -1;
286 * Obtain a list of catalog entries and pack their attributes until
287 * the output buffer is full or maxcount entries have been packed.
291 * Constrain our list size.
293 maxentries
= uio_resid(uio
) / (fixedblocksize
+ HFS_AVERAGE_NAME_SIZE
);
294 /* There is maxcount for the bulk vnop */
296 maxentries
= min(maxentries
, maxcount
);
297 maxentries
= min(maxentries
, MAXCATENTRIES
);
298 if (maxentries
< 1) {
303 /* Initialize a catalog entry list. */
304 ce_list
= hfs_mallocz(CE_LIST_SIZE(maxentries
));
305 ce_list
->maxentries
= maxentries
;
308 * Populate the ce_list from the catalog file.
310 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
312 error
= cat_getentriesattr(hfsmp
, dirhint
, ce_list
, &reachedeof
);
313 /* Don't forget to release the descriptors later! */
315 hfs_systemfile_unlock(hfsmp
, lockflags
);
317 if ((error
== ENOENT
) || (reachedeof
!= 0)) {
325 dcp
->c_touch_acctime
= TRUE
;
328 * Check for a FS corruption in the valence. We're holding the cnode lock
329 * exclusive since we need to serialize the directory hints, so if we found
330 * that the valence reported 0, but we actually found some items here, then
331 * silently minimally self-heal and bump the valence to 1.
333 if ((dcp
->c_entries
== 0) && (ce_list
->realentries
> 0)) {
335 dcp
->c_flag
|= C_MODIFIED
;
336 printf("%s : repairing valence to non-zero!\n", __FUNCTION__
);
337 /* force an update on dcp while we're still holding the lock. */
342 * Drop the directory lock so we don't deadlock when we:
343 * - acquire a child cnode lock
344 * - make calls to vnode_authorize()
345 * - make calls to kauth_cred_ismember_gid()
350 /* Process the catalog entries. */
351 for (i
= 0; i
< (int)ce_list
->realentries
; ++i
) {
352 struct cnode
*cp
= NULL
;
353 struct vnode
*vp
= NULL
;
354 struct cat_desc
* cdescp
;
355 struct cat_attr
* cattrp
;
356 struct cat_fork c_datafork
;
357 struct cat_fork c_rsrcfork
;
359 bzero(&c_datafork
, sizeof(c_datafork
));
360 bzero(&c_rsrcfork
, sizeof(c_rsrcfork
));
361 cdescp
= &ce_list
->entry
[i
].ce_desc
;
362 cattrp
= &ce_list
->entry
[i
].ce_attr
;
363 c_datafork
.cf_size
= ce_list
->entry
[i
].ce_datasize
;
364 c_datafork
.cf_blocks
= ce_list
->entry
[i
].ce_datablks
;
365 c_rsrcfork
.cf_size
= ce_list
->entry
[i
].ce_rsrcsize
;
366 c_rsrcfork
.cf_blocks
= ce_list
->entry
[i
].ce_rsrcblks
;
368 if (((alist
->commonattr
& ATTR_CMN_USERACCESS
) &&
369 (cattrp
->ca_recflags
& kHFSHasSecurityMask
))
372 ((alist
->commonattr
& ATTR_CMN_DATA_PROTECT_FLAGS
) && (vap
))
376 * Obtain vnode for our vnode_authorize() calls.
378 if (hfs_vget(hfsmp
, cattrp
->ca_fileid
, &vp
, 0, 0) != 0) {
381 } else if (vap
|| !(options
& FSOPT_NOINMEMUPDATE
)) {
382 /* Get in-memory cnode data (if any). */
383 vp
= hfs_chash_getvnode(hfsmp
, cattrp
->ca_fileid
, 0, 0, 0);
387 /* Only use cnode's decriptor for non-hardlinks */
388 if (!(cp
->c_flag
& C_HARDLINK
))
389 cdescp
= &cp
->c_desc
;
390 cattrp
= &cp
->c_attr
;
391 if (cp
->c_datafork
) {
392 c_datafork
.cf_size
= cp
->c_datafork
->ff_size
;
393 c_datafork
.cf_blocks
= cp
->c_datafork
->ff_blocks
;
395 if (cp
->c_rsrcfork
) {
396 c_rsrcfork
.cf_size
= cp
->c_rsrcfork
->ff_size
;
397 c_rsrcfork
.cf_blocks
= cp
->c_rsrcfork
->ff_blocks
;
399 /* All done with cnode. */
405 *((u_int32_t
*)attrptr
) = 0;
406 attrptr
= ((u_int32_t
*)attrptr
) + 1;
407 attrblk
.ab_attrlist
= alist
;
408 attrblk
.ab_attrbufpp
= &attrptr
;
409 attrblk
.ab_varbufpp
= &varptr
;
410 attrblk
.ab_flags
= 0;
411 attrblk
.ab_blocksize
= maxattrblocksize
;
412 attrblk
.ab_context
= ctx
;
414 /* Pack catalog entries into attribute buffer. */
415 hfs_packattrblk(&attrblk
, hfsmp
, vp
, cdescp
, cattrp
, &c_datafork
, &c_rsrcfork
, ctx
);
416 currattrbufsize
= ((char *)varptr
- (char *)attrbufptr
);
418 /* All done with vnode. */
424 /* Make sure there's enough buffer space remaining. */
425 // LP64todo - fix this!
426 if (uio_resid(uio
) < 0 ||
427 currattrbufsize
> (u_int32_t
)uio_resid(uio
)) {
430 *((u_int32_t
*)attrbufptr
) = currattrbufsize
;
431 error
= uiomove((caddr_t
)attrbufptr
, currattrbufsize
, uio
);
432 if (error
!= E_NONE
) {
435 attrptr
= attrbufptr
;
436 /* Point to variable-length storage */
437 varptr
= (char *)attrbufptr
+ fixedblocksize
;
438 /* Save the last valid catalog entry */
439 lastdescp
= &ce_list
->entry
[i
].ce_desc
;
443 /* Termination checks */
444 if ((--maxcount
<= 0) ||
445 // LP64todo - fix this!
446 uio_resid(uio
) < 0 ||
447 ((u_int32_t
)uio_resid(uio
) < (fixedblocksize
+ HFS_AVERAGE_NAME_SIZE
))){
452 size_t orig_resid
= (size_t)uio_resid(uio
);
455 get_vattr_data_for_attrs(alist
, vap
, hfsmp
, vp
, cdescp
,
456 cattrp
, &c_datafork
, &c_rsrcfork
, ctx
);
459 if ((alist
->commonattr
& ATTR_CMN_DATA_PROTECT_FLAGS
) &&
461 cp_key_class_t
class;
463 if (!cp_vnode_getclass(vp
, &class)) {
464 VATTR_RETURN(vap
, va_dataprotect_class
,
469 error
= vfs_attr_pack(vp
, uio
, alist
, options
, vap
,
472 /* All done with vnode. */
478 resid
= uio_resid(uio
);
480 /* Was this entry succesful ? */
481 if (error
|| resid
== orig_resid
)
484 /* Save the last valid catalog entry */
485 lastdescp
= &ce_list
->entry
[i
].ce_desc
;
489 /* Do we have the bare minimum for the next entry ? */
490 if (resid
< sizeof(uint32_t))
493 } /* for each catalog entry */
496 * If we couldn't fit all the entries requested in the user's buffer,
499 if (*eofflag
&& (*actualcount
< (int)ce_list
->realentries
))
502 /* If we skipped catalog entries for reserved files that should
503 * not be listed in namespace, update the index accordingly.
505 if (ce_list
->skipentries
) {
506 index
+= ce_list
->skipentries
;
507 ce_list
->skipentries
= 0;
511 * If there are more entries then save the last name.
512 * Key this behavior based on whether or not we observed EOFFLAG.
514 * Do not use the valence as a way to determine if we hit EOF, since
515 * it can be wrong. Use the catalog's output only.
517 if ((*(eofflag
) == 0) && (lastdescp
!= NULL
) && (lastdescp
->cd_nameptr
!= NULL
)) {
519 /* Remember last entry */
520 if ((dirhint
->dh_desc
.cd_flags
& CD_HASBUF
) &&
521 (dirhint
->dh_desc
.cd_nameptr
!= NULL
)) {
522 dirhint
->dh_desc
.cd_flags
&= ~CD_HASBUF
;
523 vfs_removename((const char *)dirhint
->dh_desc
.cd_nameptr
);
525 dirhint
->dh_desc
.cd_namelen
= lastdescp
->cd_namelen
;
526 dirhint
->dh_desc
.cd_nameptr
= (const u_int8_t
*)
527 vfs_addname((const char *)lastdescp
->cd_nameptr
, lastdescp
->cd_namelen
, 0, 0);
528 dirhint
->dh_desc
.cd_flags
|= CD_HASBUF
;
529 dirhint
->dh_index
= index
- 1;
530 dirhint
->dh_desc
.cd_cnid
= lastdescp
->cd_cnid
;
531 dirhint
->dh_desc
.cd_hint
= lastdescp
->cd_hint
;
532 dirhint
->dh_desc
.cd_encoding
= lastdescp
->cd_encoding
;
535 /* All done with the catalog descriptors. */
536 for (i
= 0; i
< (int)ce_list
->realentries
; ++i
)
537 cat_releasedesc(&ce_list
->entry
[i
].ce_desc
);
538 ce_list
->realentries
= 0;
540 (void) hfs_lock(VTOC(dvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
544 /* Pack directory index and tag into uio_offset. */
545 while (tag
== 0) tag
= (++dcp
->c_dirhinttag
) << HFS_INDEX_BITS
;
546 uio_setoffset(uio
, index
| tag
);
547 dirhint
->dh_index
|= tag
;
554 * Drop directory hint on error or if there are no more entries,
555 * only if EOF was seen.
558 if ((error
!= 0) || *(eofflag
))
559 hfs_reldirhint(dcp
, dirhint
);
561 hfs_insertdirhint(dcp
, dirhint
);
564 hfs_free(namebuf
, MAXPATHLEN
);
568 hfs_free(attrbufptr
, maxattrblocksize
);
570 hfs_free(ce_list
, CE_LIST_SIZE(maxentries
));
572 if (vap
&& *actualcount
&& error
)
580 /*==================== Attribute list support routines ====================*/
583 * Pack cnode attributes into an attribute block.
586 hfs_packattrblk(struct attrblock
*abp
,
587 struct hfsmount
*hfsmp
,
589 struct cat_desc
*descp
,
590 struct cat_attr
*attrp
,
591 struct cat_fork
*datafork
,
592 struct cat_fork
*rsrcfork
,
593 struct vfs_context
*ctx
)
595 struct attrlist
*attrlistp
= abp
->ab_attrlist
;
597 if (attrlistp
->commonattr
)
598 packcommonattr(abp
, hfsmp
, vp
, descp
, attrp
, ctx
);
600 if (attrlistp
->dirattr
&& S_ISDIR(attrp
->ca_mode
))
601 packdirattr(abp
, hfsmp
, vp
, descp
,attrp
);
603 if (attrlistp
->fileattr
&& !S_ISDIR(attrp
->ca_mode
))
604 packfileattr(abp
, hfsmp
, attrp
, datafork
, rsrcfork
, vp
);
608 mountpointname(struct mount
*mp
)
610 struct vfsstatfs
*vsfs
= vfs_statfs(mp
);
612 size_t namelength
= strlen(vsfs
->f_mntonname
);
620 * Look backwards through the name string, looking for
621 * the first slash encountered (which must precede the
622 * last part of the pathname).
624 for (c
= vsfs
->f_mntonname
+ namelength
- 1;
625 namelength
> 0; --c
, --namelength
) {
628 } else if (foundchars
) {
633 return vsfs
->f_mntonname
;
639 struct attrblock
*abp
,
641 const u_int8_t
*name
,
645 struct attrreference
* attr_refptr
;
648 u_int32_t attrlength
;
651 /* A cnode's name may be incorrect for the root of a mounted
652 * filesystem (it can be mounted on a different directory name
653 * than the name of the volume, such as "blah-1"). So for the
654 * root directory, it's best to return the last element of the
655 location where the volume's mounted:
657 if ((vp
!= NULL
) && vnode_isvroot(vp
) &&
658 (mpname
= mountpointname(vnode_mount(vp
)))) {
659 mpnamelen
= strlen(mpname
);
661 /* Trim off any trailing slashes: */
662 while ((mpnamelen
> 0) && (mpname
[mpnamelen
-1] == '/'))
665 /* If there's anything left, use it instead of the volume's name */
667 name
= (u_int8_t
*)mpname
;
676 varbufptr
= *abp
->ab_varbufpp
;
677 attr_refptr
= (struct attrreference
*)(*abp
->ab_attrbufpp
);
679 attrlength
= namelen
+ 1;
680 attr_refptr
->attr_dataoffset
= (char *)varbufptr
- (char *)attr_refptr
;
681 attr_refptr
->attr_length
= attrlength
;
682 (void) strncpy((char *)varbufptr
, (const char *) name
, attrlength
);
684 * Advance beyond the space just allocated and
685 * round up to the next 4-byte boundary:
687 varbufptr
= ((char *)varbufptr
) + attrlength
+ ((4 - (attrlength
& 3)) & 3);
690 *abp
->ab_attrbufpp
= attr_refptr
;
691 *abp
->ab_varbufpp
= varbufptr
;
696 struct attrblock
*abp
,
697 struct hfsmount
*hfsmp
,
699 struct cat_desc
* cdp
,
700 struct cat_attr
* cap
,
701 struct vfs_context
* ctx
)
703 attrgroup_t attr
= abp
->ab_attrlist
->commonattr
;
704 struct mount
*mp
= HFSTOVFS(hfsmp
);
705 void *attrbufptr
= *abp
->ab_attrbufpp
;
706 void *varbufptr
= *abp
->ab_varbufpp
;
707 boolean_t is_64_bit
= proc_is64bit(vfs_context_proc(ctx
));
711 if (attr
& (ATTR_CMN_OWNERID
| ATTR_CMN_GRPID
)) {
712 cuid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
716 if (ATTR_CMN_NAME
& attr
) {
717 packnameattr(abp
, vp
, cdp
->cd_nameptr
, cdp
->cd_namelen
);
718 attrbufptr
= *abp
->ab_attrbufpp
;
719 varbufptr
= *abp
->ab_varbufpp
;
721 if (ATTR_CMN_DEVID
& attr
) {
722 *((dev_t
*)attrbufptr
) = hfsmp
->hfs_raw_dev
;
723 attrbufptr
= ((dev_t
*)attrbufptr
) + 1;
725 if (ATTR_CMN_FSID
& attr
) {
728 fsid
.val
[0] = hfsmp
->hfs_raw_dev
;
729 fsid
.val
[1] = vfs_typenum(mp
);
730 *((fsid_t
*)attrbufptr
) = fsid
;
731 attrbufptr
= ((fsid_t
*)attrbufptr
) + 1;
733 if (ATTR_CMN_OBJTYPE
& attr
) {
734 *((fsobj_type_t
*)attrbufptr
) = IFTOVT(cap
->ca_mode
);
735 attrbufptr
= ((fsobj_type_t
*)attrbufptr
) + 1;
737 if (ATTR_CMN_OBJTAG
& attr
) {
738 *((fsobj_tag_t
*)attrbufptr
) = VT_HFS
;
739 attrbufptr
= ((fsobj_tag_t
*)attrbufptr
) + 1;
742 * Exporting file IDs from HFS Plus:
744 * For "normal" files the c_fileid is the same value as the
745 * c_cnid. But for hard link files, they are different - the
746 * c_cnid belongs to the active directory entry (ie the link)
747 * and the c_fileid is for the actual inode (ie the data file).
749 * The stat call (getattr) will always return the c_fileid
750 * and Carbon APIs, which are hardlink-ignorant, will always
751 * receive the c_cnid (from getattrlist).
753 if (ATTR_CMN_OBJID
& attr
) {
754 ((fsobj_id_t
*)attrbufptr
)->fid_objno
= cdp
->cd_cnid
;
755 ((fsobj_id_t
*)attrbufptr
)->fid_generation
= 0;
756 attrbufptr
= ((fsobj_id_t
*)attrbufptr
) + 1;
758 if (ATTR_CMN_OBJPERMANENTID
& attr
) {
759 ((fsobj_id_t
*)attrbufptr
)->fid_objno
= cdp
->cd_cnid
;
760 ((fsobj_id_t
*)attrbufptr
)->fid_generation
= 0;
761 attrbufptr
= ((fsobj_id_t
*)attrbufptr
) + 1;
763 if (ATTR_CMN_PAROBJID
& attr
) {
764 ((fsobj_id_t
*)attrbufptr
)->fid_objno
= cdp
->cd_parentcnid
;
765 ((fsobj_id_t
*)attrbufptr
)->fid_generation
= 0;
766 attrbufptr
= ((fsobj_id_t
*)attrbufptr
) + 1;
768 if (ATTR_CMN_SCRIPT
& attr
) {
769 *((text_encoding_t
*)attrbufptr
) = cdp
->cd_encoding
;
770 attrbufptr
= ((text_encoding_t
*)attrbufptr
) + 1;
772 if (ATTR_CMN_CRTIME
& attr
) {
774 ((struct user64_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_itime
;
775 ((struct user64_timespec
*)attrbufptr
)->tv_nsec
= 0;
776 attrbufptr
= ((struct user64_timespec
*)attrbufptr
) + 1;
779 ((struct user32_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_itime
;
780 ((struct user32_timespec
*)attrbufptr
)->tv_nsec
= 0;
781 attrbufptr
= ((struct user32_timespec
*)attrbufptr
) + 1;
784 if (ATTR_CMN_MODTIME
& attr
) {
786 ((struct user64_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_mtime
;
787 ((struct user64_timespec
*)attrbufptr
)->tv_nsec
= 0;
788 attrbufptr
= ((struct user64_timespec
*)attrbufptr
) + 1;
791 ((struct user32_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_mtime
;
792 ((struct user32_timespec
*)attrbufptr
)->tv_nsec
= 0;
793 attrbufptr
= ((struct user32_timespec
*)attrbufptr
) + 1;
796 if (ATTR_CMN_CHGTIME
& attr
) {
798 ((struct user64_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_ctime
;
799 ((struct user64_timespec
*)attrbufptr
)->tv_nsec
= 0;
800 attrbufptr
= ((struct user64_timespec
*)attrbufptr
) + 1;
803 ((struct user32_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_ctime
;
804 ((struct user32_timespec
*)attrbufptr
)->tv_nsec
= 0;
805 attrbufptr
= ((struct user32_timespec
*)attrbufptr
) + 1;
808 if (ATTR_CMN_ACCTIME
& attr
) {
810 ((struct user64_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_atime
;
811 ((struct user64_timespec
*)attrbufptr
)->tv_nsec
= 0;
812 attrbufptr
= ((struct user64_timespec
*)attrbufptr
) + 1;
815 ((struct user32_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_atime
;
816 ((struct user32_timespec
*)attrbufptr
)->tv_nsec
= 0;
817 attrbufptr
= ((struct user32_timespec
*)attrbufptr
) + 1;
820 if (ATTR_CMN_BKUPTIME
& attr
) {
822 ((struct user64_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_btime
;
823 ((struct user64_timespec
*)attrbufptr
)->tv_nsec
= 0;
824 attrbufptr
= ((struct user64_timespec
*)attrbufptr
) + 1;
827 ((struct user32_timespec
*)attrbufptr
)->tv_sec
= cap
->ca_btime
;
828 ((struct user32_timespec
*)attrbufptr
)->tv_nsec
= 0;
829 attrbufptr
= ((struct user32_timespec
*)attrbufptr
) + 1;
832 if (ATTR_CMN_FNDRINFO
& attr
) {
833 u_int8_t
*finfo
= NULL
;
834 bcopy(&cap
->ca_finderinfo
, attrbufptr
, sizeof(u_int8_t
) * 32);
835 finfo
= (u_int8_t
*)attrbufptr
;
837 /* Don't expose a symlink's private type/creator. */
838 if (S_ISLNK(cap
->ca_mode
)) {
839 struct FndrFileInfo
*fip
;
841 fip
= (struct FndrFileInfo
*)attrbufptr
;
846 /* advance 16 bytes into the attrbuf */
849 /* also don't expose the date_added or write_gen_counter fields */
850 if (S_ISREG(cap
->ca_mode
) || S_ISLNK(cap
->ca_mode
)) {
851 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
852 extinfo
->document_id
= 0;
853 extinfo
->date_added
= 0;
854 extinfo
->write_gen_counter
= 0;
856 else if (S_ISDIR(cap
->ca_mode
)) {
857 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
858 extinfo
->document_id
= 0;
859 extinfo
->date_added
= 0;
860 extinfo
->write_gen_counter
= 0;
863 attrbufptr
= (char *)attrbufptr
+ sizeof(u_int8_t
) * 32;
865 if (ATTR_CMN_OWNERID
& attr
) {
866 uid_t nuid
= cap
->ca_uid
;
869 if (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
)
871 else if (nuid
== UNKNOWNUID
)
875 *((uid_t
*)attrbufptr
) = nuid
;
876 attrbufptr
= ((uid_t
*)attrbufptr
) + 1;
878 if (ATTR_CMN_GRPID
& attr
) {
879 gid_t ngid
= cap
->ca_gid
;
882 gid_t cgid
= kauth_cred_getgid(vfs_context_ucred(ctx
));
883 if (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
)
885 else if (ngid
== UNKNOWNUID
)
889 *((gid_t
*)attrbufptr
) = ngid
;
890 attrbufptr
= ((gid_t
*)attrbufptr
) + 1;
892 if (ATTR_CMN_ACCESSMASK
& attr
) {
894 * [2856576] Since we are dynamically changing the owner, also
895 * effectively turn off the set-user-id and set-group-id bits,
896 * just like chmod(2) would when changing ownership. This prevents
897 * a security hole where set-user-id programs run as whoever is
898 * logged on (or root if nobody is logged in yet!)
900 *((u_int32_t
*)attrbufptr
) = (cap
->ca_uid
== UNKNOWNUID
) ?
901 cap
->ca_mode
& ~(S_ISUID
| S_ISGID
) : cap
->ca_mode
;
902 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
904 if (ATTR_CMN_FLAGS
& attr
) {
905 *((u_int32_t
*)attrbufptr
) = cap
->ca_flags
;
906 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
908 if (ATTR_CMN_USERACCESS
& attr
) {
909 u_int32_t user_access
;
911 /* Take the long path when we have an ACL */
912 if ((vp
!= NULLVP
) && (cap
->ca_recflags
& kHFSHasSecurityMask
)) {
913 user_access
= hfs_real_user_access(vp
, abp
->ab_context
);
915 user_access
= DerivePermissionSummary(cap
->ca_uid
, cap
->ca_gid
,
916 cap
->ca_mode
, mp
, vfs_context_ucred(ctx
), 0);
918 /* Also consider READ-ONLY file system. */
919 if (vfs_flags(mp
) & MNT_RDONLY
) {
920 user_access
&= ~W_OK
;
922 /* Locked objects are not writable either */
923 if ((cap
->ca_flags
& UF_IMMUTABLE
) && (vfs_context_suser(abp
->ab_context
) != 0))
924 user_access
&= ~W_OK
;
925 if ((cap
->ca_flags
& SF_IMMUTABLE
) && (vfs_context_suser(abp
->ab_context
) == 0))
926 user_access
&= ~W_OK
;
928 *((u_int32_t
*)attrbufptr
) = user_access
;
929 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
931 if (ATTR_CMN_FILEID
& attr
) {
932 *((u_int64_t
*)attrbufptr
) = cap
->ca_fileid
;
933 attrbufptr
= ((u_int64_t
*)attrbufptr
) + 1;
935 if (ATTR_CMN_PARENTID
& attr
) {
936 *((u_int64_t
*)attrbufptr
) = cdp
->cd_parentcnid
;
937 attrbufptr
= ((u_int64_t
*)attrbufptr
) + 1;
940 *abp
->ab_attrbufpp
= attrbufptr
;
941 *abp
->ab_varbufpp
= varbufptr
;
946 struct attrblock
*abp
,
947 struct hfsmount
*hfsmp
,
949 struct cat_desc
* descp
,
950 struct cat_attr
* cattrp
)
952 attrgroup_t attr
= abp
->ab_attrlist
->dirattr
;
953 void *attrbufptr
= *abp
->ab_attrbufpp
;
957 * The DIR_LINKCOUNT is the count of real directory hard links.
958 * (i.e. its not the sum of the implied "." and ".." references
959 * typically used in stat's st_nlink field)
961 if (ATTR_DIR_LINKCOUNT
& attr
) {
962 *((u_int32_t
*)attrbufptr
) = cattrp
->ca_linkcount
;
963 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
965 if (ATTR_DIR_ENTRYCOUNT
& attr
) {
966 entries
= cattrp
->ca_entries
;
968 if (descp
->cd_parentcnid
== kHFSRootParentID
) {
969 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0)
970 --entries
; /* hide private dir */
971 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0)
972 --entries
; /* hide private dir */
974 ((hfsmp
->vcbAtrb
& kHFSVolumeJournaledMask
) &&
975 (hfsmp
->hfs_flags
& HFS_READ_ONLY
)))
976 entries
-= 2; /* hide the journal files */
979 *((u_int32_t
*)attrbufptr
) = entries
;
980 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
982 if (ATTR_DIR_MOUNTSTATUS
& attr
) {
983 if (vp
!= NULL
&& vnode_mountedhere(vp
) != NULL
)
984 *((u_int32_t
*)attrbufptr
) = DIR_MNTSTATUS_MNTPOINT
;
986 *((u_int32_t
*)attrbufptr
) = 0;
987 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
989 *abp
->ab_attrbufpp
= attrbufptr
;
994 struct attrblock
*abp
,
995 struct hfsmount
*hfsmp
,
996 struct cat_attr
*cattrp
,
997 struct cat_fork
*datafork
,
998 struct cat_fork
*rsrcfork
,
1001 #if !HFS_COMPRESSION
1004 attrgroup_t attr
= abp
->ab_attrlist
->fileattr
;
1005 void *attrbufptr
= *abp
->ab_attrbufpp
;
1006 void *varbufptr
= *abp
->ab_varbufpp
;
1007 u_int32_t allocblksize
;
1009 allocblksize
= HFSTOVCB(hfsmp
)->blockSize
;
1011 off_t datasize
= datafork
->cf_size
;
1012 off_t totalsize
= datasize
+ rsrcfork
->cf_size
;
1014 int handle_compressed
;
1015 handle_compressed
= (cattrp
->ca_flags
& UF_COMPRESSED
);// && hfs_file_is_compressed(VTOC(vp), 1);
1017 if (handle_compressed
) {
1018 if (attr
& (ATTR_FILE_DATALENGTH
|ATTR_FILE_TOTALSIZE
)) {
1019 if ( 0 == hfs_uncompressed_size_of_compressed_file(hfsmp
, vp
, cattrp
->ca_fileid
, &datasize
, 1) ) { /* 1 == don't take the cnode lock */
1020 /* total size of a compressed file is just the data size */
1021 totalsize
= datasize
;
1027 if (ATTR_FILE_LINKCOUNT
& attr
) {
1028 *((u_int32_t
*)attrbufptr
) = cattrp
->ca_linkcount
;
1029 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
1031 if (ATTR_FILE_TOTALSIZE
& attr
) {
1032 *((off_t
*)attrbufptr
) = totalsize
;
1033 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1035 if (ATTR_FILE_ALLOCSIZE
& attr
) {
1036 *((off_t
*)attrbufptr
) =
1037 (off_t
)cattrp
->ca_blocks
* (off_t
)allocblksize
;
1038 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1040 if (ATTR_FILE_IOBLOCKSIZE
& attr
) {
1041 *((u_int32_t
*)attrbufptr
) = hfsmp
->hfs_logBlockSize
;
1042 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
1044 if (ATTR_FILE_CLUMPSIZE
& attr
) {
1045 *((u_int32_t
*)attrbufptr
) = hfsmp
->vcbClpSiz
;
1046 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
1048 if (ATTR_FILE_DEVTYPE
& attr
) {
1049 if (S_ISBLK(cattrp
->ca_mode
) || S_ISCHR(cattrp
->ca_mode
))
1050 *((u_int32_t
*)attrbufptr
) = (u_int32_t
)cattrp
->ca_rdev
;
1052 *((u_int32_t
*)attrbufptr
) = 0;
1053 attrbufptr
= ((u_int32_t
*)attrbufptr
) + 1;
1056 if (ATTR_FILE_DATALENGTH
& attr
) {
1057 *((off_t
*)attrbufptr
) = datasize
;
1058 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1062 /* fake the data fork size on a decmpfs compressed file to reflect the
1063 * uncompressed size. This ensures proper reading and copying of these files.
1064 * NOTE: we may need to get the vnode here because the vnode parameter
1065 * passed by hfs_vnop_readdirattr() may be null.
1068 if ( handle_compressed
) {
1069 if (attr
& ATTR_FILE_DATAALLOCSIZE
) {
1070 *((off_t
*)attrbufptr
) = (off_t
)rsrcfork
->cf_blocks
* (off_t
)allocblksize
;
1071 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1073 if (attr
& ATTR_FILE_RSRCLENGTH
) {
1074 *((off_t
*)attrbufptr
) = 0;
1075 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1077 if (attr
& ATTR_FILE_RSRCALLOCSIZE
) {
1078 *((off_t
*)attrbufptr
) = 0;
1079 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1085 if (ATTR_FILE_DATAALLOCSIZE
& attr
) {
1086 *((off_t
*)attrbufptr
) = (off_t
)datafork
->cf_blocks
* (off_t
)allocblksize
;
1087 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1089 if (ATTR_FILE_RSRCLENGTH
& attr
) {
1090 *((off_t
*)attrbufptr
) = rsrcfork
->cf_size
;
1091 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1093 if (ATTR_FILE_RSRCALLOCSIZE
& attr
) {
1094 *((off_t
*)attrbufptr
) = (off_t
)rsrcfork
->cf_blocks
* (off_t
)allocblksize
;
1095 attrbufptr
= ((off_t
*)attrbufptr
) + 1;
1098 *abp
->ab_attrbufpp
= attrbufptr
;
1099 *abp
->ab_varbufpp
= varbufptr
;
1103 * Calculate the total size of an attribute block.
1106 hfs_attrblksize(struct attrlist
*attrlist
)
1110 int sizeof_timespec
;
1111 boolean_t is_64_bit
= proc_is64bit(current_proc());
1114 sizeof_timespec
= sizeof(struct user64_timespec
);
1116 sizeof_timespec
= sizeof(struct user32_timespec
);
1118 hfs_assert((attrlist
->commonattr
& ~ATTR_CMN_VALIDMASK
) == 0);
1120 hfs_assert((attrlist
->volattr
& ~ATTR_VOL_VALIDMASK
) == 0);
1122 hfs_assert((attrlist
->dirattr
& ~ATTR_DIR_VALIDMASK
) == 0);
1124 hfs_assert((attrlist
->fileattr
& ~ATTR_FILE_VALIDMASK
) == 0);
1126 hfs_assert((attrlist
->forkattr
& ~ATTR_FORK_VALIDMASK
) == 0);
1130 if ((a
= attrlist
->commonattr
) != 0) {
1131 if (a
& ATTR_CMN_NAME
) size
+= sizeof(struct attrreference
);
1132 if (a
& ATTR_CMN_DEVID
) size
+= sizeof(dev_t
);
1133 if (a
& ATTR_CMN_FSID
) size
+= sizeof(fsid_t
);
1134 if (a
& ATTR_CMN_OBJTYPE
) size
+= sizeof(fsobj_type_t
);
1135 if (a
& ATTR_CMN_OBJTAG
) size
+= sizeof(fsobj_tag_t
);
1136 if (a
& ATTR_CMN_OBJID
) size
+= sizeof(fsobj_id_t
);
1137 if (a
& ATTR_CMN_OBJPERMANENTID
) size
+= sizeof(fsobj_id_t
);
1138 if (a
& ATTR_CMN_PAROBJID
) size
+= sizeof(fsobj_id_t
);
1139 if (a
& ATTR_CMN_SCRIPT
) size
+= sizeof(text_encoding_t
);
1140 if (a
& ATTR_CMN_CRTIME
) size
+= sizeof_timespec
;
1141 if (a
& ATTR_CMN_MODTIME
) size
+= sizeof_timespec
;
1142 if (a
& ATTR_CMN_CHGTIME
) size
+= sizeof_timespec
;
1143 if (a
& ATTR_CMN_ACCTIME
) size
+= sizeof_timespec
;
1144 if (a
& ATTR_CMN_BKUPTIME
) size
+= sizeof_timespec
;
1145 if (a
& ATTR_CMN_FNDRINFO
) size
+= 32 * sizeof(u_int8_t
);
1146 if (a
& ATTR_CMN_OWNERID
) size
+= sizeof(uid_t
);
1147 if (a
& ATTR_CMN_GRPID
) size
+= sizeof(gid_t
);
1148 if (a
& ATTR_CMN_ACCESSMASK
) size
+= sizeof(u_int32_t
);
1149 if (a
& ATTR_CMN_FLAGS
) size
+= sizeof(u_int32_t
);
1150 if (a
& ATTR_CMN_USERACCESS
) size
+= sizeof(u_int32_t
);
1151 if (a
& ATTR_CMN_FILEID
) size
+= sizeof(u_int64_t
);
1152 if (a
& ATTR_CMN_PARENTID
) size
+= sizeof(u_int64_t
);
1154 if ((a
= attrlist
->dirattr
) != 0) {
1155 if (a
& ATTR_DIR_LINKCOUNT
) size
+= sizeof(u_int32_t
);
1156 if (a
& ATTR_DIR_ENTRYCOUNT
) size
+= sizeof(u_int32_t
);
1157 if (a
& ATTR_DIR_MOUNTSTATUS
) size
+= sizeof(u_int32_t
);
1159 if ((a
= attrlist
->fileattr
) != 0) {
1160 if (a
& ATTR_FILE_LINKCOUNT
) size
+= sizeof(u_int32_t
);
1161 if (a
& ATTR_FILE_TOTALSIZE
) size
+= sizeof(off_t
);
1162 if (a
& ATTR_FILE_ALLOCSIZE
) size
+= sizeof(off_t
);
1163 if (a
& ATTR_FILE_IOBLOCKSIZE
) size
+= sizeof(u_int32_t
);
1164 if (a
& ATTR_FILE_CLUMPSIZE
) size
+= sizeof(u_int32_t
);
1165 if (a
& ATTR_FILE_DEVTYPE
) size
+= sizeof(u_int32_t
);
1166 if (a
& ATTR_FILE_DATALENGTH
) size
+= sizeof(off_t
);
1167 if (a
& ATTR_FILE_DATAALLOCSIZE
) size
+= sizeof(off_t
);
1168 if (a
& ATTR_FILE_RSRCLENGTH
) size
+= sizeof(off_t
);
1169 if (a
& ATTR_FILE_RSRCALLOCSIZE
) size
+= sizeof(off_t
);
1175 #define KAUTH_DIR_WRITE_RIGHTS (KAUTH_VNODE_ACCESS | KAUTH_VNODE_ADD_FILE | \
1176 KAUTH_VNODE_ADD_SUBDIRECTORY | \
1177 KAUTH_VNODE_DELETE_CHILD)
1179 #define KAUTH_DIR_READ_RIGHTS (KAUTH_VNODE_ACCESS | KAUTH_VNODE_LIST_DIRECTORY)
1181 #define KAUTH_DIR_EXECUTE_RIGHTS (KAUTH_VNODE_ACCESS | KAUTH_VNODE_SEARCH)
1183 #define KAUTH_FILE_WRITE_RIGHTS (KAUTH_VNODE_ACCESS | KAUTH_VNODE_WRITE_DATA)
1185 #define KAUTH_FILE_READRIGHTS (KAUTH_VNODE_ACCESS | KAUTH_VNODE_READ_DATA)
1187 #define KAUTH_FILE_EXECUTE_RIGHTS (KAUTH_VNODE_ACCESS | KAUTH_VNODE_EXECUTE)
1191 * Compute the same [expensive] user_access value as getattrlist does
1194 hfs_real_user_access(vnode_t vp
, vfs_context_t ctx
)
1196 u_int32_t user_access
= 0;
1198 if (vnode_isdir(vp
)) {
1199 if (vnode_authorize(vp
, NULLVP
, KAUTH_DIR_WRITE_RIGHTS
, ctx
) == 0)
1200 user_access
|= W_OK
;
1201 if (vnode_authorize(vp
, NULLVP
, KAUTH_DIR_READ_RIGHTS
, ctx
) == 0)
1202 user_access
|= R_OK
;
1203 if (vnode_authorize(vp
, NULLVP
, KAUTH_DIR_EXECUTE_RIGHTS
, ctx
) == 0)
1204 user_access
|= X_OK
;
1206 if (vnode_authorize(vp
, NULLVP
, KAUTH_FILE_WRITE_RIGHTS
, ctx
) == 0)
1207 user_access
|= W_OK
;
1208 if (vnode_authorize(vp
, NULLVP
, KAUTH_FILE_READRIGHTS
, ctx
) == 0)
1209 user_access
|= R_OK
;
1210 if (vnode_authorize(vp
, NULLVP
, KAUTH_FILE_EXECUTE_RIGHTS
, ctx
) == 0)
1211 user_access
|= X_OK
;
1213 return (user_access
);
1218 DerivePermissionSummary(uid_t obj_uid
, gid_t obj_gid
, mode_t obj_mode
,
1219 struct mount
*mp
, kauth_cred_t cred
, __unused
struct proc
*p
)
1221 u_int32_t permissions
;
1223 if (obj_uid
== UNKNOWNUID
)
1224 obj_uid
= kauth_cred_getuid(cred
);
1226 /* User id 0 (root) always gets access. */
1227 if (!suser(cred
, NULL
)) {
1228 permissions
= R_OK
| W_OK
| X_OK
;
1232 /* Otherwise, check the owner. */
1233 if (hfs_owner_rights(VFSTOHFS(mp
), obj_uid
, cred
, NULL
, false) == 0) {
1234 permissions
= ((u_int32_t
)obj_mode
& S_IRWXU
) >> 6;
1238 /* Otherwise, check the groups. */
1239 if (! (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
)) {
1242 if (kauth_cred_ismember_gid(cred
, obj_gid
, &is_member
) == 0 && is_member
) {
1243 permissions
= ((u_int32_t
)obj_mode
& S_IRWXG
) >> 3;
1248 /* Otherwise, settle for 'others' access. */
1249 permissions
= (u_int32_t
)obj_mode
& S_IRWXO
;
1252 return (permissions
);
1257 * ===========================================================================
1258 * Support functions for filling up a vnode_attr structure based on attributes
1260 * ===========================================================================
1263 get_vattr_data_for_attrs(struct attrlist
*alp
, struct vnode_attr
*vap
,
1264 struct hfsmount
*hfsmp
, struct vnode
*vp
, struct cat_desc
*descp
,
1265 struct cat_attr
*atrp
, struct cat_fork
*datafork
, struct cat_fork
*rsrcfork
,
1268 if (alp
->commonattr
)
1269 vattr_data_for_common_attrs(alp
, vap
, hfsmp
, vp
, descp
, atrp
,
1272 if (alp
->dirattr
&& S_ISDIR(atrp
->ca_mode
))
1273 vattr_data_for_dir_attrs(alp
, vap
, hfsmp
, vp
, descp
, atrp
);
1275 if (alp
->fileattr
&& !S_ISDIR(atrp
->ca_mode
)) {
1276 vattr_data_for_file_attrs(alp
, vap
, hfsmp
, atrp
, datafork
,
1282 copy_name_attr(struct vnode_attr
*vap
, struct vnode
*vp
, const u_int8_t
*name
,
1287 u_int32_t attrlength
;
1290 /* A cnode's name may be incorrect for the root of a mounted
1291 * filesystem (it can be mounted on a different directory name
1292 * than the name of the volume, such as "blah-1"). So for the
1293 * root directory, it's best to return the last element of the
1294 location where the volume's mounted:
1296 if ((vp
!= NULL
) && vnode_isvroot(vp
) &&
1297 (mpname
= mountpointname(vnode_mount(vp
)))) {
1298 mpnamelen
= strlen(mpname
);
1300 /* Trim off any trailing slashes: */
1301 while ((mpnamelen
> 0) && (mpname
[mpnamelen
-1] == '/'))
1304 /* If there's anything left, use it instead of the volume's name */
1305 if (mpnamelen
> 0) {
1306 name
= (u_int8_t
*)mpname
;
1307 namelen
= mpnamelen
;
1316 attrlength
= namelen
+ 1;
1317 (void) strncpy((char *)vap
->va_name
, (const char *) name
, attrlength
);
1319 * round upto 8 and zero out the rounded up bytes.
1321 attrlength
= min(kHFSPlusMaxFileNameBytes
, ((attrlength
+ 7) & ~0x07));
1322 bzero(vap
->va_name
+ attrlength
, kHFSPlusMaxFileNameBytes
- attrlength
);
1326 vattr_data_for_common_attrs( struct attrlist
*alp
, struct vnode_attr
*vap
,
1327 struct hfsmount
*hfsmp
, struct vnode
*vp
, struct cat_desc
*cdp
,
1328 struct cat_attr
*cap
, vfs_context_t ctx
)
1330 attrgroup_t attr
= alp
->commonattr
;
1331 struct mount
*mp
= HFSTOVFS(hfsmp
);
1335 if (attr
& (ATTR_CMN_OWNERID
| ATTR_CMN_GRPID
)) {
1336 cuid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
1340 if (ATTR_CMN_NAME
& attr
) {
1342 copy_name_attr(vap
, vp
, cdp
->cd_nameptr
,
1344 VATTR_SET_SUPPORTED(vap
, va_name
);
1346 VATTR_CLEAR_SUPPORTED(vap
, va_name
);
1350 if (ATTR_CMN_DEVID
& attr
) {
1351 vap
->va_devid
= hfsmp
->hfs_raw_dev
;
1352 VATTR_SET_SUPPORTED(vap
, va_devid
);
1355 if (ATTR_CMN_FSID
& attr
) {
1356 vap
->va_fsid64
.val
[0] = hfsmp
->hfs_raw_dev
;
1357 vap
->va_fsid64
.val
[1] = vfs_typenum(mp
);
1358 VATTR_SET_SUPPORTED(vap
, va_fsid64
);
1361 * We always provide the objtype even if not asked because VFS helper
1362 * functions depend on knowing the object's type.
1364 vap
->va_objtype
= IFTOVT(cap
->ca_mode
);
1365 VATTR_SET_SUPPORTED(vap
, va_objtype
);
1367 if (ATTR_CMN_OBJTAG
& attr
) {
1368 vap
->va_objtag
= VT_HFS
;
1369 VATTR_SET_SUPPORTED(vap
, va_objtag
);
1372 * Exporting file IDs from HFS Plus:
1374 * For "normal" files the c_fileid is the same value as the
1375 * c_cnid. But for hard link files, they are different - the
1376 * c_cnid belongs to the active directory entry (ie the link)
1377 * and the c_fileid is for the actual inode (ie the data file).
1379 * The stat call (getattr) will always return the c_fileid
1380 * and Carbon APIs, which are hardlink-ignorant, will always
1381 * receive the c_cnid (from getattrlist).
1383 if ((ATTR_CMN_OBJID
& attr
) ||
1384 (ATTR_CMN_OBJPERMANENTID
& attr
)) {
1385 vap
->va_linkid
= cdp
->cd_cnid
;
1386 VATTR_SET_SUPPORTED(vap
, va_linkid
);
1389 if (ATTR_CMN_PAROBJID
& attr
) {
1390 vap
->va_parentid
= cdp
->cd_parentcnid
;
1391 VATTR_SET_SUPPORTED(vap
, va_parentid
);
1394 if (ATTR_CMN_SCRIPT
& attr
) {
1395 vap
->va_encoding
= cdp
->cd_encoding
;
1396 VATTR_SET_SUPPORTED(vap
, va_encoding
);
1399 if (ATTR_CMN_CRTIME
& attr
) {
1400 vap
->va_create_time
.tv_sec
= cap
->ca_itime
;
1401 vap
->va_create_time
.tv_nsec
= 0;
1402 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1405 if (ATTR_CMN_MODTIME
& attr
) {
1406 vap
->va_modify_time
.tv_sec
= cap
->ca_mtime
;
1407 vap
->va_modify_time
.tv_nsec
= 0;
1408 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1411 if (ATTR_CMN_CHGTIME
& attr
) {
1412 vap
->va_change_time
.tv_sec
= cap
->ca_ctime
;
1413 vap
->va_change_time
.tv_nsec
= 0;
1414 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1417 if (ATTR_CMN_ACCTIME
& attr
) {
1418 vap
->va_access_time
.tv_sec
= cap
->ca_atime
;
1419 vap
->va_access_time
.tv_nsec
= 0;
1420 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1423 if (ATTR_CMN_BKUPTIME
& attr
) {
1424 vap
->va_backup_time
.tv_sec
= cap
->ca_btime
;
1425 vap
->va_backup_time
.tv_nsec
= 0;
1426 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1429 if (ATTR_CMN_FNDRINFO
& attr
) {
1430 u_int8_t
*finfo
= NULL
;
1432 bcopy(&cap
->ca_finderinfo
, &vap
->va_finderinfo
[0],
1433 sizeof(u_int8_t
) * 32);
1434 finfo
= (u_int8_t
*)(&vap
->va_finderinfo
[0]);
1436 /* Don't expose a symlink's private type/creator. */
1437 if (S_ISLNK(cap
->ca_mode
)) {
1438 struct FndrFileInfo
*fip
;
1440 fip
= (struct FndrFileInfo
*)finfo
;
1445 /* advance 16 bytes into the attrbuf */
1448 /* also don't expose the date_added or write_gen_counter fields */
1449 if (S_ISREG(cap
->ca_mode
) || S_ISLNK(cap
->ca_mode
)) {
1450 struct FndrExtendedFileInfo
*extinfo
=
1451 (struct FndrExtendedFileInfo
*)finfo
;
1452 extinfo
->document_id
= 0;
1453 extinfo
->date_added
= 0;
1454 extinfo
->write_gen_counter
= 0;
1455 } else if (S_ISDIR(cap
->ca_mode
)) {
1456 struct FndrExtendedDirInfo
*extinfo
=
1457 (struct FndrExtendedDirInfo
*)finfo
;
1458 extinfo
->document_id
= 0;
1459 extinfo
->date_added
= 0;
1460 extinfo
->write_gen_counter
= 0;
1463 VATTR_SET_SUPPORTED(vap
, va_finderinfo
);
1466 if (ATTR_CMN_OWNERID
& attr
) {
1467 uid_t nuid
= cap
->ca_uid
;
1470 if (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
)
1472 else if (nuid
== UNKNOWNUID
)
1477 VATTR_SET_SUPPORTED(vap
, va_uid
);
1480 if (ATTR_CMN_GRPID
& attr
) {
1481 gid_t ngid
= cap
->ca_gid
;
1484 gid_t cgid
= kauth_cred_getgid(vfs_context_ucred(ctx
));
1485 if (((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
)
1487 else if (ngid
== UNKNOWNUID
)
1492 VATTR_SET_SUPPORTED(vap
, va_gid
);
1495 if (ATTR_CMN_ACCESSMASK
& attr
) {
1498 * [2856576] Since we are dynamically changing the owner, also
1499 * effectively turn off the set-user-id and set-group-id bits,
1500 * just like chmod(2) would when changing ownership. This prevents
1501 * a security hole where set-user-id programs run as whoever is
1502 * logged on (or root if nobody is logged in yet!)
1504 nmode
= (cap
->ca_uid
== UNKNOWNUID
) ?
1505 cap
->ca_mode
& ~(S_ISUID
| S_ISGID
) : cap
->ca_mode
;
1507 vap
->va_mode
= nmode
;
1508 VATTR_SET_SUPPORTED(vap
, va_mode
);
1511 if (ATTR_CMN_FLAGS
& attr
) {
1512 vap
->va_flags
= cap
->ca_flags
;
1513 VATTR_SET_SUPPORTED(vap
, va_flags
);
1516 if (ATTR_CMN_GEN_COUNT
& attr
) {
1517 vap
->va_write_gencount
= hfs_get_gencount_from_blob(
1518 (const uint8_t *)cap
->ca_finderinfo
, cap
->ca_mode
);
1519 VATTR_SET_SUPPORTED(vap
, va_write_gencount
);
1522 if (ATTR_CMN_DOCUMENT_ID
& attr
) {
1523 vap
->va_document_id
= hfs_get_document_id_from_blob(
1524 (const uint8_t *)cap
->ca_finderinfo
, cap
->ca_mode
);
1525 VATTR_SET_SUPPORTED(vap
, va_document_id
);
1528 if (ATTR_CMN_USERACCESS
& attr
) {
1529 u_int32_t user_access
;
1531 /* Take the long path when we have an ACL */
1532 if ((vp
!= NULLVP
) && (cap
->ca_recflags
& kHFSHasSecurityMask
)) {
1533 user_access
= hfs_real_user_access(vp
, ctx
);
1535 user_access
= DerivePermissionSummary(cap
->ca_uid
, cap
->ca_gid
,
1536 cap
->ca_mode
, mp
, vfs_context_ucred(ctx
), 0);
1538 /* Also consider READ-ONLY file system. */
1539 if (vfs_flags(mp
) & MNT_RDONLY
) {
1540 user_access
&= ~W_OK
;
1542 /* Locked objects are not writable either */
1543 if ((cap
->ca_flags
& UF_IMMUTABLE
) && (vfs_context_suser(ctx
) != 0))
1544 user_access
&= ~W_OK
;
1545 if ((cap
->ca_flags
& SF_IMMUTABLE
) && (vfs_context_suser(ctx
) == 0))
1546 user_access
&= ~W_OK
;
1548 vap
->va_user_access
= user_access
;
1549 VATTR_SET_SUPPORTED(vap
, va_user_access
);
1553 * Right now the best we can do is tell if we *don't* have extended
1554 * security (like hfs_vnop_getattr).
1556 if (ATTR_CMN_EXTENDED_SECURITY
& attr
) {
1557 if (!(cap
->ca_recflags
& kHFSHasSecurityMask
)) {
1558 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
1559 VATTR_SET_SUPPORTED(vap
, va_acl
);
1563 if (ATTR_CMN_FILEID
& attr
) {
1564 vap
->va_fileid
= cap
->ca_fileid
;
1565 VATTR_SET_SUPPORTED(vap
, va_fileid
);
1568 if (ATTR_CMN_PARENTID
& attr
) {
1569 vap
->va_parentid
= cdp
->cd_parentcnid
;
1570 VATTR_SET_SUPPORTED(vap
, va_parentid
);
1573 if (ATTR_CMN_ADDEDTIME
& attr
) {
1574 if (cap
->ca_recflags
& kHFSHasDateAddedMask
) {
1575 vap
->va_addedtime
.tv_sec
= hfs_get_dateadded_from_blob(
1576 (const uint8_t *)cap
->ca_finderinfo
, cap
->ca_mode
);
1577 vap
->va_addedtime
.tv_nsec
= 0;
1578 VATTR_SET_SUPPORTED(vap
, va_addedtime
);
1584 vattr_data_for_dir_attrs(struct attrlist
*alp
, struct vnode_attr
*vap
,
1585 struct hfsmount
*hfsmp
, struct vnode
*vp
, struct cat_desc
* descp
,
1586 struct cat_attr
* cattrp
)
1588 attrgroup_t attr
= alp
->dirattr
;
1592 * The DIR_LINKCOUNT is the count of real directory hard links.
1593 * (i.e. its not the sum of the implied "." and ".." references
1594 * typically used in stat's st_nlink field)
1596 if (ATTR_DIR_LINKCOUNT
& attr
) {
1597 vap
->va_dirlinkcount
= cattrp
->ca_linkcount
;
1598 VATTR_SET_SUPPORTED(vap
, va_dirlinkcount
);
1600 if (ATTR_DIR_ENTRYCOUNT
& attr
) {
1601 entries
= cattrp
->ca_entries
;
1603 if (descp
->cd_parentcnid
== kHFSRootParentID
) {
1604 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0)
1605 --entries
; /* hide private dir */
1606 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0)
1607 --entries
; /* hide private dir */
1609 ((hfsmp
->vcbAtrb
& kHFSVolumeJournaledMask
) &&
1610 (hfsmp
->hfs_flags
& HFS_READ_ONLY
)))
1611 entries
-= 2; /* hide the journal files */
1614 vap
->va_nchildren
= entries
;
1615 VATTR_SET_SUPPORTED(vap
, va_nchildren
);
1618 if (ATTR_DIR_MOUNTSTATUS
& attr
) {
1620 * There is not vnode_attr for mount point status.
1621 * XXX. Should there be ?
1623 u_int32_t mstatus
= 0;
1625 if (vp
!= NULL
&& vnode_mountedhere(vp
) != NULL
)
1626 mstatus
= DIR_MNTSTATUS_MNTPOINT
;
1631 vattr_data_for_file_attrs(struct attrlist
*alp
, struct vnode_attr
*vap
,
1632 struct hfsmount
*hfsmp
, struct cat_attr
*cattrp
, struct cat_fork
*datafork
,
1633 struct cat_fork
*rsrcfork
, struct vnode
*vp
)
1635 #if !HFS_COMPRESSION
1638 attrgroup_t attr
= alp
->fileattr
;
1639 off_t da_size
, rsrc_len
, rsrc_alloc
;
1640 u_int32_t allocblksize
;
1642 allocblksize
= HFSTOVCB(hfsmp
)->blockSize
;
1644 off_t datasize
= datafork
->cf_size
;
1645 off_t totalsize
= datasize
+ rsrcfork
->cf_size
;
1647 int handle_compressed
;
1648 handle_compressed
= (cattrp
->ca_flags
& UF_COMPRESSED
);// && hfs_file_is_compressed(VTOC(vp), 1);
1650 if (handle_compressed
) {
1651 if (attr
& (ATTR_FILE_DATALENGTH
|ATTR_FILE_TOTALSIZE
)) {
1652 if ( 0 == hfs_uncompressed_size_of_compressed_file(hfsmp
, vp
, cattrp
->ca_fileid
, &datasize
, 1) ) { /* 1 == don't take the cnode lock */
1653 /* total size of a compressed file is just the data size */
1654 totalsize
= datasize
;
1660 if (ATTR_FILE_LINKCOUNT
& attr
) {
1661 vap
->va_nlink
= cattrp
->ca_linkcount
;
1662 VATTR_SET_SUPPORTED(vap
, va_nlink
);
1664 if (ATTR_FILE_TOTALSIZE
& attr
) {
1665 VATTR_RETURN(vap
, va_total_size
, totalsize
);
1667 if (ATTR_FILE_ALLOCSIZE
& attr
) {
1668 VATTR_RETURN(vap
, va_total_alloc
,
1669 (off_t
)cattrp
->ca_blocks
* (off_t
)allocblksize
);
1671 if (ATTR_FILE_IOBLOCKSIZE
& attr
) {
1672 VATTR_RETURN(vap
, va_iosize
, hfsmp
->hfs_logBlockSize
);
1675 /* ATTR_FILE_CLUMPSIZE is obsolete */
1677 if (ATTR_FILE_DEVTYPE
& attr
) {
1680 if (S_ISBLK(cattrp
->ca_mode
) || S_ISCHR(cattrp
->ca_mode
))
1681 dev
= (u_int32_t
)cattrp
->ca_rdev
;
1683 VATTR_RETURN(vap
, va_rdev
, dev
);
1686 if (ATTR_FILE_DATALENGTH
& attr
) {
1687 VATTR_RETURN(vap
, va_data_size
, datasize
);
1690 /* fake the data fork size on a decmpfs compressed file to reflect the
1691 * uncompressed size. This ensures proper reading and copying of these
1693 * NOTE: we may need to get the vnode here because the vnode parameter
1694 * passed by hfs_vnop_readdirattr() may be null.
1697 if (handle_compressed
) {
1698 da_size
= (off_t
)rsrcfork
->cf_blocks
* (off_t
)allocblksize
;
1705 da_size
= (off_t
)datafork
->cf_blocks
* (off_t
)allocblksize
;
1706 rsrc_len
= rsrcfork
->cf_size
;
1707 rsrc_alloc
= (off_t
)rsrcfork
->cf_blocks
* (off_t
)allocblksize
;
1710 if (ATTR_FILE_DATAALLOCSIZE
& attr
) {
1711 VATTR_RETURN(vap
, va_data_alloc
, da_size
);
1714 if (ATTR_FILE_RSRCLENGTH
& attr
) {
1715 VATTR_RETURN(vap
, va_rsrc_length
, rsrc_len
);
1718 if (ATTR_FILE_RSRCALLOCSIZE
& attr
) {
1719 VATTR_RETURN(vap
, va_rsrc_alloc
, rsrc_alloc
);