1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <stdio.h>
7 #include <sys/xattr.h>
8 #include "selinux_internal.h"
9 #include "policy.h"
10
fsetxattr_wrapper(int fd,const char * name,const void * value,size_t size,int flags)11 static int fsetxattr_wrapper(int fd, const char* name, const void* value, size_t size, int flags) {
12 char buf[40];
13 int rc, fd_flag, saved_errno = errno;
14
15 rc = fsetxattr(fd, name, value, size, flags);
16 if (rc == 0 || errno != EBADF)
17 return rc;
18
19 /* Emulate O_PATH support */
20 fd_flag = fcntl(fd, F_GETFL);
21 if (fd_flag == -1 || (fd_flag & O_PATH) == 0) {
22 errno = EBADF;
23 return -1;
24 }
25
26 snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
27 errno = saved_errno;
28 return setxattr(buf, name, value, size, flags);
29 }
30
fsetfilecon_raw(int fd,const char * context)31 int fsetfilecon_raw(int fd, const char * context)
32 {
33 int rc = fsetxattr_wrapper(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1,
34 0);
35 if (rc < 0 && errno == ENOTSUP) {
36 char * ccontext = NULL;
37 int err = errno;
38 if ((fgetfilecon_raw(fd, &ccontext) >= 0) &&
39 (strcmp(context,ccontext) == 0)) {
40 rc = 0;
41 } else {
42 errno = err;
43 }
44 freecon(ccontext);
45 }
46 return rc;
47 }
48
49
fsetfilecon(int fd,const char * context)50 int fsetfilecon(int fd, const char *context)
51 {
52 int ret;
53 char * rcontext;
54
55 if (selinux_trans_to_raw_context(context, &rcontext))
56 return -1;
57
58 ret = fsetfilecon_raw(fd, rcontext);
59
60 freecon(rcontext);
61
62 return ret;
63 }
64