差分

移動先: 案内検索

ユーザ権限とアクセス制御

1,748 バイト追加, 2018年12月14日 (金) 02:10
/* ディレクトリにsetgidビットを設定する */
<syntaxhighlight langpre class="bash">
$ ls -l memo.txt
-rw-r--r-- 1 hironobu hironobu 44 Sep 20 19:17 memo.txt
$ ls -l /var/mail/hironobu
-rw-rw---- 1 hironobu mail 0 Dec 9 19:39 hironobu
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ chmod go-rw memo.txt
$ ls -l memo.txt
-rw------- 1 hironobu hironobu 44 Sep 20 19:17 memo.txt
</syntaxhighlightpre>
ユーザ(所有者)、グループ、その他ユーザの読み取り許可・不許可、書き込み許可・不許可、実行の許可・不許可は3ビットで表すので、この部分を8進数で表現することもできます。
<syntaxhighlight langpre class="bash">
$ chmod 600 memo.txt
$ ls -l memo.txt
-rw------- 1 hironobu hironobu 44 Sep 20 19:17 memo.txt
</syntaxhighlightpre>
== グループID利用の方法 ==
<syntaxhighlight langpre class="bash">
$ ls -l
合計 56
-rw-r--r-- 1 taro taro 51200 2005-11-23 18:33 group_share.dat
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ chmod o-wr group_share.dat
$ chmod g+wr group_share.dat
</syntaxhighlightpre>
あるいは
<syntaxhighlight langpre class="bash">
$ chmod 660 group_share.dat
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ ls -l
合計 56
-rw-rw---- 1 taro taro 51200 2005-11-23 18:33 group_share.dat
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
# groupadd taro-hanako
# gpasswd -a taro taro-hanako
# gpasswd -a hanako taro-hanako
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ chgrp taro-hanako group_share.dat
...
-rw-rw---- 1 taro taro-hanako 51200 2005-11-23 18:33 group_share.dat
</syntaxhighlightpre>
;考えてみよう: apacheはユーザwww-dataの権限で動作しています。cgi-binなどでコマンドを動かしファイルinfo.datに情報を書き込む必要があります。しかし、info.dataの中身に関してはセキュリティ上の問題のためユーザfoo以外には読ませたくありません(www-dataにも許可しません)。さて、この場合、info.datの適切なファイルの所有者、適切なパーミッションはどのようなものでしょうか。
=== プロセスのユーザIDとグループID ===
まずログインした時に、そのアカウント名に割り振られたユーザIDとグループIDの権限でログインシェルが動き始めます。
<syntaxhighlight langpre class="bash">
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
hironobu 2796 0.0 1.0 8752 6716 ? R 12:14 0:05 emacs20 -geometry
...
</syntaxhighlightpre>
=== 現在の権限管理 ===
古典的なUNIXはUIDとGIDだけで説明は済みますが、Linuxも含めBSD 4.3以降では、もっと複雑な設定ができます。
<syntaxhighlight langpre class="bash">
$ find /bin -perm +a+s -exec ls -l {} \;
-rwsr-xr-x 1 root root 26252 Mar 3 2012 /bin/fusermount
-rwsr-xr-x 1 root root 31116 Sep 13 2012 /bin/su
-rwsr-xr-x 1 root root 67720 Mar 30 2012 /bin/umount
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ find /bin -perm +g+s -exec ls -l {} \;
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
% ps -eo user,group,args | grep httpd
root root /usr/sbin/httpd
apache apache /usr/sbin/httpd
apache apache /usr/sbin/httpd
</syntaxhighlightpre== ディレクトリへの特殊な設定 ===== ディレクトリにsetgidビットを設定する === ディレクトリのグループにsetgidビットを設定すると、そのディレクトリ以下に作られるファイル及びディレクトリには現ディレクトリのグループが適用されます。 <pre class="bash">$ ls -l合計 4drwxrwx--- 2 hironobu hironobu 4096 12月 14 02:35 foo$ sudo touch foo/fileA$ ls -al foo合計 8drwxrwx--- 2 hironobu hironobu 4096 12月 14 02:36 .drwxrwx--- 3 hironobu hironobu 4096 12月 14 02:35 ..-rw-r----- 1 root root 0 12月 14 02:36 fileA</pre> この例は、fooというディレクトリがあり、そのディレクトリ下にコマンド touch を使いrootの持ち物である fileA を作成します。この時のfileAの所有者、グループともrootです。  <pre class="bash">$ chmod g+s foo$ ls -ld foodrwxrws--- 2 hironobu hironobu 4096 12月 14 02:36 foo$ sudo touch foo/fileB$ ls -al foo合計 8drwxrws--- 2 hironobu hironobu 4096 12月 14 02:37 .drwxrwx--- 3 hironobu hironobu 4096 12月 14 02:35 ..-rw-r----- 1 root root 0 12月 14 02:36 fileA-rw-r----- 1 root hironobu 0 12月 14 02:37 fileB</pre> ディレクトリ foo に対しsetgid ビットの設定を行います。setgid ビットがセットされるとグループの"x"だった部分が"s"に変化します。次にコマンド touch で fileB を作成します。 ディレクトリのグループ hironobu がファイルに継承されて fileB の所有者は root グループが hironobu になります。 === ディレクトリにStickyビットを設定する === ユーザ(user)が読み書きできるディレクトリにStickyビットを設定した場合、そのディレクトリにどのユーザでもファイルを作成することができますが、ディレクトリから削除する場合、そのファイルの所有者(owner)しか削除できません。 <pre class="bash">$ mkdir temp$ ls -dl tempdrwxr----- 2 hironobu hironobu 4096 12月 14 03:12 temp$ chmod go+rwxt temp$ ls -dl tempdrwxrwxrwt 2 hironobu hironobu 4096 12月 14 03:12 temp</pre> 
== ACL ==
<syntaxhighlight langpre class="bash">
$touch foo.txt
$ ls -l foo.txt
-rw-rw-r-- 1 hironobu hironobu 0 Aug 31 08:15 foo.txt
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ getfacl foo.txt
# file: foo.txt
group::rw-
other::r--
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ chmod go-rw foo.txt
$ ls -l foo.txt
-rw------- 1 hironobu hironobu 0 Aug 31 08:15 foo.txt
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ setfacl -m user:www-data:r foo.txt
</syntaxhighlightpre>
<syntaxhighlight langpre class="bash">
$ getfacl foo.txt
# file: foo.txt
mask::r--
other::---
</syntaxhighlightpre>
openSUSE
<ref>
openSUSE ドキュメンテーショ 第9章 Linux におけるアクセス制御リスト第15章 Linuxのアクセス制御リストhttphttps://opensuse-man-jawww.berliossuse.decom/opensuseja-htmljp/documentation/sles10/book_sle_reference/data/cha.security.acls.html</ref>
やRadHat
<ref>
<syntaxhighlight langpre class="bash">
% ls -l foo.txt
-rw-r-----+ 1 hironobu hironobu 0 Aug 31 08:15 foo.txt
</syntaxhighlightpre>