From 3cbc6c627fe57c9a9783c52d148078f8d52f7b96 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Sat, 31 Jan 2015 19:57:46 -0800 Subject: Add fchmodat(AT_SYMLINK_NOFOLLOW) and fchmod O_PATH support Many libc functions have an option to not follow symbolic links. This is useful to avoid security sensitive code from inadvertantly following attacker supplied symlinks and taking inappropriate action on files it shouldn't. For example, open() has O_NOFOLLOW, chown() has lchown(), stat() has lstat(), etc. There is no such equivalent function for chmod(), such as lchmod(). To address this, POSIX introduced fchmodat(AT_SYMLINK_NOFOLLOW), which is intended to provide a way to perform a chmod operation which doesn't follow symlinks. Currently, the Linux kernel doesn't implement AT_SYMLINK_NOFOLLOW. In GLIBC, attempting to use the AT_SYMLINK_NOFOLLOW flag causes fchmodat to return ENOTSUP. Details are in "man fchmodat". Bionic currently differs from GLIBC in that AT_SYMLINK_NOFOLLOW is silently ignored and treated as if the flag wasn't present. This patch provides a userspace implementation of AT_SYMLINK_NOFOLLOW for bionic. Using open(O_PATH | O_NOFOLLOW), we can provide a way to atomically change the permissions on files without worrying about race conditions. As part of this change, we add support for fchmod on O_PATH file descriptors, because it's relatively straight forward and could be useful in the future. The basic idea behind this implementation comes from https://sourceware.org/bugzilla/show_bug.cgi?id=14578 , specifically comment #10. Change-Id: I1eba0cdb2c509d9193ceecf28f13118188a3cfa7 --- libc/Android.mk | 2 + libc/SYSCALLS.TXT | 4 +- libc/arch-arm/syscalls/__fchmod.S | 14 +++++++ libc/arch-arm/syscalls/__fchmodat.S | 14 +++++++ libc/arch-arm/syscalls/fchmod.S | 14 ------- libc/arch-arm/syscalls/fchmodat.S | 14 ------- libc/arch-arm64/syscalls/__fchmod.S | 15 +++++++ libc/arch-arm64/syscalls/__fchmodat.S | 15 +++++++ libc/arch-arm64/syscalls/fchmod.S | 14 ------- libc/arch-arm64/syscalls/fchmodat.S | 14 ------- libc/arch-mips/syscalls/__fchmod.S | 19 +++++++++ libc/arch-mips/syscalls/__fchmodat.S | 19 +++++++++ libc/arch-mips/syscalls/fchmod.S | 19 --------- libc/arch-mips/syscalls/fchmodat.S | 19 --------- libc/arch-mips64/syscalls/__fchmod.S | 26 ++++++++++++ libc/arch-mips64/syscalls/__fchmodat.S | 26 ++++++++++++ libc/arch-mips64/syscalls/fchmod.S | 25 ------------ libc/arch-mips64/syscalls/fchmodat.S | 25 ------------ libc/arch-x86/syscalls/__fchmod.S | 26 ++++++++++++ libc/arch-x86/syscalls/__fchmodat.S | 31 +++++++++++++++ libc/arch-x86/syscalls/fchmod.S | 26 ------------ libc/arch-x86/syscalls/fchmodat.S | 36 ----------------- libc/arch-x86_64/syscalls/__fchmod.S | 16 ++++++++ libc/arch-x86_64/syscalls/__fchmodat.S | 16 ++++++++ libc/arch-x86_64/syscalls/fchmod.S | 15 ------- libc/arch-x86_64/syscalls/fchmodat.S | 16 -------- libc/bionic/fchmod.cpp | 72 ++++++++++++++++++++++++++++++++++ libc/bionic/fchmodat.cpp | 67 +++++++++++++++++++++++++++++++ 28 files changed, 380 insertions(+), 239 deletions(-) create mode 100644 libc/arch-arm/syscalls/__fchmod.S create mode 100644 libc/arch-arm/syscalls/__fchmodat.S delete mode 100644 libc/arch-arm/syscalls/fchmod.S delete mode 100644 libc/arch-arm/syscalls/fchmodat.S create mode 100644 libc/arch-arm64/syscalls/__fchmod.S create mode 100644 libc/arch-arm64/syscalls/__fchmodat.S delete mode 100644 libc/arch-arm64/syscalls/fchmod.S delete mode 100644 libc/arch-arm64/syscalls/fchmodat.S create mode 100644 libc/arch-mips/syscalls/__fchmod.S create mode 100644 libc/arch-mips/syscalls/__fchmodat.S delete mode 100644 libc/arch-mips/syscalls/fchmod.S delete mode 100644 libc/arch-mips/syscalls/fchmodat.S create mode 100644 libc/arch-mips64/syscalls/__fchmod.S create mode 100644 libc/arch-mips64/syscalls/__fchmodat.S delete mode 100644 libc/arch-mips64/syscalls/fchmod.S delete mode 100644 libc/arch-mips64/syscalls/fchmodat.S create mode 100644 libc/arch-x86/syscalls/__fchmod.S create mode 100644 libc/arch-x86/syscalls/__fchmodat.S delete mode 100644 libc/arch-x86/syscalls/fchmod.S delete mode 100644 libc/arch-x86/syscalls/fchmodat.S create mode 100644 libc/arch-x86_64/syscalls/__fchmod.S create mode 100644 libc/arch-x86_64/syscalls/__fchmodat.S delete mode 100644 libc/arch-x86_64/syscalls/fchmod.S delete mode 100644 libc/arch-x86_64/syscalls/fchmodat.S create mode 100644 libc/bionic/fchmod.cpp create mode 100644 libc/bionic/fchmodat.cpp (limited to 'libc') diff --git a/libc/Android.mk b/libc/Android.mk index 691017a..9c54ab8 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -115,6 +115,8 @@ libc_bionic_src_files := \ bionic/error.cpp \ bionic/eventfd_read.cpp \ bionic/eventfd_write.cpp \ + bionic/fchmod.cpp \ + bionic/fchmodat.cpp \ bionic/ffs.cpp \ bionic/flockfile.cpp \ bionic/fork.cpp \ diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT index 0fa2a1e..d68a00f 100644 --- a/libc/SYSCALLS.TXT +++ b/libc/SYSCALLS.TXT @@ -113,7 +113,7 @@ int writev(int, const struct iovec*, int) all int __fcntl64:fcntl64(int, int, void*) arm,mips,x86 int fcntl(int, int, void*) arm64,mips64,x86_64 int flock(int, int) all -int fchmod(int, mode_t) all +int __fchmod:fchmod(int, mode_t) all int dup(int) all int pipe2(int*, int) all int dup3(int, int, int) all @@ -131,7 +131,7 @@ int __getdents64:getdents64(unsigned int, struct dirent*, unsigned int) arm,ar int __openat:openat(int, const char*, int, mode_t) all int faccessat(int, const char*, int, int) all -int fchmodat(int, const char*, mode_t, int) all +int __fchmodat:fchmodat(int, const char*, mode_t) all int fchownat(int, const char*, uid_t, gid_t, int) all int fstatat64|fstatat:fstatat64(int, const char*, struct stat*, int) arm,mips,x86 int fstatat64|fstatat:newfstatat(int, const char*, struct stat*, int) arm64,x86_64 diff --git a/libc/arch-arm/syscalls/__fchmod.S b/libc/arch-arm/syscalls/__fchmod.S new file mode 100644 index 0000000..ff888a1 --- /dev/null +++ b/libc/arch-arm/syscalls/__fchmod.S @@ -0,0 +1,14 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmod) + mov ip, r7 + ldr r7, =__NR_fchmod + swi #0 + mov r7, ip + cmn r0, #(MAX_ERRNO + 1) + bxls lr + neg r0, r0 + b __set_errno_internal +END(__fchmod) diff --git a/libc/arch-arm/syscalls/__fchmodat.S b/libc/arch-arm/syscalls/__fchmodat.S new file mode 100644 index 0000000..4d10f00 --- /dev/null +++ b/libc/arch-arm/syscalls/__fchmodat.S @@ -0,0 +1,14 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmodat) + mov ip, r7 + ldr r7, =__NR_fchmodat + swi #0 + mov r7, ip + cmn r0, #(MAX_ERRNO + 1) + bxls lr + neg r0, r0 + b __set_errno_internal +END(__fchmodat) diff --git a/libc/arch-arm/syscalls/fchmod.S b/libc/arch-arm/syscalls/fchmod.S deleted file mode 100644 index 5675f0a..0000000 --- a/libc/arch-arm/syscalls/fchmod.S +++ /dev/null @@ -1,14 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmod) - mov ip, r7 - ldr r7, =__NR_fchmod - swi #0 - mov r7, ip - cmn r0, #(MAX_ERRNO + 1) - bxls lr - neg r0, r0 - b __set_errno_internal -END(fchmod) diff --git a/libc/arch-arm/syscalls/fchmodat.S b/libc/arch-arm/syscalls/fchmodat.S deleted file mode 100644 index 3f7e0ee..0000000 --- a/libc/arch-arm/syscalls/fchmodat.S +++ /dev/null @@ -1,14 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmodat) - mov ip, r7 - ldr r7, =__NR_fchmodat - swi #0 - mov r7, ip - cmn r0, #(MAX_ERRNO + 1) - bxls lr - neg r0, r0 - b __set_errno_internal -END(fchmodat) diff --git a/libc/arch-arm64/syscalls/__fchmod.S b/libc/arch-arm64/syscalls/__fchmod.S new file mode 100644 index 0000000..05c67fc --- /dev/null +++ b/libc/arch-arm64/syscalls/__fchmod.S @@ -0,0 +1,15 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmod) + mov x8, __NR_fchmod + svc #0 + + cmn x0, #(MAX_ERRNO + 1) + cneg x0, x0, hi + b.hi __set_errno_internal + + ret +END(__fchmod) +.hidden __fchmod diff --git a/libc/arch-arm64/syscalls/__fchmodat.S b/libc/arch-arm64/syscalls/__fchmodat.S new file mode 100644 index 0000000..2406ea8 --- /dev/null +++ b/libc/arch-arm64/syscalls/__fchmodat.S @@ -0,0 +1,15 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmodat) + mov x8, __NR_fchmodat + svc #0 + + cmn x0, #(MAX_ERRNO + 1) + cneg x0, x0, hi + b.hi __set_errno_internal + + ret +END(__fchmodat) +.hidden __fchmodat diff --git a/libc/arch-arm64/syscalls/fchmod.S b/libc/arch-arm64/syscalls/fchmod.S deleted file mode 100644 index 83a8060..0000000 --- a/libc/arch-arm64/syscalls/fchmod.S +++ /dev/null @@ -1,14 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmod) - mov x8, __NR_fchmod - svc #0 - - cmn x0, #(MAX_ERRNO + 1) - cneg x0, x0, hi - b.hi __set_errno_internal - - ret -END(fchmod) diff --git a/libc/arch-arm64/syscalls/fchmodat.S b/libc/arch-arm64/syscalls/fchmodat.S deleted file mode 100644 index 8c5bb0e..0000000 --- a/libc/arch-arm64/syscalls/fchmodat.S +++ /dev/null @@ -1,14 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmodat) - mov x8, __NR_fchmodat - svc #0 - - cmn x0, #(MAX_ERRNO + 1) - cneg x0, x0, hi - b.hi __set_errno_internal - - ret -END(fchmodat) diff --git a/libc/arch-mips/syscalls/__fchmod.S b/libc/arch-mips/syscalls/__fchmod.S new file mode 100644 index 0000000..9bc491c --- /dev/null +++ b/libc/arch-mips/syscalls/__fchmod.S @@ -0,0 +1,19 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmod) + .set noreorder + .cpload t9 + li v0, __NR_fchmod + syscall + bnez a3, 1f + move a0, v0 + j ra + nop +1: + la t9,__set_errno_internal + j t9 + nop + .set reorder +END(__fchmod) diff --git a/libc/arch-mips/syscalls/__fchmodat.S b/libc/arch-mips/syscalls/__fchmodat.S new file mode 100644 index 0000000..07ea8f8 --- /dev/null +++ b/libc/arch-mips/syscalls/__fchmodat.S @@ -0,0 +1,19 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmodat) + .set noreorder + .cpload t9 + li v0, __NR_fchmodat + syscall + bnez a3, 1f + move a0, v0 + j ra + nop +1: + la t9,__set_errno_internal + j t9 + nop + .set reorder +END(__fchmodat) diff --git a/libc/arch-mips/syscalls/fchmod.S b/libc/arch-mips/syscalls/fchmod.S deleted file mode 100644 index 2a95cc3..0000000 --- a/libc/arch-mips/syscalls/fchmod.S +++ /dev/null @@ -1,19 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmod) - .set noreorder - .cpload t9 - li v0, __NR_fchmod - syscall - bnez a3, 1f - move a0, v0 - j ra - nop -1: - la t9,__set_errno_internal - j t9 - nop - .set reorder -END(fchmod) diff --git a/libc/arch-mips/syscalls/fchmodat.S b/libc/arch-mips/syscalls/fchmodat.S deleted file mode 100644 index d9de036..0000000 --- a/libc/arch-mips/syscalls/fchmodat.S +++ /dev/null @@ -1,19 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmodat) - .set noreorder - .cpload t9 - li v0, __NR_fchmodat - syscall - bnez a3, 1f - move a0, v0 - j ra - nop -1: - la t9,__set_errno_internal - j t9 - nop - .set reorder -END(fchmodat) diff --git a/libc/arch-mips64/syscalls/__fchmod.S b/libc/arch-mips64/syscalls/__fchmod.S new file mode 100644 index 0000000..94dd0a1 --- /dev/null +++ b/libc/arch-mips64/syscalls/__fchmod.S @@ -0,0 +1,26 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmod) + .set push + .set noreorder + li v0, __NR_fchmod + syscall + bnez a3, 1f + move a0, v0 + j ra + nop +1: + move t0, ra + bal 2f + nop +2: + .cpsetup ra, t1, 2b + LA t9,__set_errno_internal + .cpreturn + j t9 + move ra, t0 + .set pop +END(__fchmod) +.hidden __fchmod diff --git a/libc/arch-mips64/syscalls/__fchmodat.S b/libc/arch-mips64/syscalls/__fchmodat.S new file mode 100644 index 0000000..79f453f --- /dev/null +++ b/libc/arch-mips64/syscalls/__fchmodat.S @@ -0,0 +1,26 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmodat) + .set push + .set noreorder + li v0, __NR_fchmodat + syscall + bnez a3, 1f + move a0, v0 + j ra + nop +1: + move t0, ra + bal 2f + nop +2: + .cpsetup ra, t1, 2b + LA t9,__set_errno_internal + .cpreturn + j t9 + move ra, t0 + .set pop +END(__fchmodat) +.hidden __fchmodat diff --git a/libc/arch-mips64/syscalls/fchmod.S b/libc/arch-mips64/syscalls/fchmod.S deleted file mode 100644 index a877b78..0000000 --- a/libc/arch-mips64/syscalls/fchmod.S +++ /dev/null @@ -1,25 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmod) - .set push - .set noreorder - li v0, __NR_fchmod - syscall - bnez a3, 1f - move a0, v0 - j ra - nop -1: - move t0, ra - bal 2f - nop -2: - .cpsetup ra, t1, 2b - LA t9,__set_errno_internal - .cpreturn - j t9 - move ra, t0 - .set pop -END(fchmod) diff --git a/libc/arch-mips64/syscalls/fchmodat.S b/libc/arch-mips64/syscalls/fchmodat.S deleted file mode 100644 index 151492a..0000000 --- a/libc/arch-mips64/syscalls/fchmodat.S +++ /dev/null @@ -1,25 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmodat) - .set push - .set noreorder - li v0, __NR_fchmodat - syscall - bnez a3, 1f - move a0, v0 - j ra - nop -1: - move t0, ra - bal 2f - nop -2: - .cpsetup ra, t1, 2b - LA t9,__set_errno_internal - .cpreturn - j t9 - move ra, t0 - .set pop -END(fchmodat) diff --git a/libc/arch-x86/syscalls/__fchmod.S b/libc/arch-x86/syscalls/__fchmod.S new file mode 100644 index 0000000..7ad213e --- /dev/null +++ b/libc/arch-x86/syscalls/__fchmod.S @@ -0,0 +1,26 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmod) + pushl %ebx + .cfi_def_cfa_offset 8 + .cfi_rel_offset ebx, 0 + pushl %ecx + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset ecx, 0 + mov 12(%esp), %ebx + mov 16(%esp), %ecx + movl $__NR_fchmod, %eax + int $0x80 + cmpl $-MAX_ERRNO, %eax + jb 1f + negl %eax + pushl %eax + call __set_errno_internal + addl $4, %esp +1: + popl %ecx + popl %ebx + ret +END(__fchmod) diff --git a/libc/arch-x86/syscalls/__fchmodat.S b/libc/arch-x86/syscalls/__fchmodat.S new file mode 100644 index 0000000..f03c03f --- /dev/null +++ b/libc/arch-x86/syscalls/__fchmodat.S @@ -0,0 +1,31 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmodat) + pushl %ebx + .cfi_def_cfa_offset 8 + .cfi_rel_offset ebx, 0 + pushl %ecx + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset ecx, 0 + pushl %edx + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset edx, 0 + mov 16(%esp), %ebx + mov 20(%esp), %ecx + mov 24(%esp), %edx + movl $__NR_fchmodat, %eax + int $0x80 + cmpl $-MAX_ERRNO, %eax + jb 1f + negl %eax + pushl %eax + call __set_errno_internal + addl $4, %esp +1: + popl %edx + popl %ecx + popl %ebx + ret +END(__fchmodat) diff --git a/libc/arch-x86/syscalls/fchmod.S b/libc/arch-x86/syscalls/fchmod.S deleted file mode 100644 index 37851ff..0000000 --- a/libc/arch-x86/syscalls/fchmod.S +++ /dev/null @@ -1,26 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmod) - pushl %ebx - .cfi_def_cfa_offset 8 - .cfi_rel_offset ebx, 0 - pushl %ecx - .cfi_adjust_cfa_offset 4 - .cfi_rel_offset ecx, 0 - mov 12(%esp), %ebx - mov 16(%esp), %ecx - movl $__NR_fchmod, %eax - int $0x80 - cmpl $-MAX_ERRNO, %eax - jb 1f - negl %eax - pushl %eax - call __set_errno_internal - addl $4, %esp -1: - popl %ecx - popl %ebx - ret -END(fchmod) diff --git a/libc/arch-x86/syscalls/fchmodat.S b/libc/arch-x86/syscalls/fchmodat.S deleted file mode 100644 index f515512..0000000 --- a/libc/arch-x86/syscalls/fchmodat.S +++ /dev/null @@ -1,36 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmodat) - pushl %ebx - .cfi_def_cfa_offset 8 - .cfi_rel_offset ebx, 0 - pushl %ecx - .cfi_adjust_cfa_offset 4 - .cfi_rel_offset ecx, 0 - pushl %edx - .cfi_adjust_cfa_offset 4 - .cfi_rel_offset edx, 0 - pushl %esi - .cfi_adjust_cfa_offset 4 - .cfi_rel_offset esi, 0 - mov 20(%esp), %ebx - mov 24(%esp), %ecx - mov 28(%esp), %edx - mov 32(%esp), %esi - movl $__NR_fchmodat, %eax - int $0x80 - cmpl $-MAX_ERRNO, %eax - jb 1f - negl %eax - pushl %eax - call __set_errno_internal - addl $4, %esp -1: - popl %esi - popl %edx - popl %ecx - popl %ebx - ret -END(fchmodat) diff --git a/libc/arch-x86_64/syscalls/__fchmod.S b/libc/arch-x86_64/syscalls/__fchmod.S new file mode 100644 index 0000000..ba75f74 --- /dev/null +++ b/libc/arch-x86_64/syscalls/__fchmod.S @@ -0,0 +1,16 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmod) + movl $__NR_fchmod, %eax + syscall + cmpq $-MAX_ERRNO, %rax + jb 1f + negl %eax + movl %eax, %edi + call __set_errno_internal +1: + ret +END(__fchmod) +.hidden __fchmod diff --git a/libc/arch-x86_64/syscalls/__fchmodat.S b/libc/arch-x86_64/syscalls/__fchmodat.S new file mode 100644 index 0000000..a8fae95 --- /dev/null +++ b/libc/arch-x86_64/syscalls/__fchmodat.S @@ -0,0 +1,16 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(__fchmodat) + movl $__NR_fchmodat, %eax + syscall + cmpq $-MAX_ERRNO, %rax + jb 1f + negl %eax + movl %eax, %edi + call __set_errno_internal +1: + ret +END(__fchmodat) +.hidden __fchmodat diff --git a/libc/arch-x86_64/syscalls/fchmod.S b/libc/arch-x86_64/syscalls/fchmod.S deleted file mode 100644 index b35bd21..0000000 --- a/libc/arch-x86_64/syscalls/fchmod.S +++ /dev/null @@ -1,15 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmod) - movl $__NR_fchmod, %eax - syscall - cmpq $-MAX_ERRNO, %rax - jb 1f - negl %eax - movl %eax, %edi - call __set_errno_internal -1: - ret -END(fchmod) diff --git a/libc/arch-x86_64/syscalls/fchmodat.S b/libc/arch-x86_64/syscalls/fchmodat.S deleted file mode 100644 index 2d78d8e..0000000 --- a/libc/arch-x86_64/syscalls/fchmodat.S +++ /dev/null @@ -1,16 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(fchmodat) - movq %rcx, %r10 - movl $__NR_fchmodat, %eax - syscall - cmpq $-MAX_ERRNO, %rax - jb 1f - negl %eax - movl %eax, %edi - call __set_errno_internal -1: - ret -END(fchmodat) diff --git a/libc/bionic/fchmod.cpp b/libc/bionic/fchmod.cpp new file mode 100644 index 0000000..6e020b6 --- /dev/null +++ b/libc/bionic/fchmod.cpp @@ -0,0 +1,72 @@ +/* + * 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 +#include +#include +#include +#include +#include + +extern "C" int __fchmod(int, mode_t); + +int fchmod(int fd, mode_t mode) { + int saved_errno = errno; + int result = __fchmod(fd, mode); + + if ((result == 0) || (errno != EBADF)) { + return result; + } + + // fd could be an O_PATH file descriptor, and the kernel + // may not directly support fchmod() on such a file descriptor. + // Use /proc/self/fd instead to emulate this support. + // https://sourceware.org/bugzilla/show_bug.cgi?id=14578 + // + // As of February 2015, there are no kernels which support fchmod + // on an O_PATH file descriptor, and "man open" documents fchmod + // on O_PATH file descriptors as returning EBADF. + 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; + result = chmod(buf, mode); + if ((result == -1) && (errno == ELOOP)) { + // Linux does not support changing the mode of a symlink. + // For fchmodat(AT_SYMLINK_NOFOLLOW), POSIX requires a return + // value of ENOTSUP. Assume that's true here too. + errno = ENOTSUP; + } + + return result; +} diff --git a/libc/bionic/fchmodat.cpp b/libc/bionic/fchmodat.cpp new file mode 100644 index 0000000..c28e15a --- /dev/null +++ b/libc/bionic/fchmodat.cpp @@ -0,0 +1,67 @@ +/* + * 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 +#include +#include +#include +#include + +#include "private/ErrnoRestorer.h" + +extern "C" int __fchmodat(int, const char*, mode_t); + +int fchmodat(int dirfd, const char* pathname, mode_t mode, int flags) { + if ((flags & ~AT_SYMLINK_NOFOLLOW) != 0) { + errno = EINVAL; + return -1; + } + + if (flags & AT_SYMLINK_NOFOLLOW) { + // Emulate AT_SYMLINK_NOFOLLOW using the mechanism described + // at https://sourceware.org/bugzilla/show_bug.cgi?id=14578 + // comment #10 + + int fd = openat(dirfd, pathname, O_PATH | O_NOFOLLOW | O_CLOEXEC); + if (fd == -1) { + return -1; // returns errno from openat + } + + // POSIX requires that ENOTSUP be returned when the system + // doesn't support setting the mode of a symbolic link. + // This is true for all Linux kernels. + // We rely on the O_PATH compatibility layer added in the + // fchmod() function to get errno correct. + int result = fchmod(fd, mode); + ErrnoRestorer errno_restorer; // don't let close() clobber errno + close(fd); + return result; + } + + return __fchmodat(dirfd, pathname, mode); +} -- cgit v1.1