# 2020/10/16 ## part 1  * 每個檔案有兩部分:metadata跟file content,file content會catch在buffer cache,metadata會catch在inode(metadata在disk上稱為inode)  * UNIX裡面的resource都是認id,inode會有個id:inode number * 為甚麼這個metadata沒有存檔名: Ans: 檔案實際上是一堆disk block,為了access這些disk block,會有一個metadata去描述他,所以會有一個inode去associate這些disk block,透過這個inode就可以把檔案的內容拿出來,UNIX認為說這個檔案的檔名是對剛剛這個檔案去給他命名,而此命名對一個檔案可以有很多種。每個目錄裡面會有檔案名稱,會緊接一個link,就是inode number,直接連到stat這個structure。 **檔名並不是檔案本身的屬性** |file types|e.g.| |-|-| |Regular Files|text, binary| |Directory Files|only kernel can update these files {(filename, pointer)}| |Character Special Files|tty, audio| |Block Special Files|disks| |FIFO|named pipes| |Sockets|在特定OS裡面(如4.3+BSD)中是一個file type| |Symbolic Links|捷徑檔| * st_mode在記完file type後會緊跟著記file permission #define S_IXOTH 00001 第一個0代表八進位 第二個有關security * file&directory permissions: * file有X權限:可以呼叫exec()來run這個file * file有R權限:在open的時候可以呼叫O_RDONLY,O_RDWR * 要看自己對file是哪種身分(owner/group/other),check身分會按照owner->group->other這個順序來確認,再按照身分來check是哪種權限。 * file有W權限:在open的時可以呼叫O_WRONLY,O_RDWR或O_TRUNC,truncate代表open的時候可以有一些允許的Option。 * directory有X權限:可以cd (search bit) * directory有R權限:可以ls * directory有W權限:可以create file/unlink file * 舉例:在目錄裡面殺一個檔案/建一個檔案:目錄需要W+X,檔案不用任何權限 * tmp目錄:權限幾乎是777,放在這個目錄的檔案都是暫時的檔案。(會有一個security bit來保護) * process的metadata:process control block * process的ID: * real user ID:指跑起來的process是誰(不能變,除了BSD) * real group ID:real user id的main group * **effective user ID:一個process能不能access檔案(看權限),是看effective user ID**(可以變) * effective group ID:effective user id的main group * supplementary group IDs:一個人可以有多個group,所以可以有很多個supplementary group id * saved set-user-ID:當effective user id 要改變的時候,儲存舊的effective user id,saved by exec() * saved set-group-ID:saved set-user-ID的main group ## part 2 要check一個檔案有沒有被set uid,就是先做stat,然後st_mode去check S_ISUID這個macro。 先前提到的00001的第二個0: * 04000:set user-id on execution * 02000:set group-id on execution * 如果被set uid,ls結果會出現- - s - - - - - -,這個s如果是小寫,代表原本的x存在,大寫則不存在 * 如果看到權限有s,盡量不要去任意執行他 * 01000:sticky bit 舉例: ``` ls -alt /usr/bin/passwd -rwsr-xr-x 1 root root 25692 May 24... ``` 要執行/usr/bin/passwd這個執行檔(不是/etc/passwd),ls看這個檔,會發現被set user-id。 WHY?因為此執行檔的owner是superuser,當我是Other,他允許我執行(因為other是r-x),即使不是我寫的,我就可以執行起來。 這樣會發生什麼事?因為set uid,所以執行起來,我的effective uid就會變成superuser。代表我執行起來這隻process,在access檔案的時候,我是用superuser的權限在access。 這個時候,這隻process就可以去更改/etc/passwd這個檔案。 建議這裡看PJ影片,有另一個範例講得更清楚,文字不好解釋。(2019/10/16-2 5:05~8:48) 舉例2: 一個user要怎麼讓他朋友(other)複製自己的檔案而他人不能? 注意:此檔案需要有r權限使他人可以讀檔來複製 * 方案1:給適當的權限 * 做法:檔案的other要打開r,並且將此檔案藏到一個目錄,目錄只能設x,目錄如果開r的話每個人都可以對目錄ls,就能看到目錄下的檔案並複製,因此只設x就只能cd進去而不能知道目錄底下有哪些檔案,並且告知朋友有一個檔案在這裡。(如果有其他人知道此路徑,其他人也可以複製) * 方案2:寫一個set-user-id的程式來保護檔案 * 做法:檔案的全部權限只開自己的r,程式run set-uid,允許大家都來run,同時可以在程式裡面偵測process id是否為朋友的id,如果是就可以執行,不是則否。 舉例3:(舊期中考題) 有個老師在睡前打好成績的資料,發現檔案不小心開了other w權限,立刻改掉,並且重新對照一次成績發現成績無誤,但隔天起來發現有人的成績被改了。 * w權限打開代表可以open for writing,因此學生可以open for writing,但是沒有改檔案,這裡老師不會發現有任何改動,同時檔案的權限被改了,但學生仍然可以改檔案,因為File Access Test是在檔案open的那一刻就做的,不會在後面的每一步驟都檢查權限。學生是在w權限下open for writing成功,代表open file table中file status是open for writing的權限。 Access檔案要看的是effective uid File Access Test: * effective uid == 0 : superuser * effective uid == UID of the file : check user(owner)的權限,如果user的權限對了就可以做,user沒權限就會直接deny * effective gid == any of its supplementary gid : check group的權限,group權限對了就可以做,group沒權限就會直接deny * check other的權限,other權限對了就可以做,other沒權限就會直接deny 一個檔案被create了,他的owner看的是effective UID * uid of a file = effective uid of the creating process * gid of a file: * = the effective gid of the process * = gid of directory in which it is being created * 4.3BSD and FIPS 151-1 * SVR4 need to set the set-gid bit of the residing dir ```int access(const char *pathname, int mode)``` * access會用在借權力給別人的時候 * access : 檢查real uid對這個檔案(pathname)有沒有mode的權限 * 雖然權限借出去,但是對方只能做你的程式允許做的事 umask是一個避免犯錯的機制 process內部有個變數儲存目前所在的工作目錄,還有另一個變數記umask,umask會關掉設定的權限 umask(0)會回傳原本設定的umask值,先儲存起來,然後就可以create指定權限的file,再把umask設回去,這樣就可以在特定umask下建指定權限的檔案(例如大公司) 當process生小孩,小孩會繼承parent的umask,改小孩的umask跟parent無關,反之亦然 舉個例,在shell中,因為umask是shell看得懂的指令,因此直接呼叫umask是詢問**shell**這隻process的umask,當執行一個會更改umask的process時,shell會fork一個child process去執行a.out,這時child會繼承parent的umask,而a.out更改的是child的umask,與parent無關,因此對原本的shell不會更改umask。 為甚麼umask會是一個built-in command? Ans:如果要改的是shell的umask,但umask是個執行檔,shell會fork一個child process去執行umask,但child改的umask跟parent無關,因此umask如果是個執行檔,會沒有作用,必須是個built-in command。 ## part 3 期中似乎不考? Networking(ch16) 接口不是一個fd是一個socket,會記在open file descripter table裡面。 layer: * physical layer就是實體的網路連線 * link layer指的是在同一個link上的node,最基本的就是點對點,兩個點之間有一條連線 * network layer是指點對點互相連接,會形成一個graph,graph中任何兩個沒有直接連線的點可以透過間接的好幾個點連到對方去,就會形成一個network * transport layer代表我要control我連線到哪個level protocal:兩個人對話要有一個協定來規範怎麼對話 封包從socket出去,會先包transport layer的protocal(前後加一些information),再包network layer的protocal,一層一層包,包完在physical layer丟出去,接到了之後就會從最外面包的protocal開始拆。 IP: * pointer to pointer * ip會identify世界上唯一一台電腦 * ip在送的時候是unrealiable,封包掉了就掉了,不會通知封包掉了 * 如果中間網路fail掉了,或是router的buffer被塞滿了,會直接丟掉封包 * 送封包不一定會按照同一條路徑 * 封包會被切成fragment送出去,根據不同的路徑被送出去 TCP & UDP: * process to process * unix一個port就會對應到一個process,因此要bind一個port,process會跟unix講說我要associate到哪個port,如果成功了,別人就能透過那個port來對談 * TCP在送是realiable的(像一個電信公司),會從固定一條路徑傳訊息,如果中間有封包掉了,TCP會要求重新傳一份。 * UDP沒有固定的路徑傳訊,但是比如在系館要看一部影片,server在二樓機房,就不用用到TCP,因為掉封包的機率太低,用太powerful的protocal太浪費資源。 Port Number: * 一個machine有固定的port number * process結束的時候要把port number release,不然在執行第二遍的時候會bind失敗。 * TCP就是IP address + port number,兩邊都要有才是完整的TCP/IP * UNIX中<1023的port number是root only * server會listen在一個well-known的port,當要到一個socket之後要bind到某個特定的port,就可以開始listen,要小心forever block 後面都在講有關於作業一的部分了:>
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up