I have an ubuntu 20.04.x instance on AWS which displayed very unexpected behaviour.
I have a file in /tmp
:
root@ip-10-0-1-145:/tmp# ls -l api_requests_2023-08-23.log
-rw-r--r-- 1 www-data www-data 47450 Aug 23 19:04 api_requests_2023-08-23.log
I tried to truncate it, as root
which FAILED
!!!
root@ip-10-0-1-145:/tmp# truncate -s 0 api_requests_2023-08-23.log
truncate: cannot open 'api_requests_2023-08-23.log' for writing: Permission denied
I tried strace to check where the failure was, which gave me:
root@ip-10-0-1-145:/tmp# strace -e trace=file truncate -s 0 api_requests_2023-08-23.log
execve("/usr/bin/truncate", ["truncate", "-s", "0", "api_requests_2023-08-23.log"], 0x7ffcaa0ab958 /* 16 vars */) = 0
...
openat(AT_FDCWD, "api_requests_2023-08-23.log", O_WRONLY|O_CREAT|O_NONBLOCK, 0666) = -1 EACCES (Permission denied)
...
truncate: cannot open 'api_requests_2023-08-23.log' for writing: Permission denied
...
+++ exited with 1 +++
Given that temp has the sticky bit set as usual:
root@ip-10-0-1-145:/tmp# ls -ld
drwxrwxrwt 4 root root 12288 Sep 12 09:11 .
I tried to truncate the file using it's owner:
root@ip-10-0-1-145:/tmp# sudo -u www-data truncate -s 0 api_requests_2023-08-23.log
root@ip-10-0-1-145:/tmp# ls -lh api_requests_2023-08-23.log
-rw-r--r-- 1 www-data www-data 0 Sep 12 09:13 api_requests_2023-08-23.log
The truncate
as owner worked!!! (so this isn't caused by filesystem issues; the filesystem is local, on-disk, ext4, mounted with defaults).
This behaviour for sticky bit on dirs is defined as **'only owner or root'
** for all operations.
How was root
constrained on opening the file for writing (trucating via >api_requests_2023-08-23.log
also failed)?