5

I have script that can be runned from different users on the same machine. This script should write logs to the same file on every run.

Minimal version of script:

#!/usr/bin/env bash
                                  # 2
touch /var/tmp/lll.log            # 3
chmod 666 /var/tmp/lll.log        # 4 (You can comment this line, but  this will change nothing)
echo ghghhghg >> /var/tmp/lll.log # 5

There is no problem when it started from root and then from other user, but error thrown when order is opposite.

./savetmp.sh: line 5: /var/tmp/lll.log: Permission denied

Output of ls -ld /var/tmp /var/tmp/lll.log:

.rw------- 9 armoken  1 May 10:52 /var/tmp/lll.log
drwxrwxrwt - root     1 May 10:52 /var/tmp

cat /proc/sys/fs/protected_regular:

1

How to fix that?

7
  • Is the filesystem read-only somehow? (check mount)
    – DopeGhoti
    Commented May 1 at 8:10
  • @DopeGhoti, no. Any user can write to this directory. Also problem exists when /tmp used, but there is no problem when user home directory used. The same error appears on Ubuntu and NixOS.
    – armoken
    Commented May 1 at 8:26
  • Are you saying that root gets an error when running the script? So user1 runs it, and then root runs it and root gets an error?
    – terdon
    Commented May 1 at 9:28
  • @terdon, yes it is.
    – armoken
    Commented May 1 at 9:31
  • 1
    lwn.net/Articles/748141
    – Tom Yan
    Commented May 1 at 9:46

1 Answer 1

8

When you run this as the user armoken the file is created according to your current permissions settings, which are such that you can read/write the file but no-one else can:

ls -l /var/tmp/lll.log
-rw------- 9 armoken  1 May 10:52 /var/tmp/lll.log

So when other users try to write to this file they have no permission to do so.

However, it's more complicated than this because you have the protected regular files security feature enabled in your system's kernel (cat /proc/sys/fs/protected_regular returns non-zero). This means that, regardless of these permissions, no-one other than the owner can write to a file in a sticky directory such as /var/tmp - not even root - unless the file is owned by the owner of the directory itself.

So, if you want everyone to be able to read/write this file in this directory you need to set it up so that root owns it and that anyone can write to it. But bear in mind this means other people can erase or change content in the file too.

#!/bin/sh
if [ ! -f /var/tmp/lll.log ]
then
    # File does not exist
    if [ "$(id -u)" -eq '0' ]
    then
        # We are root so create the file (and continue)
        >/var/tmp/lll.log
        chmod a=rw /var/tmp/lll.log
    else
        echo 'ERROR: Log file does not exist. Have your systems administrator create it before proceeding' >&2
        exit 1
    fi
fi

# Now anyone can read/write the contents of the file
echo 'This is a test message' >>/var/tmp/lll.log

This is not defensive coding, though, as anyone can still create the file and prevent others from using it.

A better solution might be to use a logger. For example, this will write to the files managed through journalctl (and/or /var/log/user.log otherwise)

logger 'This is a test message'

journalctl --since today | tail
…
May 01 10:14:24 myServer myUser[18892]: This is a test message
…
2
  • 1
    In case any of you aren't aware of it, the security feature can be disabled with the sysctl fs.protected_regular, so you don't need to rebuild a kernel or whatever to get rid of it. (Certainly that would at least pose some theoretical risks. So make your own call.)
    – Tom Yan
    Commented May 1 at 15:25
  • Ah yes, found that while investigating the feature, thank you, @TomYan. My thinking is that if it's been enabled then blindly advising people to disable it seemed irresponsible at best. Especially as it doesn't really solve the potential security issues posed by a world rw log file Commented May 1 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .