| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "rsync.h" |
| 33 |
#include "acl.h" |
| 34 |
|
| 35 |
#ifdef SUPPORT_ACL_FD |
| 36 |
|
| 37 |
#include <errno.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include <unistd.h> |
| 41 |
#include <fcntl.h> |
| 42 |
|
| 43 |
#if defined HAVE_SYS_XATTR_H |
| 44 |
#include <sys/xattr.h> |
| 45 |
#elif defined HAVE_ATTR_XATTR_H |
| 46 |
#include <attr/xattr.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifdef HAVE_XATTRAT_SYSCALLS |
| 50 |
#include <sys/syscall.h> |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
struct rsync_xattr_args { |
| 55 |
uint64_t value __attribute__((aligned(8))); |
| 56 |
uint32_t size; |
| 57 |
uint32_t flags; |
| 58 |
}; |
| 59 |
#endif |
| 60 |
|
| 61 |
|
| 62 |
#ifndef ENOATTR |
| 63 |
#define ENOATTR ENODATA |
| 64 |
#endif |
| 65 |
|
| 66 |
#define ACL_XATTR_ACCESS "system.posix_acl_access" |
| 67 |
#define ACL_XATTR_DEFAULT "system.posix_acl_default" |
| 68 |
|
| 69 |
|
| 70 |
#define ACL_EA_VERSION 0x0002 |
| 71 |
#define ACL_EA_HDR_LEN 4 |
| 72 |
#define ACL_EA_ENT_LEN 8 |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static void put_le16(unsigned char *p, uint16_t v) |
| 77 |
{ |
| 78 |
p[0] = (unsigned char)(v & 0xff); |
| 79 |
p[1] = (unsigned char)((v >> 8) & 0xff); |
| 80 |
} |
| 81 |
|
| 82 |
static void put_le32(unsigned char *p, uint32_t v) |
| 83 |
{ |
| 84 |
p[0] = (unsigned char)(v & 0xff); |
| 85 |
p[1] = (unsigned char)((v >> 8) & 0xff); |
| 86 |
p[2] = (unsigned char)((v >> 16) & 0xff); |
| 87 |
p[3] = (unsigned char)((v >> 24) & 0xff); |
| 88 |
} |
| 89 |
|
| 90 |
static uint16_t get_le16(const unsigned char *p) |
| 91 |
{ |
| 92 |
return (uint16_t)(p[0] | (p[1] << 8)); |
| 93 |
} |
| 94 |
|
| 95 |
static uint32_t get_le32(const unsigned char *p) |
| 96 |
{ |
| 97 |
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
| 98 |
| ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); |
| 99 |
} |
| 100 |
|
| 101 |
static int is_named_tag(uint16_t tag) |
| 102 |
{ |
| 103 |
return tag == RACL_USER || tag == RACL_GROUP; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static int ent_compare(const void *a, const void *b) |
| 110 |
{ |
| 111 |
const rsync_acl_ent *x = a, *y = b; |
| 112 |
if (x->tag != y->tag) |
| 113 |
return x->tag < y->tag ? -1 : 1; |
| 114 |
if (x->id != y->id) |
| 115 |
return x->id < y->id ? -1 : 1; |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
static unsigned char *acl_to_xattr(const rsync_acl_ent *ents, int count, size_t *len_out) |
| 121 |
{ |
| 122 |
size_t len = ACL_EA_HDR_LEN + (size_t)count * ACL_EA_ENT_LEN; |
| 123 |
unsigned char *buf = malloc(len); |
| 124 |
rsync_acl_ent *sorted = NULL; |
| 125 |
unsigned char *p; |
| 126 |
int i; |
| 127 |
|
| 128 |
if (!buf) |
| 129 |
return NULL; |
| 130 |
if (count > 1) { |
| 131 |
sorted = malloc((size_t)count * sizeof sorted[0]); |
| 132 |
if (!sorted) { |
| 133 |
free(buf); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
memcpy(sorted, ents, (size_t)count * sizeof sorted[0]); |
| 137 |
qsort(sorted, count, sizeof sorted[0], ent_compare); |
| 138 |
ents = sorted; |
| 139 |
} |
| 140 |
|
| 141 |
put_le32(buf, ACL_EA_VERSION); |
| 142 |
p = buf + ACL_EA_HDR_LEN; |
| 143 |
for (i = 0; i < count; i++, p += ACL_EA_ENT_LEN) { |
| 144 |
put_le16(p, ents[i].tag); |
| 145 |
put_le16(p + 2, ents[i].perm); |
| 146 |
put_le32(p + 4, is_named_tag(ents[i].tag) ? ents[i].id : RACL_UNDEFINED_ID); |
| 147 |
} |
| 148 |
|
| 149 |
if (sorted) |
| 150 |
free(sorted); |
| 151 |
*len_out = len; |
| 152 |
return buf; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
static int xattr_to_acl(const unsigned char *buf, size_t len, |
| 157 |
rsync_acl_ent **out, int *count_out) |
| 158 |
{ |
| 159 |
rsync_acl_ent *ents; |
| 160 |
const unsigned char *p; |
| 161 |
int n, i; |
| 162 |
|
| 163 |
if (len < ACL_EA_HDR_LEN || (len - ACL_EA_HDR_LEN) % ACL_EA_ENT_LEN != 0 |
| 164 |
|| get_le32(buf) != ACL_EA_VERSION) { |
| 165 |
errno = EINVAL; |
| 166 |
return -1; |
| 167 |
} |
| 168 |
n = (int)((len - ACL_EA_HDR_LEN) / ACL_EA_ENT_LEN); |
| 169 |
|
| 170 |
ents = n ? malloc((size_t)n * sizeof ents[0]) : NULL; |
| 171 |
if (n && !ents) |
| 172 |
return -1; |
| 173 |
p = buf + ACL_EA_HDR_LEN; |
| 174 |
for (i = 0; i < n; i++, p += ACL_EA_ENT_LEN) { |
| 175 |
ents[i].tag = get_le16(p); |
| 176 |
ents[i].perm = get_le16(p + 2); |
| 177 |
ents[i].id = is_named_tag(ents[i].tag) ? get_le32(p + 4) : RACL_UNDEFINED_ID; |
| 178 |
} |
| 179 |
if (n > 1) |
| 180 |
qsort(ents, n, sizeof ents[0], ent_compare); |
| 181 |
|
| 182 |
*out = ents; |
| 183 |
*count_out = n; |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
static int proc_fd_leaf_path(char *buf, size_t buflen, int dirfd, const char *leaf) |
| 197 |
{ |
| 198 |
int n = snprintf(buf, buflen, "/proc/self/fd/%d/%s", dirfd, leaf); |
| 199 |
if (n < 0 || (size_t)n >= buflen) { |
| 200 |
errno = ENAMETOOLONG; |
| 201 |
return -1; |
| 202 |
} |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
static ssize_t do_getxattr(int fd, int dirfd, const char *leaf, |
| 207 |
const char *name, void *val, size_t size) |
| 208 |
{ |
| 209 |
char p[MAXPATHLEN]; |
| 210 |
|
| 211 |
if (fd >= 0) |
| 212 |
return fgetxattr(fd, name, val, size); |
| 213 |
#ifdef HAVE_XATTRAT_SYSCALLS |
| 214 |
{ |
| 215 |
struct rsync_xattr_args args; |
| 216 |
ssize_t ret; |
| 217 |
args.value = (uint64_t)(uintptr_t)val; |
| 218 |
args.size = (uint32_t)size; |
| 219 |
args.flags = 0; |
| 220 |
ret = syscall(SYS_getxattrat, dirfd, leaf, AT_SYMLINK_NOFOLLOW, |
| 221 |
name, &args, sizeof args); |
| 222 |
if (ret != -1 || errno != ENOSYS) |
| 223 |
return ret; |
| 224 |
|
| 225 |
} |
| 226 |
#endif |
| 227 |
if (proc_fd_leaf_path(p, sizeof p, dirfd, leaf) < 0) |
| 228 |
return -1; |
| 229 |
return lgetxattr(p, name, val, size); |
| 230 |
} |
| 231 |
|
| 232 |
static int do_setxattr(int fd, int dirfd, const char *leaf, |
| 233 |
const char *name, const void *val, size_t size) |
| 234 |
{ |
| 235 |
char p[MAXPATHLEN]; |
| 236 |
|
| 237 |
if (fd >= 0) |
| 238 |
return fsetxattr(fd, name, val, size, 0); |
| 239 |
#ifdef HAVE_XATTRAT_SYSCALLS |
| 240 |
{ |
| 241 |
struct rsync_xattr_args args; |
| 242 |
int ret; |
| 243 |
args.value = (uint64_t)(uintptr_t)val; |
| 244 |
args.size = (uint32_t)size; |
| 245 |
args.flags = 0; |
| 246 |
ret = syscall(SYS_setxattrat, dirfd, leaf, AT_SYMLINK_NOFOLLOW, |
| 247 |
name, &args, sizeof args); |
| 248 |
if (ret != -1 || errno != ENOSYS) |
| 249 |
return ret; |
| 250 |
} |
| 251 |
#endif |
| 252 |
if (proc_fd_leaf_path(p, sizeof p, dirfd, leaf) < 0) |
| 253 |
return -1; |
| 254 |
return lsetxattr(p, name, val, size, 0); |
| 255 |
} |
| 256 |
|
| 257 |
static int do_removexattr(int fd, int dirfd, const char *leaf, const char *name) |
| 258 |
{ |
| 259 |
char p[MAXPATHLEN]; |
| 260 |
|
| 261 |
if (fd >= 0) |
| 262 |
return fremovexattr(fd, name); |
| 263 |
#ifdef HAVE_XATTRAT_SYSCALLS |
| 264 |
{ |
| 265 |
int ret = syscall(SYS_removexattrat, dirfd, leaf, AT_SYMLINK_NOFOLLOW, name); |
| 266 |
if (ret != -1 || errno != ENOSYS) |
| 267 |
return ret; |
| 268 |
} |
| 269 |
#endif |
| 270 |
if (proc_fd_leaf_path(p, sizeof p, dirfd, leaf) < 0) |
| 271 |
return -1; |
| 272 |
return lremovexattr(p, name); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
static int read_full_xattr(int fd, int dirfd, const char *leaf, |
| 277 |
const char *name, unsigned char **buf_out, size_t *len_out) |
| 278 |
{ |
| 279 |
unsigned char *buf = NULL; |
| 280 |
size_t size = 0; |
| 281 |
int tries; |
| 282 |
|
| 283 |
for (tries = 0; tries < 8; tries++) { |
| 284 |
ssize_t n = do_getxattr(fd, dirfd, leaf, name, size ? buf : NULL, size); |
| 285 |
if (n >= 0) { |
| 286 |
if (size == 0) { |
| 287 |
|
| 288 |
size = n ? (size_t)n : 1; |
| 289 |
buf = malloc(size); |
| 290 |
if (!buf) |
| 291 |
return -1; |
| 292 |
continue; |
| 293 |
} |
| 294 |
*buf_out = buf; |
| 295 |
*len_out = (size_t)n; |
| 296 |
return 0; |
| 297 |
} |
| 298 |
if (errno == ERANGE) { |
| 299 |
if (buf) |
| 300 |
free(buf); |
| 301 |
buf = NULL; |
| 302 |
size = 0; |
| 303 |
continue; |
| 304 |
} |
| 305 |
if (buf) |
| 306 |
free(buf); |
| 307 |
return -1; |
| 308 |
} |
| 309 |
if (buf) |
| 310 |
free(buf); |
| 311 |
errno = ERANGE; |
| 312 |
return -1; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static int acl_get_common(int fd, int dirfd, const char *leaf, |
| 318 |
int want_default, rsync_acl_ent **entries, int *count) |
| 319 |
{ |
| 320 |
const char *name = want_default ? ACL_XATTR_DEFAULT : ACL_XATTR_ACCESS; |
| 321 |
unsigned char *buf; |
| 322 |
size_t len; |
| 323 |
int rc; |
| 324 |
|
| 325 |
*entries = NULL; |
| 326 |
*count = 0; |
| 327 |
|
| 328 |
if (read_full_xattr(fd, dirfd, leaf, name, &buf, &len) < 0) { |
| 329 |
if (errno == ENODATA || errno == ENOATTR) |
| 330 |
return 0; |
| 331 |
return -1; |
| 332 |
} |
| 333 |
|
| 334 |
rc = xattr_to_acl(buf, len, entries, count); |
| 335 |
free(buf); |
| 336 |
return rc; |
| 337 |
} |
| 338 |
|
| 339 |
int xacl_get_fd(int fd, int want_default, rsync_acl_ent **entries, int *count) |
| 340 |
{ |
| 341 |
return acl_get_common(fd, -1, NULL, want_default, entries, count); |
| 342 |
} |
| 343 |
|
| 344 |
int xacl_get_at(int dirfd, const char *leaf, int want_default, |
| 345 |
rsync_acl_ent **entries, int *count) |
| 346 |
{ |
| 347 |
return acl_get_common(-1, dirfd, leaf, want_default, entries, count); |
| 348 |
} |
| 349 |
|
| 350 |
static int acl_set_common(int fd, int dirfd, const char *leaf, |
| 351 |
int want_default, const rsync_acl_ent *ents, int count) |
| 352 |
{ |
| 353 |
const char *name = want_default ? ACL_XATTR_DEFAULT : ACL_XATTR_ACCESS; |
| 354 |
unsigned char *buf; |
| 355 |
size_t len; |
| 356 |
int rc, save_errno; |
| 357 |
|
| 358 |
buf = acl_to_xattr(ents, count, &len); |
| 359 |
if (!buf) { |
| 360 |
errno = ENOMEM; |
| 361 |
return -1; |
| 362 |
} |
| 363 |
rc = do_setxattr(fd, dirfd, leaf, name, buf, len); |
| 364 |
save_errno = errno; |
| 365 |
free(buf); |
| 366 |
errno = save_errno; |
| 367 |
return rc < 0 ? -1 : 0; |
| 368 |
} |
| 369 |
|
| 370 |
int xacl_set_fd(int fd, int want_default, const rsync_acl_ent *ents, int count) |
| 371 |
{ |
| 372 |
return acl_set_common(fd, -1, NULL, want_default, ents, count); |
| 373 |
} |
| 374 |
|
| 375 |
int xacl_set_at(int dirfd, const char *leaf, int want_default, |
| 376 |
const rsync_acl_ent *ents, int count) |
| 377 |
{ |
| 378 |
return acl_set_common(-1, dirfd, leaf, want_default, ents, count); |
| 379 |
} |
| 380 |
|
| 381 |
static int acl_del_default_common(int fd, int dirfd, const char *leaf) |
| 382 |
{ |
| 383 |
if (do_removexattr(fd, dirfd, leaf, ACL_XATTR_DEFAULT) < 0) { |
| 384 |
if (errno == ENODATA || errno == ENOATTR) |
| 385 |
return 0; |
| 386 |
return -1; |
| 387 |
} |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
int xacl_del_default_fd(int fd) |
| 392 |
{ |
| 393 |
return acl_del_default_common(fd, -1, NULL); |
| 394 |
} |
| 395 |
|
| 396 |
int xacl_del_default_at(int dirfd, const char *leaf) |
| 397 |
{ |
| 398 |
return acl_del_default_common(-1, dirfd, leaf); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
static int proc_self_fd_usable(void) |
| 404 |
{ |
| 405 |
int dfd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 406 |
char p[64]; |
| 407 |
int usable = 0; |
| 408 |
|
| 409 |
if (dfd < 0) |
| 410 |
return 0; |
| 411 |
if (snprintf(p, sizeof p, "/proc/self/fd/%d/.", dfd) < (int)sizeof p) { |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
errno = 0; |
| 416 |
lgetxattr(p, "user.rsync_acl_probe", NULL, 0); |
| 417 |
usable = !(errno == ENOENT || errno == ENOTDIR); |
| 418 |
} |
| 419 |
close(dfd); |
| 420 |
return usable; |
| 421 |
} |
| 422 |
|
| 423 |
int xacl_at_available(void) |
| 424 |
{ |
| 425 |
static int avail = -1; |
| 426 |
|
| 427 |
if (avail < 0) { |
| 428 |
#ifdef HAVE_XATTRAT_SYSCALLS |
| 429 |
|
| 430 |
|
| 431 |
struct rsync_xattr_args args; |
| 432 |
args.value = 0; |
| 433 |
args.size = 0; |
| 434 |
args.flags = 0; |
| 435 |
errno = 0; |
| 436 |
syscall(SYS_getxattrat, AT_FDCWD, ".", AT_SYMLINK_NOFOLLOW, |
| 437 |
"user.rsync_acl_probe", &args, sizeof args); |
| 438 |
if (errno != ENOSYS) { |
| 439 |
avail = 1; |
| 440 |
return avail; |
| 441 |
} |
| 442 |
#endif |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
avail = proc_self_fd_usable(); |
| 447 |
} |
| 448 |
return avail; |
| 449 |
} |
| 450 |
|
| 451 |
#endif |