libc: Add O_PATH support for fgetxattr / fsetxattr
Support O_PATH file descriptors when handling fgetxattr and fsetxattr.
This avoids requiring file read access to pull extended attributes.
This is needed to support O_PATH file descriptors when calling
SELinux's fgetfilecon() call. In particular, this allows the querying
and setting of SELinux file context by using something like the following
code:
int dirfd = open("/path/to/dir", O_DIRECTORY);
int fd = openat(dirfd, "file", O_PATH | O_NOFOLLOW);
char *context;
fgetfilecon(fd, &context);
This change was motivated by a comment in
https://android-review.googlesource.com/#/c/152680/1/toys/posix/ls.c
Change-Id: Ic0cdf9f9dd0e35a63b44a4c4a08400020041eddf
diff --git a/libc/Android.mk b/libc/Android.mk
index d6274dd..4dd1086 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -129,8 +129,10 @@
bionic/fchmod.cpp \
bionic/fchmodat.cpp \
bionic/ffs.cpp \
+ bionic/fgetxattr.cpp \
bionic/flockfile.cpp \
bionic/fpclassify.cpp \
+ bionic/fsetxattr.cpp \
bionic/ftruncate.cpp \
bionic/futimens.cpp \
bionic/getcwd.cpp \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 9d7f839..4890b89 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -122,8 +122,8 @@
int fchown:fchown32(int, uid_t, gid_t) arm,x86
int fchown:fchown(int, uid_t, gid_t) arm64,mips,mips64,x86_64
void sync(void) all
-int fsetxattr(int, const char*, const void*, size_t, int) all
-ssize_t fgetxattr(int, const char*, void*, size_t) all
+int ___fsetxattr:fsetxattr(int, const char*, const void*, size_t, int) all
+ssize_t ___fgetxattr:fgetxattr(int, const char*, void*, size_t) all
ssize_t flistxattr(int, char*, size_t) all
int fremovexattr(int, const char*) all
diff --git a/libc/arch-arm/syscalls/fgetxattr.S b/libc/arch-arm/syscalls/___fgetxattr.S
similarity index 81%
rename from libc/arch-arm/syscalls/fgetxattr.S
rename to libc/arch-arm/syscalls/___fgetxattr.S
index 3f1e5fc..e776cd6 100644
--- a/libc/arch-arm/syscalls/fgetxattr.S
+++ b/libc/arch-arm/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
mov ip, r7
ldr r7, =__NR_fgetxattr
swi #0
@@ -11,4 +11,5 @@
bxls lr
neg r0, r0
b __set_errno_internal
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-arm/syscalls/fsetxattr.S b/libc/arch-arm/syscalls/___fsetxattr.S
similarity index 89%
rename from libc/arch-arm/syscalls/fsetxattr.S
rename to libc/arch-arm/syscalls/___fsetxattr.S
index 0e33ad2..5445191 100644
--- a/libc/arch-arm/syscalls/fsetxattr.S
+++ b/libc/arch-arm/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
mov ip, sp
stmfd sp!, {r4, r5, r6, r7}
.cfi_def_cfa_offset 16
@@ -19,4 +19,5 @@
bxls lr
neg r0, r0
b __set_errno_internal
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-arm64/syscalls/fgetxattr.S b/libc/arch-arm64/syscalls/___fgetxattr.S
similarity index 79%
rename from libc/arch-arm64/syscalls/fgetxattr.S
rename to libc/arch-arm64/syscalls/___fgetxattr.S
index 0d6ada7..c0334cc 100644
--- a/libc/arch-arm64/syscalls/fgetxattr.S
+++ b/libc/arch-arm64/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
mov x8, __NR_fgetxattr
svc #0
@@ -11,4 +11,5 @@
b.hi __set_errno_internal
ret
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-arm64/syscalls/fsetxattr.S b/libc/arch-arm64/syscalls/___fsetxattr.S
similarity index 79%
rename from libc/arch-arm64/syscalls/fsetxattr.S
rename to libc/arch-arm64/syscalls/___fsetxattr.S
index e69e718..92be8de 100644
--- a/libc/arch-arm64/syscalls/fsetxattr.S
+++ b/libc/arch-arm64/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
mov x8, __NR_fsetxattr
svc #0
@@ -11,4 +11,5 @@
b.hi __set_errno_internal
ret
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-mips/syscalls/fgetxattr.S b/libc/arch-mips/syscalls/___fgetxattr.S
similarity index 82%
rename from libc/arch-mips/syscalls/fgetxattr.S
rename to libc/arch-mips/syscalls/___fgetxattr.S
index 6516feb..50ab69c 100644
--- a/libc/arch-mips/syscalls/fgetxattr.S
+++ b/libc/arch-mips/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
.set noreorder
.cpload t9
li v0, __NR_fgetxattr
@@ -16,4 +16,5 @@
j t9
nop
.set reorder
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-mips/syscalls/fsetxattr.S b/libc/arch-mips/syscalls/___fsetxattr.S
similarity index 82%
rename from libc/arch-mips/syscalls/fsetxattr.S
rename to libc/arch-mips/syscalls/___fsetxattr.S
index 663c0df..0312921 100644
--- a/libc/arch-mips/syscalls/fsetxattr.S
+++ b/libc/arch-mips/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
.set noreorder
.cpload t9
li v0, __NR_fsetxattr
@@ -16,4 +16,5 @@
j t9
nop
.set reorder
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-mips64/syscalls/fgetxattr.S b/libc/arch-mips64/syscalls/___fgetxattr.S
similarity index 85%
rename from libc/arch-mips64/syscalls/fgetxattr.S
rename to libc/arch-mips64/syscalls/___fgetxattr.S
index 44c248a..935b080 100644
--- a/libc/arch-mips64/syscalls/fgetxattr.S
+++ b/libc/arch-mips64/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
.set push
.set noreorder
li v0, __NR_fgetxattr
@@ -22,4 +22,5 @@
j t9
move ra, t0
.set pop
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-mips64/syscalls/fsetxattr.S b/libc/arch-mips64/syscalls/___fsetxattr.S
similarity index 85%
rename from libc/arch-mips64/syscalls/fsetxattr.S
rename to libc/arch-mips64/syscalls/___fsetxattr.S
index 0ad1f90..c02f406 100644
--- a/libc/arch-mips64/syscalls/fsetxattr.S
+++ b/libc/arch-mips64/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
.set push
.set noreorder
li v0, __NR_fsetxattr
@@ -22,4 +22,5 @@
j t9
move ra, t0
.set pop
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-x86/syscalls/fgetxattr.S b/libc/arch-x86/syscalls/___fgetxattr.S
similarity index 92%
rename from libc/arch-x86/syscalls/fgetxattr.S
rename to libc/arch-x86/syscalls/___fgetxattr.S
index 1eff931..2891511 100644
--- a/libc/arch-x86/syscalls/fgetxattr.S
+++ b/libc/arch-x86/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
pushl %ebx
.cfi_def_cfa_offset 8
.cfi_rel_offset ebx, 0
@@ -33,4 +33,5 @@
popl %ecx
popl %ebx
ret
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-x86/syscalls/fsetxattr.S b/libc/arch-x86/syscalls/___fsetxattr.S
similarity index 93%
rename from libc/arch-x86/syscalls/fsetxattr.S
rename to libc/arch-x86/syscalls/___fsetxattr.S
index 7af0ef0..287dafc 100644
--- a/libc/arch-x86/syscalls/fsetxattr.S
+++ b/libc/arch-x86/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
pushl %ebx
.cfi_def_cfa_offset 8
.cfi_rel_offset ebx, 0
@@ -38,4 +38,5 @@
popl %ecx
popl %ebx
ret
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-x86_64/syscalls/fgetxattr.S b/libc/arch-x86_64/syscalls/___fgetxattr.S
similarity index 82%
rename from libc/arch-x86_64/syscalls/fgetxattr.S
rename to libc/arch-x86_64/syscalls/___fgetxattr.S
index 7762474..302fd77 100644
--- a/libc/arch-x86_64/syscalls/fgetxattr.S
+++ b/libc/arch-x86_64/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
movq %rcx, %r10
movl $__NR_fgetxattr, %eax
syscall
@@ -13,4 +13,5 @@
call __set_errno_internal
1:
ret
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-x86_64/syscalls/fsetxattr.S b/libc/arch-x86_64/syscalls/___fsetxattr.S
similarity index 82%
rename from libc/arch-x86_64/syscalls/fsetxattr.S
rename to libc/arch-x86_64/syscalls/___fsetxattr.S
index 97822c4..125ef20 100644
--- a/libc/arch-x86_64/syscalls/fsetxattr.S
+++ b/libc/arch-x86_64/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
movq %rcx, %r10
movl $__NR_fsetxattr, %eax
syscall
@@ -13,4 +13,5 @@
call __set_errno_internal
1:
ret
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/bionic/fgetxattr.cpp b/libc/bionic/fgetxattr.cpp
new file mode 100644
index 0000000..6d999bf
--- /dev/null
+++ b/libc/bionic/fgetxattr.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/xattr.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+extern "C" ssize_t ___fgetxattr(int, const char*, void*, size_t);
+
+ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) {
+ int saved_errno = errno;
+ ssize_t result = ___fgetxattr(fd, name, value, size);
+
+ if ((result != -1) || (errno != EBADF)) {
+ return result;
+ }
+
+ // fd could be an O_PATH file descriptor, and the kernel
+ // may not directly support fgetxattr() on such a file descriptor.
+ // Use /proc/self/fd instead to emulate this support.
+ int fd_flag = fcntl(fd, F_GETFL);
+ if ((fd_flag == -1) || ((fd_flag & O_PATH) == 0)) {
+ errno = EBADF;
+ return -1;
+ }
+
+ char buf[40];
+ snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
+ errno = saved_errno;
+ return getxattr(buf, name, value, size);
+}
diff --git a/libc/bionic/fsetxattr.cpp b/libc/bionic/fsetxattr.cpp
new file mode 100644
index 0000000..6d2e868
--- /dev/null
+++ b/libc/bionic/fsetxattr.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/xattr.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+extern "C" int ___fsetxattr(int, const char*, const void*, size_t, int);
+
+int fsetxattr(int fd, const char* name, const void* value, size_t size, int flags) {
+ int saved_errno = errno;
+ int result = ___fsetxattr(fd, name, value, size, flags);
+
+ if ((result == 0) || (errno != EBADF)) {
+ return result;
+ }
+
+ // fd could be an O_PATH file descriptor, and the kernel
+ // may not directly support fsetxattr() on such a file descriptor.
+ // Use /proc/self/fd instead to emulate this support.
+ int fd_flag = fcntl(fd, F_GETFL);
+ if ((fd_flag == -1) || ((fd_flag & O_PATH) == 0)) {
+ errno = EBADF;
+ return -1;
+ }
+
+ char buf[40];
+ snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
+ errno = saved_errno;
+ return setxattr(buf, name, value, size, flags);
+}