I want to know how does a process in Linux decides what privileges it has?
Suppose there is a binary program Read_File that reads from file /home/myname/data.txt and displays the contents of it to the STD output; now, how does Read_File decides whether or not it has permission to read data.txt, what type of ids it checks to decide the privileges?
First, a bit of background:
The process is usually run by a specific user. So for example, if you log in yourself and run the program, it will run with the same privileges as yourself. You can check the permissions on the file with either
statorls -l.Using
statThe important infos here are:
This tells you the permissions for the owner (
rw-), group (r--) and everyone else (r--). It also shows you the current owner id (Uid) and the current group id (Gid).The abbreviations stand for:
r= read accessw= write accessx= execute/traverse directory accessUsing
ls -lls -lgives you a quick summary:Here you can see the same info as with
stat, but as a summary. Also, the uid’s and gid’s are resolved into names (in this casemalbertanddomain users). You can usels -uto see these as numeric values.In case you want to run the application as a different user as yourself, you can either use
su,sudoor your application itself can drop priviledges and change the user it is running as. This is usually the way system daemons do things.ACLs / extended attributes
Be careful about extended attributes. When listing the files using
ls -lthese are visible with an appended+sign. For example:Notice the following line:
The
+sign in-rwxr--+points to extended attributes. It is possible that these are ACLs. There is an excellent document on ACLs in the SuSE documentation. Go have a look at it if you need to. Explaining ACLs would certainly explode this article, so I won’t discuss those.Extended attributes could also be related to the file system. See the man page of chattr for more information on that.
Now, as a sidenote: this is StackOverflow. Questions should be development related. For questions like this one, there’s http://www.serverfault.com. But As you were not aware, that this is not a development problem, but more related to the OS, I felt I should answer anyway 😉
Good luck, and have fun!