Showing posts with label permissions. Show all posts
Showing posts with label permissions. Show all posts

Tuesday, December 29, 2009

Linux File permissions: Beyond "rwx"

 

The basic:

All the Linux users know about the "rwx" file permissions. These defines if the people who is the owner/ in the same group/ any other people can read/write/execute the file.

However, have you notice some other things in the permission field? Sometimes a "s", sometimes a "t", or "c", "b", "l"... What does this mean? In this article, we'll check out the meanings of all these characters.

First things first, let's make the basic clear. When we do a:

ls -l

in a directory, we get something like this:

drwxr-xr-x 2 dejavu dejavu 512 7 23 14:52 directory

-rw-r--r-- 1 dejavu dejavu 3 7 23 14:52 file

What we are caring about here is the first 10 characters. The first character is "d" if it is a directory, and a "-" if it is a normal file. The following 9 characters can be divided into 3 groups: 2-4, 5-7, 8-10. Which marks the permissions for owner, group, and others, accordingly.

For a file, read, write, and executable is straightforward. For a directory, read and write is easy to understand too. But.... What does "x" for a directory mean? Could we really execute a directory?

What "x" mean for directories

In the Linux file system, everything is a file. The directory is just a special kind of file. And in this "directory" file, there are several blocks, each block holds the information of one file or one sub-directory in it.

Thus we can understand why the r permission is needed to do a ls in a directory: in fact the ls program reads the "directory" file and re-formats it to some human readable text.

And we can understand why we need the w permission to create, delete, or rename a file/sub-directory in a directory: we actually need to write to this "directory" file, to modify the corresponding blocks.

And for x, executable, let's think it as "search, and read part of the file, and then do things accordingly".

Now if we want to enter a directory, say cd to it, we'll need to read part of the "directory" file and do things accordingly. So if we don't have a x permission on a directory, we can't cd into it.

And say if we have an executable file in a directory, if we want to run that file, we first need to make sure the file exists (read the "directory" file), and then do things accordingly(executable permission of the "directory" file)

It might be confusing and you might say: "I'll just make the r and the x appears together". Right. This is indeed most of the cases. However, with the understanding above, we can do something really cool:

First, let's setup our experiment environment:

mkdir directory

cd directory

echo "hello" > file

Now if we do:

ls -l

we'll get something like:

-rw-r--r-- 1 dejavu dejavu 6 7 23 15:40 file

The permission bit mights be slightly different, depending on your umask settings, and the user-name/group-name will be different. Now do a:

cd ..

chmod 300 directory

ls -l

And you'll see:

d-wx------ 2 dejavu dejavu 512 7 23 15:40 directory

We have the executable permission, but not the read permission on this directory. Now if we try to:

cd directory

ls -l

We'll be able to do the cd command, which is decide by the x permission we have, but the ls -l command will fail:

Opening directory: permission denied

This says we don't have the r permission, so we can't read the "directory" file and find out what is in it.

But we know there's a file named "file" in it. So we can do:

cat file

or:

cd ..

cat directory/file

and we'll get what we want: the output as:

hello

Note if you are using some "tab completion" on file and directory names of your shell, you'll find out that they won't work on the name "file". This is because the file/directory name completion need to read what is in the directory, and requires the r permission of the "directory" file, which we don't have here.

The first character

As we said above, Linux treats everything is the file system a file. Most of the time we'll see directory files (with a d as the first character of the permission field) and normal files (with a - as the first character of the permission field).

However if you do a:

ls /dev

You'll find many other characters: c, b, and l, etc.

These are for "special" files in Linux. Both c and b marks device files, which corresponds to a device.

In the Linux world, there are two ways to read/write a file: stream based, and block based. Stream based means we'll need to read or write the file character by character. As for device, this is typically the terminals. And for block based devices, we read/write the file block by block, the block size, 512 bytes, 1024 bytes, will be determined by the device. The most typical device is the disk.

And a l marks a symbolic link file. Think symbolic link file kind of the "file shortcut" on windows. And we'll talk more about this in a later article.

