1

Why can the ls command of Ubuntu list the files of a directory with no execution permission set ?

The Test directory has read and write permissions set but no execution permission set. I understand that the x attribute of the directory specify whether the directory can be accessed, and if it is not set then it doesn't matter whether r or w is set (please correct me if I'm wrong).

The cd and cat commands works as expected, i.e. that cannot do their job, since they cannot access the directory.

enter image description here

t3dodson
  • 3,949
  • 2
  • 29
  • 40
Shuzheng
  • 11,288
  • 20
  • 88
  • 186

2 Answers2

0

Edit:

Apologies, after re-reading the original post, I have a better understanding of the question. The files can be listed even though there is no execute permission because you have read permission on the directory. The x bit controls access to the inode, which contains the file metadata such as permissions info. This is why the files can be listed, but no permission data is available.

https://askubuntu.com/questions/83788/accessing-files-in-a-directory-without-x-permission

See also:

https://unix.stackexchange.com/questions/21251/how-do-directory-permissions-in-linux-work

Community
  • 1
  • 1
Patrick Tucci
  • 1,824
  • 1
  • 16
  • 22
  • `x` for directories is **not** execute. It is "enter" as the link you indicated states. That is distinct from "read" which is why the OPs example works. – Etan Reisner Jun 11 '15 at 18:39
  • If I can see the files in the directory, then why can't their info be read, like their owners and stuff ? – Shuzheng Jun 11 '15 at 18:49
  • @NicolasLykkeIversen To more directly answer your question "why can the ls command see the files without x", it's because since you have read permission, you can list the files, but without x permission, you can't see their inodes, which contains the permission metadata. – Patrick Tucci Jun 11 '15 at 19:05
  • @chipmunkofdoom2 Your "... you have read permission on the files." should be "... you have read permission on the directory." – twalberg Jun 11 '15 at 20:24
  • @NicolasLykkeIversen To word things in a different way that might help with comprehension - in most Linux file systems, a directory contains only the names of the files and a pointer to another structure that contains the permissions and other information. Because of this separation, the ability to see what file names are in a directory only requires read permission on the directory, while accessing the indirection to the other metadata requires execute permission on the directory. – twalberg Jun 11 '15 at 20:27
  • @twalberg you are correct. I worded that incorrectly, I will change. – Patrick Tucci Jun 11 '15 at 21:23
0

Having +r but not +x on a directory allows reading its contents, but not making it the current directory. Conversely, having +x on a directory but not +r will allow you to make it your current directory but not list it.

In other words, on a directory:

  • r: The read bit allows you to read the contents of that directory
  • w: The write bit allows you to create, rename and delete files
  • x: The execute bit allows you to chdir into that directory
  • If I can read its contents, then why cannot all file-info be read ? – Shuzheng Jun 11 '15 at 18:45
  • If you look at the documentation for `stat()`, it says: These functions return information about a file. No permissions are required on the file itself, but — in the case of stat() and lstat() — execute (search) permission is required on all of the directories in path that lead to the file. – jeanfrancoisim Jun 11 '15 at 18:47
  • What is `stat()` -- why do you mention this function ? – Shuzheng Jun 11 '15 at 18:55
  • @NicolasLykkeIversen: to do its job, `ls` uses various functions such as `lstat()`. Try `strace ls` – jfs Jun 11 '15 at 23:27