0

I have a text file called Demo, which have write permission for the owner only. I also have an executable file (here called demowr.bash) that will write to this Demo file, and because I want other users to use this program, I set the set-uid bit. Here is what the ls -l is seen for the files.

-rw-r--r-- 1 Coder Coder 6 Oct 28 23:36 ./Demo
-rwsrwxr-x 1 Coder Coder 7472 Oct 28 23:27 ./demowr.bash*

If I execute the file as Coder, all is fine. But if I run it as any other user, it does not work, saying that it does not have write permission.

I have used the following codes to the executable file and the text file:

chmod u+s ./demowr.bash
chmod o-w ./demowr.bash

chmod o-w ./Demo
chmod g-w ./Demo

Here is the contents of the demowr.bash file

#!/bin/bash
echo "$1">Demo

Why is that,that even when the s bit is set, I am unable to write when run as an another user?

PS

Linux ignores the setuid¹ bit on all interpreted executables (i.e. executables starting with a #! line)

I also wrote a C program, to do the same thing. The result is the same, the access function is saying that write permission is not given. Here is the C code:

#define FILENAME "./Demo"

int main(int argn, char *argv[])
{
  int length = strlen(argv[1]);
  if (access(FILENAME,W_OK) == -1){
    printf("Error: You do now have write permission.\n");
    return 1;
  }

  int fd = open(FILENAME,O_WRONLY);
  write(fd,argv[1],length);
  close(fd);
  return 0;
}

When run as a non-owner, I am getting the error.

Error: You do now have write permission.
Arjob Mukherjee
  • 387
  • 1
  • 10
  • 2
    Possible duplicate of [Why do my setuid root bash shell scripts not work?](http://stackoverflow.com/questions/33565729/why-do-my-setuid-root-bash-shell-scripts-not-work) – melpomene Oct 28 '16 at 18:35
  • I have edited my question. It is not a duplicate. – Arjob Mukherjee Oct 28 '16 at 18:44
  • 2
    http://man7.org/linux/man-pages/man2/access.2.html "The check is done using the calling process's *real* UID and GID, rather than the effective IDs as is done when actually attempting an operation (e.g., [open(2)](http://man7.org/linux/man-pages/man2/open.2.html)) on the file." – melpomene Oct 28 '16 at 18:45
  • 1
    Also, your code is broken: Check the return value from `open` for errors before using `fd`. – melpomene Oct 28 '16 at 18:46
  • Thanks, the problem is solved. – Arjob Mukherjee Oct 28 '16 at 18:51
  • **WATCH OUT:** Having that program available as a setuid binary will allow anyone who can run it to gain root privileges. –  Oct 28 '16 at 19:04
  • For any value of `X`, _"X not working as it should"_ almost always means _"X not working **as I expected**"_ — the SUID bit is, in all probability, working as it should. – Stephen P Oct 28 '16 at 20:41

0 Answers0