05 Linux Access Control
05 Linux Access Control
05 Linux Access Control
/etc/group /etc/shadow
/etc/group defines groups and Sysadmins can define new groups,
lists which users belong to them. /etc/shadow contains hashed passwords:
e.g. on puma
root:$1$SdMYD5fz$cr120C7tA0wDhXPrQHc3H1:15583:0:99999:7:::
root:x:0:root
bin:x:1:root,bin,daemon • faculty (all cs faculty) bin:*:13665:0:99999:7:::
daemon:*:13665:0:99999:7:::
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
• cs111sta (members of the adm:*:13665:0:99999:7:::
adm:x:4:root,adm,daemon CS111 teaching staff) …
lynux:$1$mUwRqvWp$2yfe5MJV0HbtdcDad8E7i/:15583:0:99999:7:::
tty:x:5: • cs251stu (cs251 students) gdome:$1$d6tpFiwO$rBD5jd1ASAYatV2UuAlXW.:14274:0:99999:7:::
disk:x:6:root
cs342:$1$kmTSYJoG$0AnYYfykI2e/nWPRXdjwO.:14194:0:99999:7:::
lp:x:7:daemon,lp
mem:x:8:
kmem:x:9:
wheel:x:10:root
Your Ubuntu VM uses longer salts and hashes:
...
lynux:x:500:
wendy:$6$FoFIUVCi$/FUkEldzfdJXMefmv/s76m4wRpeZPnHjsVdJ9pO.QgKWuZmV
gdome:x:501:
cjt5J53lZ8Sifj9Q3Pm6n6ukR9p8A1\43mnE2Q0:15594:0:99999:7:::
cs342:x:502:
cs342sta:x:503:cs342,lynux
cs342stu:x:504:cs342,gdome,lynux
Linux Authentication and Access Control 5-3 Linux Authentication and Access Control 5-4
LDAP Checking/Changing Who You Are
Our networked dept. machines now use LDAP, a database system for o whoami: name associated with current uid
users/groups that does not use /etc/passwd and /etc/group directly. o groups: groups of which current uid is a member
(But you will be using /etc/passwd and /etc/group on the machines you
o su username : "become" username
administer in E125.)
o su - username: "become" username, using initialization files
On puma, use getent command to get info from the LDAP database: o su: "become" root (su = superuser)
[fturbak@puma ~] getent passwd fturbak o sudo command: Execute command as root
fturbak:x:708:708:Franklyn Turbak:/home/fturbak:/bin/bash (if you re in /etc/sudoers and you give your password.)
[fturbak@puma ~] getent passwd acarnigl
acarnigl:x:4568:4569:Allison Carniglia class of 2013:/students/acarnigl:/usr/local/bin/scponly
# /usr/local/bin/scponly only allows SCP, not login access. For 110/111 students
Linux Authentication and Access Control 5-5 Linux Authentication and Access Control 5-6
Sudoer wendy changes guest password (red parts not echoed) Sudoer wendy adds user acct1
!wendy@cs342-ubuntu-1:~$sudo passwd guest !wendy@cs342-ubuntu-1:~$sudo useradd -m -s /bin/bash acct1
[sudo] password for wendy: Tr0ub4dor&3 [sudo] password for wendy: Tr0ub4dor&3
Enter new UNIX password: foobar
Retype new UNIX password: foobar • By default, puts user in empty group with same name as user
passwd: password updated successfully /etc/passwd now has: !acct1:x:1101:1101::/home/acct1:/bin/bash
/etc/group now has: acct1:x:1101:
• As root, wouldn’t need extra sudo password • Still need to set password (via passwd); ! in /etc/shadow locks user out:
acct1:!:15601:0:99999:7:::
• Can change anyone’s password as root
• -m creates account /home/acct1
• Anyone change her own password w/o being root. • -s specifies shell program for user.
• Many more options; see man useradd
• Add user to group sudo to make her a sudoer
• There are graphical user interfaces for creating accounts, setting passwords,
etc. but you will not have access to these in many contexts (like Capture the
Flag contests) So best to learn shell commands instead!
Linux Authentication and Access Control 5-9 Linux Authentication and Access Control 5-10
Linux Authentication and Access Control 5-11 Linux Authentication and Access Control 5-12
Special permissions Permissions as Bits & Octal Numbers
There are 12 (not 9) permission bits in Linux. 11 10 9 8 7 6 5 4 3 2 1 0
11 10 9 8 7 6 5 4 3 2 1 0 set set stic- user user user group group group other other other
uid gid key r w x r w x r w x
set set stic- user user user group group group other other other
uid gid key r w x r w x r w x
setuid bit permissions octal
setuid bit
changes user "x" to "s" -rwxrwxrwx 777
• on executable program: change effective user id (more later)
and no "x" to "S" -rwxr-x--- 750
setgid bit
setgid bit -rw-r—-r-- 644
• on executable program: change effective group (more later)
changes group "x" to "s" -rwsr-sr-- 6754
• on directory: files/subdirectories inherit group and its permissions and no "x" to "S"
from directory -rwsr-Sr-- 6744
sticky bit -rwsr-xr-- 4754
sticky bit
changes other "x" to "t" -rwxr-sr-- 2754
• on directory: only owner can delete files in the dir (used in /tmp) and no "x" to "T
-rwxr-xr-t 1755
-rwxr-xr-T 1754
Linux Authentication and Access Control 5-13 Linux Authentication and Access Control 5-14
Linux Authentication and Access Control 5-15 Linux Authentication and Access Control 5-16
Changing File Owner & Group: chown & chgrp chown & chgrp Examples
chown username/uid file : change owner of file [lynux@localhost ~]$ touch owntest; ls -al owntest
-rw-rw-r-- 1 lynux lynux 0 2010-09-20 03:24 owntest
chgrp groupname/gid file : change group of file
[lynux@localhost ~]$ chown gdome owntest
chown username/uid.groupname/gid file : change owner & group of file chown: changing ownership of `owntest': Operation not permitted
Linux Authentication and Access Control 5-17 Linux Authentication and Access Control 5-18
Why? For safety reasons, this version of Linux does not allow
shell scripts to be suid!
Linux Authentication and Access Control 5-23 Linux Authentication and Access Control 5-24
Circumventing the Restriction with execv Compile & Run squirrel3.c
[lynux@localhost setuid]$ gcc -o squirrel3 squirrel3.c
Do we have to write all suid programs in C rather than as bash scripts?
[lynux@localhost setuid]$ chmod u+s squirrel3; ls -al squirrel3
Sort of ... but there's a trick to transform a bash script to a C program. -rwsrwxr-x 1 lynux lynux 4820 2008-09-16 07:08 squirrel3
Here's a C program named squirrel3.c that runs the script squirrel2.sh : [lynux@localhost setuid]$ echo –n > nest; ls –al nest
-rw-rw-r-- 1 lynux lynux 0 2008-09-16 07:14 nest
Linux Authentication and Access Control 5-25 Linux Authentication and Access Control 5-26
Linux Authentication and Access Control 5-29 Linux Authentication and Access Control 5-30
The SetGID (SGID) Bit (Continued) SGID in Practice: CS111 Drop Folders
/home/cs111/drop:
[gdome@localhost shared]$ touch one; ls –al one
drwxrwx--- 2 cs111 cs111 4096 Sep 8 11:17 .
-rw-rw-r-- 1 gdome gdome 0 2008-09-16 08:43 one
lrwxrwxrwx 1 root root 29 Sep 8 11:17 astud1 -> /students/astud1/cs111/drop
...
[gdome@localhost shared]$ chmod g+s .
chmod: changing permissions of `.': Operation not permitted /home/cs111/drop/astud1:
drwxr-s--- 13 astud1 cs111 4096 Sep 2 20:39 .
[lynux@localhost download]$ chmod g+s shared; ls -al shared drwxr-s--- 3 astud1 cs111 4096 Sep 6 01:15 ps01
drwxrwsr-x 2 lynux cs342stu 4096 2008-09-16 08:43 shared drwxr-s--- 3 astud1 cs111 4096 Sep 15 23:35 ps02
...
[gdome@localhost shared]$ touch two; mkdir sub; ls -al
total 32 /home/cs111/drop/astud1/ps01:
drwxrwsr-x 3 lynux cs342stu 4096 2008-09-16 08:44 . drwxr-s--- 3 astud1 cs111 4096 Sep 6 01:15 .
drwxrwxr-x 4 lynux lynux 4096 2008-09-16 08:36 .. drwxr-sr-x 2 astud1 cs111 4096 Sep 6 01:16 Amanda_ps01
-rw-rw-r-- 1 gdome gdome 0 2008-09-16 08:43 one
/home/cs111/drop/astud1/ps01/Amanda_ps01:
drwxrwsr-x 2 gdome cs342stu 4096 2008-09-16 08:44 sub
drwxr-sr-x 2 astud1 cs111 4096 Sep 6 01:16 .
-rw-rw-r-- 1 gdome cs342stu 0 2008-09-16 08:44 two -rw-r--r-- 1 astud1 cs111 9044 Sep 6 01:15 Buggle.class
...
-rw-r--r-- 1 astud1 cs111 2678 Sep 6 01:15 Writing.java
Linux Authentication and Access Control 5-31 Linux Authentication and Access Control 5-32
The Sticky Bit: Problem The Sticky Bit: Solution
Any user with write access to directory can delete a file from
directory, regardless of owner: The "sticky bit" on a directory allows users to delete only those files
owned by them. Used to protect files in shared directory /tmp
[lynux@localhost shared]$ touch important; chmod 700 important
[lynux@localhost shared]$ ls -al
[lynux@localhost shared]$ chmod +t .
total 36
[lynux@localhost shared]$ touch important2
drwxrwsr-x 3 lynux cs342stu 4096 2008-09-16 09:06 .
[lynux@localhost shared]$ chmod 700 important2; ls -al
drwxrwxr-x 4 lynux lynux 4096 2008-09-16 08:36 ..
total 36
-rwx------ 1 lynux cs342stu 0 2008-09-16 09:06 important
drwxrwsr-t 3 lynux cs342stu 4096 2008-09-16 09:09 .
-rw-rw-r-- 1 gdome gdome 0 2008-09-16 08:43 one
drwxrwxr-x 4 lynux lynux 4096 2008-09-16 08:36 ..
drwxrwsr-x 2 gdome cs342stu 4096 2008-09-16 08:44 sub
-rwx------ 1 lynux cs342stu 0 2008-09-16 09:09 important2
-rw-rw-r-- 1 gdome cs342stu 0 2008-09-16 08:44 two
-rw-rw-r-- 1 gdome gdome 0 2008-09-16 08:43 one
[gdome@localhost shared]$ rm important drwxrwsr-x 2 gdome cs342stu 4096 2008-09-16 08:44 sub
rm: remove write-protected regular empty file `important'? y -rw-rw-r-- 1 gdome cs342stu 0 2008-09-16 08:44 two
Linux Authentication and Access Control 5-35 Linux Authentication and Access Control 5-36
Real-Life Access-Control Design Problem File Attributes
Goal: CS111 pset solutions viewable on web by staff but not students. In addition to permissions, Linux ext2 files also have attributes
settable via chattr (by root only) and gettable via lsattr.
Important details: These include:
• don’t want solutions to be world-readable on puma
o a : (append only) Can only append to file with this attribute.
• viewable web pages must be readable by user apache.
o c : (compressed) A file with this attribute is compressed when
How to achieve this goal? written to disk and uncompressed when read from disk.
o i : (immutable) A file with this cannot be modified, deleted or
renamed, and no link can be created to this file.
See man pages and Hacking Linux Exposed for more details.
Linux Authentication and Access Control 5-37 Linux Authentication and Access Control 5-38
[lynux@localhost shared]$ echo foo > itest; cat itest [lynux@localhost shared]$ echo foo > atest; cat atest
foo foo
[lynux@localhost shared]$ echo bar" > itest [lynux@localhost shared]$ echo bar" > atest
bash: itest: Permission denied bash: atest: Operation not permitted
[lynux@localhost shared]$ echo bar" >> atest; cat atest
foo
bar
Linux Authentication and Access Control 5-39 Linux Authentication and Access Control 5-40