There are also s and p. Where s means a Unix socket, and p means a named pipeline. These are facilities the Linux programs used to to IPC - Inter Process Communication. Most of the time the programs will create and delete these files automatically, and you don't need to worry about them.

Setuid setgid and sticky bits:

I have already discussed about these three in my previous posts..

You can refer the below links:

http://basicslinux.blogspot.com/2009/09/sticky-bit.html
http://basicslinux.blogspot.com/2009/09/special-permissions-within-red-hat.html

Friday, September 25, 2009

sticky bit

sticky bit — used primarily on directories, this bit dictates that a file created in the directory can be removed only by the user who created the file. it is indicated by the character t in place of the x in the everyone category. in Red Hat Linux the sticky bit is set by default on the /tmp/ directory for exactly this reason.

source :http://www.redhat.com/docs/manuals/linux/RHL-8.0-Manual/admin-primer/ch-acctsgrps.html

Wednesday, September 23, 2009

special permissions within Red Hat Linux.

special permissions within Red Hat Linux.

  • setuid — used only for applications, this permission indicates that the application runs as the owner of the file and not as the user executing the application. It is indicated by the character s in place of the x in the owner category. If the owner of the file does not have execution permissions, the S is capitalized.
  • setgid — used primarily for applications, this permission indicates that the application runs as the group owning the file and not as the group executing the application. If applied to a directory, all files a user creates within the directory are owned by the group who owns the directory, rather than by the user's private group (see the chapter titled Users and Groups in Official Red Hat Linux Reference Guide for more about user private groups). It is indicated by the character s in place of the x in the group category. If the group owner of the file or directory does not have execution permissions, the S is capitalized.

  • sticky bit — used primarily on directories, this bit dictates that a file created in the directory can be removed only by the user who created the file. it is indicated by the character t in place of the x in the everyone category. in Red Hat Linux the sticky bit is set by default on the /tmp/ directory for exactly this reason.

Sunday, September 20, 2009

Passwords and Permisssions

Ordinary users have two ways of protecting the data in their accounts:

1. choose a secure password

2. set permissions

