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.