黃雅琳
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 1082, lsa --- - Book mode https://hackmd.io/@ncnu-opensource/By4H6JLNW [TOC] # What is ==container== ? ## 補充 VM 背景 > FIXME: 資源管理、動態調配 > FIXME: 只比時體積好一些 前後被 > FIXME: linux kernel(基本東西讓硬體可以run) 搭配工具軟體-> 能使用的os - 為了解決**環境移植**仍能讓程式正程執行的解決方法 - 虛擬出整個 OS 環境,來**隔離**執行環境 - 屬於硬體抽象層等級的虛擬化 - 優點 - 軟硬體分離 - 安全性高 - 缺點 - 較慢(建制、啟動速度單位:分鐘) - 較肥(佔用空間單位:GB) ## 談談 container 背景 - 改善了 VM 肥慢的問題 - 多個 container 與 Host 共用同一個==kernel== - **不須**虛擬出整個 OS 環境 - 屬於[OS 層的虛擬化技術](https://blog.trendmicro.com.tw/?p=60814) > FIXME: ::: info 小結: VM 虛擬的單位是整個 OS container 虛擬的單位是 process ::: ## So... what is container ? - 打包了一應用程式執行所需的任何東西 ex: 執行環境、函式庫... - 運行在**共用**的 **Host kernel** 上 - 優點 - 較快(佈署速度單位:秒) - 較瘦(佔用空間單位:MB) - CI/CD 敏捷開發(持續整合/持續交付) - 管理軟體更新較容易,只須用 image 重啟 - 缺點 - 若有安全性問題影響範圍比較大,因為人家 VM 隔離措施做的比較完整 - 對 **Host kernel** 的依賴性比較高 - container 彼此溝通需要透過的網路,網路設定較複雜 ### 比較 VM 與 container [![](https://i.imgur.com/7H9EwUg.png)](https://blog.yowko.com/container/) ※Container 支援的 OS 是需要和 Host OS **相同 kernel** ||VM|container| |---|---|---| |特點|安全性較高|較效率、較輕量| |適合情境|需要完整os的環境、比起速度更要求安全的應用|輕量化的服務與軟體佈署| ||須管理、更動系統底層的應用|移植性更高的應用情境| |啟動程序|要呼叫開機程序...|只呼叫執行檔| 補充舉例 還是使用者(給vm比較好用、也可使用container)給公司(佈署服務) ## 如何實現把 host OS 的資源做隔離 ? ![](https://i.imgur.com/JCVC15B.png) ![](https://i.imgur.com/i9GULVq.png) ### namespace - a type of namespace - a particular space of names - ### cgroup - 全名 control group - 將 process 分組管理 - 管理資源分配 ex I/O 使用權限、記憶體區塊分配... ![](https://i.imgur.com/igWmju7.png) - 以 group 為單位去設定 process 的存取權限、分配到的資源 - 設定檔掛在 `/sys/fs/cgroup` 下 [![](https://i.imgur.com/SuC2Owk.png)](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/428122/) - `cat /proc/cgroups` 可以看有哪些子系統有掛上去 - `cat /proc/${pid}/cgroup`可以看此 pid 屬於哪個 cgroup - 層級管理的概念 like 繼承父類別 ### chroot > FIXME: container 實現安全 - 將當前這個檔案設成根目錄 - 意即它往上慨不到其他東西了 # SElinux and AppArmor ## 補充 LSM ### Linux Security Modules (Linux 安全模組) - 用途 - 限制 subject 存取或操作 object 的能力(權限賦予) - 架構 - subject 主體 - ex: 使用者、 process 、 thread - object 物件 - ex: 檔案、目錄、TCP/UDP埠、共享記憶體段、I/O裝置 - policy 規則 - 任何 subject 對任何 object 的操作都需要有一組 policy 授權才行 - 比喻: 貓狗 & 貓糧狗糧 - 存取模式 - MAC (強制存取控制) - policy 由OS kernel 集中管理並強制執行 - DAC (自由選定存取控制) - 使用者可以自行改 policy 或分配安全屬性 - ex: 檔案權限 rwx - 缺點 - 管理員無法控制使用者 - ex: 使用者自己把敏感檔案( ~/.ssh/ )設成任何人皆可讀取的權限 - process 可更改安全性屬性 - ex: 寫程式把私人郵件改成大家都能讀取 - process 繼承使用者的權限 - ex: 在瀏覽器植入木馬程式看私人的ssh金鑰 - 官方Linux核心中支援的安全模組 - **AppArmor** - **SELinux** - Smack - TOMOYO Linux - Yama - 系統預設的 LSM - Ubuntu、OpenSUSE、SUSE - **AppArmor** - CentOS、RHEL、Fedora - **SELinux** ## SElinux ### SElinux 是什麼? [wiki](https://wiki.centos.org/zh-tw/HowTos/SELinux) - 全名 Security-Enhanced Linux (安全增強式) - 一個 Linux 核心安全模組 - 一個在 kernel 中實踐的強制存取控制(MAC)安全性機制 ### 三種模式 ||中文|SELinux 會被啟用|policy 會被執行| |---|---|---|---| |Enforcing(預設)|強制模式|v|v| |Permissive|寬容模式|v|會檢查 policy 和紀錄相關訊息,但不執行(打小報告但不阻止)| |Disabled|停用模式||| ### 指令語法 1. 查詢當前模式 ``` getenforce ``` - 查詢 SELinux 更詳細的資訊 ``` sestatus ``` ## AppArmor ![](https://i.imgur.com/GOCxu7D.png) ### AppArmor 是什麼? [wiki](https://zh.wikipedia.org/wiki/AppArmor) - 全名 Application Armor (應用盔甲) - 一個 Linux 核心安全模組 - 允許系統管理員通過每個 process 的設定檔來限制 process 的功能 - AppArmor 是一個對核心的增強工具,將程式限制在一個有限的資源集合中 - AppArmor 控制的主體是==程式==不是使用者 ### 兩種模式 ||中文|policy 會被執行| |---|---|---| |Enforcing|執行模式|v| |Complain|學習模式|以「學習」模式載入的設定檔不會執行 policy ,但會向 syslogd 打小報告| - 對比一下 SElinux 和 AppArmor 的模式 |SElinux|AppArmor| |---|---|---| |Enforcing|Enforcing| |Permissive|Complain| |Disabled|| ### 配置檔案放在哪個目錄下 - /etc/apparmor.d/ ### [一些 AppArmor 常用的操作](https://help.ubuntu.com/lts/serverguide/apparmor.html) > FIXME :指令都可以執行但怎麼知道在哪裡有變化 1. 先裝個管理套件包 (有工具、profile...可用) ``` sudo apt install apparmor-profiles ``` - /etc/apparmor.d/ 的 before & after ![](https://i.imgur.com/T5Tub1G.png) ![](https://i.imgur.com/BjCogLc.png) 2. 查詢當前狀態: 總共有幾個 profile 檔案, 哪幾個是enforce mode, 哪幾個是 complain mode ``` sudo apparmor_status ``` ![](https://i.imgur.com/mdt4wPP.png) ![](https://i.imgur.com/E3JcN6y.png) 3. 設定 proflie 模式: - 把一個 profile run 在 enforce mode 或 complain mode 上 ``` sudo aa-enforce <profile_name> sudo aa-complain <profile_name> ``` - 若想把 /etc/apparmor.d/ 底下所有 profile 都改 mode ``` sudo aa-complain /etc/apparmor.d/* sudo aa-enforce /etc/apparmor.d/* ``` - 用 ==apparmor_parser== 把 profile 加載到 kernel - -a 加載 - -r 重新加載 ``` cat /etc/apparmor.d/<profile_name> | sudo apparmor_parser -a cat /etc/apparmor.d/<profile_name> | sudo apparmor_parser -r ``` 4. 改完 profile 都要記得 reload 一下服務 ``` sudo systemctl reload apparmor.service ``` 5. 若想 disable profile - 不用 rm profile。建個鏈結去 /etc/apparmor.d/disable/ 底下,再用 -R 去 disable profile ``` sudo ln -s /etc/apparmor.d/<profile.name> /etc/apparmor.d/disable/ sudo apparmor_parser -R /etc/apparmor.d/<profile.name> ``` - 若想 enable profile 則 rm 鏈結,再用 -a 去 enable profile ``` sudo rm /etc/apparmor.d/disable/profile.name cat /etc/apparmor.d/<profile.name> | sudo apparmor_parser -a ``` 6. 若想啟用、停止 apparmor.service & 卸載、加入核心模組 - 停止服務、卸載模組 ``` sudo systemctl stop apparmor.service sudo update-rc.d -f apparmor remove ``` - 啟用服務、加載模組 ``` sudo systemctl start apparmor.service sudo update-rc.d apparmor defaults ``` - 還有另一種方式啟用、停用服務 ``` /etc/init.d/apparmor start /etc/init.d/apparmor stop /etc/init.d/apparmor restart ``` > FIXME: 他們再做什麼流程kermel 0 - [/etc/init.d 和 service 指令的差別](https://wiki.debian.org/Debate/initsystem/upstart) - SysV init (舊的系統啟動方式) - Upstart - systemd 7. 怎麼自己建一個 proflie? - aa-genprof 可以幫你的可執行檔生一個 proflie ``` sudo aa-genprof slapd ``` - (補充) aa-logprof 可以查看 log 紀錄 - [文件內容大概長這樣](http://www.361way.com/apparmor-selinux/3648.html) - 想知道更多->[AppArmor官網](https://wiki.ubuntu.com/AppArmor) 8. (補充)[套件 apparmor_parser 的語法架構](http://manpages.ubuntu.com/manpages/bionic/man8/apparmor_parser.8.html#privileged%20profile%20commands) ``` apparmor_parser [options] <command> [profiles]... apparmor_parser [options] <command> apparmor_parser [-hv] [--help] [--version] ``` #### command #### options ## SElinux 和 AppArmor 的異同 ### 共同點 - 都使用 LSM 框架增強 Linux 核心安全性 - 都生成 log 檔、提供看 log 的工具 - 都在應用程式層內工作 - 都有 GUI 與非 GUI 工具 - 都允許管理員在沒有真正阻止存取的情況下嘗試策略(而只是警告),以便僅在足夠數量的測試之後才應用安全加固策略 ### 不同點 || SElinux | AppArmor | |---|---|---| | 安全標籤 | *檔案的 ==inode==* | *檔案的 ==路徑==* | - inode - 類Unix檔案系統中的一種資料結構 - 用來描述檔案系統裡的 object (物件) - ex: 檔案、目錄... - 每個 inode (object) 都有自己的屬性(ex: 權限...)、磁碟塊位置 - 具唯一性 - 舉例: 如果給一個無法存取的檔案建立了硬連結 - 在 AppArmor 可以存取那個硬連結 - 在 SELinux 仍會拒絕存取。因為 inode 參照的基礎資料一樣,所以 policy 仍然管的到 ## SElinux 和 AppArmor 優缺點比較 ### SElinux - 優點 - 安全性較高 - 因 inode 在檔案系統裡具唯一性,所以不會因修改檔名而被繞過 policy - 缺點 - 太複雜,不適合一般使用者 - 需要有支援「安全標籤」的檔案系統 - 因此無法為通過 NFS (網路檔案系統) 掛載的檔案提供存取控制 ### AppArmor - 優點 - 易用性: 程式碼可讀性高、寫起來不用像SElinux要寫很長的程式 - 缺點 - 修改檔案名就能繞過 policy - 只能對已知動作約束,對於未知的管不到 - 技術文件較少,較難找到解決方案參考 || SElinux | AppArmor | |---|---|---| |優點|安全性較高|易用性| |缺點|太複雜|policy 能被繞過|

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    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

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully