I have a file called test with no rwx permissions (I have removed all the permission using chmod)
[root@dev]# ls -l test
———-. 1 root root 21 Sep 7 22:22 test
Q1) I am trying to run the below code to check if the file can be opened or not. The value returned to fd is positive. Shouldn’t fd be -1. ?
fd = open(fname, O_RDONLY);
if (fd == -1) {
/* Print error message */
return (-1);
}
close(fd);
Q2) I am even able to cat the file. Shouldn’t there be an error when I cat the file.
[root@dev]# cat test
This is a test file.
[root@dev]#
NOTE: I am using root account.
Short answer: root can do anything.
Longer answer: In general, to open a file in a given mode, POSIX requires that the permissions based on your uid and gid allow you the requested type of access, or that you have “appropriate privileges”, a phrase which is implementation-defined. In most real-world implementations, one such condition that satisfies “appropriate privileges” is being the root user (uid 0).