xref: /openbsd/lib/libkvm/kvm_udf.c (revision 0b082c0e)
1 /*	$OpenBSD: kvm_udf.c,v 1.11 2021/09/10 00:02:43 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/ucred.h>
32 #define _KERNEL
33 #include <sys/mount.h>
34 #undef _KERNEL
35 #include <sys/lock.h>
36 #include <sys/vnode.h>
37 #include <sys/sysctl.h>
38 #include <sys/specdev.h>
39 
40 #include <crypto/siphash.h>
41 
42 #include <isofs/udf/ecma167-udf.h>
43 #include <isofs/udf/udf.h>
44 
45 #include <stdint.h>
46 #include <limits.h>
47 #include <kvm.h>
48 #include <db.h>
49 
50 #include "kvm_private.h"
51 #include "kvm_file.h"
52 
53 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
54 static mode_t
udf_permtomode(struct unode * up)55 udf_permtomode(struct unode *up)
56 {
57 	uint32_t perm;
58 	uint16_t flags;
59 	mode_t mode;
60 
61 	perm = letoh32(up->u_fentry->perm);
62 	flags = letoh16(up->u_fentry->icbtag.flags);
63 
64 	mode = perm & UDF_FENTRY_PERM_USER_MASK;
65 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
66 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
67 	mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
68 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
69 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
70 
71 	return (mode);
72 }
73 
74 int
_kvm_stat_udf(kvm_t * kd,struct kinfo_file * kf,struct vnode * vp)75 _kvm_stat_udf(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
76 {
77 	struct unode up;
78 	struct file_entry fentry;
79 	struct umount um;
80 
81 	if (KREAD(kd, (u_long)VTOU(vp), &up)) {
82 		_kvm_err(kd, kd->program, "can't read unode at %p", VTOU(vp));
83 		return (-1);
84 	}
85 	if (KREAD(kd, (u_long)up.u_fentry, &fentry)) {
86 		_kvm_err(kd, kd->program, "can't read file_entry at %p",
87 		    up.u_fentry);
88 		return (-1);
89 	}
90 	if (KREAD(kd, (u_long)up.u_ump, &um)) {
91 		_kvm_err(kd, kd->program, "can't read umount at %p",
92 		    up.u_ump);
93 		return (-1);
94 	}
95 	kf->va_fsid = up.u_dev;
96 	kf->va_fileid = (long)up.u_ino;
97 	kf->va_mode = udf_permtomode(&up); /* XXX */
98 	kf->va_rdev = 0;
99 	kf->va_nlink = letoh16(fentry.link_cnt);
100 	if (vp->v_type & VDIR) {
101 		/*
102 		 * Directories that are recorded within their ICB will show
103 		 * as having 0 blocks recorded.  Since tradition dictates
104 		 * that directories consume at least one logical block,
105 		 * make it appear so.
106 		 */
107 		if (fentry.logblks_rec != 0) {
108 			kf->va_size =
109 			    letoh64(fentry.logblks_rec) * um.um_bsize;
110 		} else {
111 			kf->va_size = um.um_bsize;
112 		}
113 	} else {
114 		kf->va_size = letoh64(fentry.inf_len);
115 	}
116 
117 	return (0);
118 }
119