0

This simple script

sudo rm -rf /tmp/a
echo a > /tmp/a
chmod a+w /tmp/a
echo b >> /tmp/a
sudo bash -c 'whoami; echo c >> /tmp/a'

outputs

root
bash: /tmp/a: Permission denied

Why does root not have permissions to write to /tmp/a?

Here's a reproducible way to demonstrate this via Docker:

# alpine 3.8
alpine=$(docker run -d alpine:3.8 sleep 99999999999999)
d() { docker exec $alpine "$@"; }
d apk add sudo bash
d adduser -D u
d sudo -u u bash -c 'echo a > /tmp/a; chmod a+w /tmp/a'
d bash -c 'whoami; echo b >> /tmp/a'
docker rm -f $alpine

# ubuntu 18.04
ubuntu=$(docker run -d ubuntu:18.04 sleep 99999999999999)
d() { docker exec $ubuntu "$@"; }
d useradd u
d apt-get update
d apt-get install -y sudo
d sudo -u u bash -c 'echo a > /tmp/a; chmod a+w /tmp/a'
d bash -c 'whoami; echo b >> /tmp/a'
docker rm -f $ubuntu
Cbhihe
  • 2,549
  • 2
  • 21
  • 30
ens
  • 212
  • 2
  • 10
  • When I try this on my system (Ubuntu 18.04) it works perfectly and there is no permission denied error. – gogoud Feb 15 '20 at 13:11
  • I've added an Ubuntu 18.04 example which also fails. – ens Feb 15 '20 at 13:28
  • maybe it is something Docker-specific? I am not using Docker – gogoud Feb 15 '20 at 13:29
  • No, I'm getting this error on regular systems, Docker is just for reproducibility. – ens Feb 15 '20 at 13:35
  • ok well good luck, I am using a regular system and not seeing it. – gogoud Feb 15 '20 at 13:37
  • In your system what is the output of `sudo sysctl fs.protected_symlinks`? – Paulo Tomé Feb 15 '20 at 13:48
  • It's `1`. The error indeed disappears when using a path that is not `/tmp/`-prefixed. But why is this relevant when there are no symlinks involved? – ens Feb 15 '20 at 13:56
  • 2
    See [Group permissions for root not working in /tmp](https://unix.stackexchange.com/questions/503111/group-permissions-for-root-not-working-in-tmp). – Paulo Tomé Feb 15 '20 at 14:06
  • That solves it, thanks! Do you want to add an answer that I can accept? Or should a mod mark this as a duplicate? – ens Feb 15 '20 at 14:19
  • 1
    Does this answer your question? [Group permissions for root not working in /tmp](https://unix.stackexchange.com/questions/503111/group-permissions-for-root-not-working-in-tmp) – Paulo Tomé Feb 15 '20 at 14:23
  • 2
    My suggestion is to mark it as a duplicate. :) You're welcome. – Paulo Tomé Feb 15 '20 at 14:24

0 Answers0