1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
14 * Use is subject to license terms.
15 */
16
17 #include <sys/zfs_context.h>
18 #include <sys/vfs.h>
19 #include <sys/fs/zfs.h>
20 #include <sys/zfs_znode.h>
21 #include <sys/zfs_sa.h>
22 #include <sys/zfs_acl.h>
23
24 #ifndef _KERNEL
25 static
26 #endif
27 void
zfs_oldace_byteswap(ace_t * ace,int ace_cnt)28 zfs_oldace_byteswap(ace_t *ace, int ace_cnt)
29 {
30 for (int i = 0; i != ace_cnt; i++, ace++) {
31 ace->a_who = BSWAP_32(ace->a_who);
32 ace->a_access_mask = BSWAP_32(ace->a_access_mask);
33 ace->a_flags = BSWAP_16(ace->a_flags);
34 ace->a_type = BSWAP_16(ace->a_type);
35 }
36 }
37
38 /*
39 * swap ace_t and ace_object_t
40 */
41 #ifndef _KERNEL
42 static
43 #endif
44 void
zfs_ace_byteswap(void * buf,size_t size,boolean_t zfs_layout)45 zfs_ace_byteswap(void *buf, size_t size, boolean_t zfs_layout)
46 {
47 caddr_t end;
48 caddr_t ptr;
49 zfs_ace_t *zacep = NULL;
50 ace_t *acep;
51 uint16_t entry_type;
52 size_t entry_size;
53 int ace_type;
54
55 end = (caddr_t)buf + size;
56 ptr = buf;
57
58 while (ptr < end) {
59 if (zfs_layout) {
60 /*
61 * Avoid overrun. Embedded aces can have one
62 * of several sizes. We don't know exactly
63 * how many our present, only the size of the
64 * buffer containing them. That size may be
65 * larger than needed to hold the aces
66 * present. As long as we do not do any
67 * swapping beyond the end of our block we are
68 * okay. It is safe to swap any non-ace data
69 * within the block since it is just zeros.
70 */
71 if (ptr + sizeof (zfs_ace_hdr_t) > end) {
72 break;
73 }
74 zacep = (zfs_ace_t *)ptr;
75 zacep->z_hdr.z_access_mask =
76 BSWAP_32(zacep->z_hdr.z_access_mask);
77 zacep->z_hdr.z_flags = BSWAP_16(zacep->z_hdr.z_flags);
78 ace_type = zacep->z_hdr.z_type =
79 BSWAP_16(zacep->z_hdr.z_type);
80 entry_type = zacep->z_hdr.z_flags & ACE_TYPE_FLAGS;
81 } else {
82 /* Overrun avoidance */
83 if (ptr + sizeof (ace_t) > end) {
84 break;
85 }
86 acep = (ace_t *)ptr;
87 acep->a_access_mask = BSWAP_32(acep->a_access_mask);
88 acep->a_flags = BSWAP_16(acep->a_flags);
89 ace_type = acep->a_type = BSWAP_16(acep->a_type);
90 acep->a_who = BSWAP_32(acep->a_who);
91 entry_type = acep->a_flags & ACE_TYPE_FLAGS;
92 }
93 switch (entry_type) {
94 case ACE_OWNER:
95 case ACE_EVERYONE:
96 case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
97 entry_size = zfs_layout ?
98 sizeof (zfs_ace_hdr_t) : sizeof (ace_t);
99 break;
100 case ACE_IDENTIFIER_GROUP:
101 default:
102 /* Overrun avoidance */
103 if (zfs_layout) {
104 if (ptr + sizeof (zfs_ace_t) <= end) {
105 zacep->z_fuid = BSWAP_64(zacep->z_fuid);
106 } else {
107 entry_size = sizeof (zfs_ace_t);
108 break;
109 }
110 }
111 switch (ace_type) {
112 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
113 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
114 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
115 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
116 entry_size = zfs_layout ?
117 sizeof (zfs_object_ace_t) :
118 sizeof (ace_object_t);
119 break;
120 default:
121 entry_size = zfs_layout ? sizeof (zfs_ace_t) :
122 sizeof (ace_t);
123 break;
124 }
125 }
126 ptr = ptr + entry_size;
127 }
128 }
129
130 void
zfs_oldacl_byteswap(void * buf,size_t size)131 zfs_oldacl_byteswap(void *buf, size_t size)
132 {
133 /*
134 * Arggh, since we don't know how many ACEs are in
135 * the array, we have to swap the entire block
136 */
137 zfs_oldace_byteswap((ace_t *)buf, size / sizeof (ace_t));
138 }
139
140 void
zfs_acl_byteswap(void * buf,size_t size)141 zfs_acl_byteswap(void *buf, size_t size)
142 {
143 zfs_ace_byteswap(buf, size, B_TRUE);
144 }
145
146 void
zfs_znode_byteswap(void * buf,size_t size)147 zfs_znode_byteswap(void *buf, size_t size)
148 {
149 znode_phys_t *zp = buf;
150
151 ASSERT(size >= sizeof (znode_phys_t));
152
153 zp->zp_crtime[0] = BSWAP_64(zp->zp_crtime[0]);
154 zp->zp_crtime[1] = BSWAP_64(zp->zp_crtime[1]);
155 zp->zp_atime[0] = BSWAP_64(zp->zp_atime[0]);
156 zp->zp_atime[1] = BSWAP_64(zp->zp_atime[1]);
157 zp->zp_mtime[0] = BSWAP_64(zp->zp_mtime[0]);
158 zp->zp_mtime[1] = BSWAP_64(zp->zp_mtime[1]);
159 zp->zp_ctime[0] = BSWAP_64(zp->zp_ctime[0]);
160 zp->zp_ctime[1] = BSWAP_64(zp->zp_ctime[1]);
161 zp->zp_gen = BSWAP_64(zp->zp_gen);
162 zp->zp_mode = BSWAP_64(zp->zp_mode);
163 zp->zp_size = BSWAP_64(zp->zp_size);
164 zp->zp_parent = BSWAP_64(zp->zp_parent);
165 zp->zp_links = BSWAP_64(zp->zp_links);
166 zp->zp_xattr = BSWAP_64(zp->zp_xattr);
167 zp->zp_rdev = BSWAP_64(zp->zp_rdev);
168 zp->zp_flags = BSWAP_64(zp->zp_flags);
169 zp->zp_uid = BSWAP_64(zp->zp_uid);
170 zp->zp_gid = BSWAP_64(zp->zp_gid);
171 zp->zp_zap = BSWAP_64(zp->zp_zap);
172 zp->zp_pad[0] = BSWAP_64(zp->zp_pad[0]);
173 zp->zp_pad[1] = BSWAP_64(zp->zp_pad[1]);
174 zp->zp_pad[2] = BSWAP_64(zp->zp_pad[2]);
175
176 zp->zp_acl.z_acl_extern_obj = BSWAP_64(zp->zp_acl.z_acl_extern_obj);
177 zp->zp_acl.z_acl_size = BSWAP_32(zp->zp_acl.z_acl_size);
178 zp->zp_acl.z_acl_version = BSWAP_16(zp->zp_acl.z_acl_version);
179 zp->zp_acl.z_acl_count = BSWAP_16(zp->zp_acl.z_acl_count);
180 if (zp->zp_acl.z_acl_version == ZFS_ACL_VERSION) {
181 zfs_acl_byteswap((void *)&zp->zp_acl.z_ace_data[0],
182 ZFS_ACE_SPACE);
183 } else {
184 zfs_oldace_byteswap((ace_t *)&zp->zp_acl.z_ace_data[0],
185 ACE_SLOT_CNT);
186 }
187 }
188
189 #if defined(_KERNEL)
190 EXPORT_SYMBOL(zfs_oldacl_byteswap);
191 EXPORT_SYMBOL(zfs_acl_byteswap);
192 EXPORT_SYMBOL(zfs_znode_byteswap);
193 #endif
194