Changing your password:

  1. One of the easiest method of breaking into a system is to crack a weak password. To create a secure password:
    • The password should be at least 6 characters long.
    • You should not use names or phrases that are easily obtained; do NOT use your name; do NOT use your dog's name; do NOT use your spouse's name; do NOT use your licence plate; do NOT use your birthdate ...
    • Include both CAPITAL and lowercase letters.
    • Include special characters ( ! @ # $ % ... )
    • Include digits
  2. One way to create a secure password is suggested in the passwd man page.
    • choose 2 small words ( eg: why not )
    • mix in some capitalization ( eg: whY Not )
    • join them with a special character ( eg: whY%Not )
  3. The command to change your password is: passwd

Setting permissions:

Access to files and directories is controlled by a permissions at both the file and directory levels.

  • permissions can be any combination of read, write, execute for the owner, group and world.
  • for a file:
    • without read permission, a user cannot view the contents of a file
    • without write permission, a user cannot modify the contents of the file
    • without execute permission, a user cannot execute the file.
  • for a directory:
    • without read permission, a user cannot get a directory listing
    • without write permission, a user cannot create new files
    • without execute permission, a user cannot cd to the directory, list the directory contents or save a file in the directory.
  • chmod - change permissions.
    • Read, write and execute permissions are set for three groups: the owner, the group and everyone else.
    • chmod 543 myfile would set access rights as follows:
      owner: 5 = 101 binary = read, not write, execute
      group: 4 = 100 binary = read, not write, not execute
      others: 3 = 011 binary = not read, write, execute
  • chmod - version 2
    • permissions can also be set using a text representation
      u - user or owner
      g - group
      o - others
      r - read permission
      w - write permission
      x - execute permission
    • chmod u+w file1
      adds write permission for the user
    • chmod +w file1
      adds write permission for the user, group and others
    • chmod go-x file1
      deletes execute permission for the group and others
    • chmod u=r file1
      changes the permissions for the user to be just read permission (group and other permissions are not changed)

Exercises:


  1. Try to change your password to cat. What error message did you get?
  2. Try to change your password to blackcat. What error message did you get?
  3. Think up a password and change your password.
  4. Try to the password for another user. For example, enter the command:
    passwd floopy
    What error message did you get?
  5. Can you view the contents of the directory /sbin?
  6. What are the permissions for the directory /sbin?
  7. Change the permissions on "readme" in your own home so that you have read/write access, the group has read access and others have no access.
  8. Change permissions on your home directory so that you have read/write/execute permissions, the group has read/execute permissions, and everyone else has no access.
  9. Make sure that you are in your own home directory. Create a subdirectory called ddd and copy readme to ddd/fff (a file called fff in the ddd subdirectory).
    • what are the permissions on ddd and fff? ____________________
    • change the permissions on fff to only rwx for the owner.
  10. Try the following exercise to see some of the operations that you can and cannot do when you have only read permission on a directory.
    • change the permissions on ddd only r for the owner (no permissions for the group and the world)
    • can you get a directory listing for ddd? _____
    • can you cd to ddd? _____
    • can you view the contents of fff with the command: cat ddd/fff ? ______
    • can you delete fff with the command: rm ddd/fff ?______
  11. Try the following to see what you can do when you have read and execute permission for a directory.
    • change the permissions on ddd only rx for the owner (no permissions for the group and the world)
    • can you get a directory listing for ddd? _____
    • can you cd to ddd? _____
    • can you view the contents of fff with the command: cat ddd/fff ? ______
    • can you delete fff with the command: rm ddd/fff ? ______
  12. Try the following to see what you can do when you have write and execute permissions for a directory.
    • change the permissions on ddd only wx for the owner (no permissions for the group and the world)
    • can you get a directory listing for ddd? _____
    • can you cd to ddd? _____
    • can you view the contents of fff with the command: cat ddd/fff ? ______
    • can you delete fff with the command: rm ddd/fff ? ______
    • can you execute the command: cd ; cp readme ddd/fff _____
  13. Try the following to see what you can do when you have only execute permissions for a directory.
    • change the permissions on ddd only x for the owner (no permissions for the group and the world)
    • can you get a directory listing for ddd? _____
    • can you cd to ddd? _____
    • can you view the contents of fff with the command: cat ddd/fff ? ______
    • can you delete with the command: rm ddd/fff
    • can you execute the command: cd ; cp readme ddd/fff _____

Questions and Answers:


  1. The directory entries for the /etc directory and the file /etc/passwd are:
    -rwxr-xr-x 18 root root 1024 May 20 11:58 /etc
    -rw-r--r-- 1 root root 658 May 20 11:58 /etc/passwd

    Can you modify this file (assuming you are not root)?
    Answer: No.
  2. Can you copy /etc/passwd to your own home directory assuming that you have write and execute permissions for your own home directory?
    Answer: Yes. Everyone has read permission for this file and can make a copy.
  3. If you are not floopy, can you copy /etc/passwd (permissions rw-r--r--) to the directory /home/floopy? The permissions on /home/floopy are rwxr-xr-x.
    Answer: No. /home/floopy is owned by floopy and the permissions are rwxr-xr-x meaning that everyone can read the contents of the directory but only floopy can store files in this directory.
  4. File1 has permissions: r--r--r--
    You enter the command: chmod ug+w file1
    What are the new permissions?
    Answer: rw-rw-r--
  5. What command will change the permissions on dir1 so that only the owner has read/execute access?
    Answer: chmod 500 dir1
  6. Can you cd to dir1 given the permissions are drw-------?
    Answer: No. Without execute access, you cannot cd to a directory or access any of the files in the directory.
  7. What command will change the permissions on dir1 so that you have write/execute permissions only?
    Answer: chmod 300 dir1
  8. Can you copy readme (permissions r--r--r--) to dir1 (permissions d-wx------ ) if you own dir1?
    Answer: Yes
  9. Can you get a directory listing for dir1 permissions (d-wx-wx-wx)?
    Answer: No. If you do not have read permission for a directory, then essentially you have blind access; you can write files in the dir1 but you cannot get a directory listing.

Source:http://floppix.ccai.com/protect.html

 
Things You Should Know About Linux !!!