리눅스

본문 바로가기
사이트 내 전체검색


리눅스
리눅스

12. systemd PrivateTmp Full

페이지 정보

작성자 관리자 댓글 0건 조회 1,037회 작성일 22-02-09 18:23

본문

12. systemd PrivateTmp Full

# df -k

Filesystem     1K-blocks     Used Available Use% Mounted on

devtmpfs        32766044        0  32766044   0% /dev

tmpfs           32776736        0  32776736   0% /dev/shm

tmpfs           32776736   475868  32300868   2% /run

tmpfs           32776736        0  32776736   0% /sys/fs/cgroup

/dev/sda4       31441920 31441900        20 100% /

/dev/sda2        1038336   242208    796128  24% /boot

/dev/sda5      870157184 72459432 797697752   9% /home

/dev/sda1        1046516    11480   1035036   2% /boot/efi

tmpfs            6555348        0   6555348   0% /run/user/0

tmpfs            6555348        0   6555348   0% /run/user/1000



# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         32G     0   32G   0% /dev
tmpfs            32G     0   32G   0% /dev/shm
tmpfs            32G  465M   31G   2% /run
tmpfs            32G     0   32G   0% /sys/fs/cgroup
/dev/sda4        30G   30G   20K 100% /
/dev/sda2      1014M  237M  778M  24% /boot
/dev/sda5       830G   70G  761G   9% /home
/dev/sda1      1022M   12M 1011M   2% /boot/efi
tmpfs           6.3G     0  6.3G   0% /run/user/0
tmpfs           6.3G     0  6.3G   0% /run/user/1000


/ 파티션이 100%가 돼서 문제가 발생 하였다. 

문제를 찾다보니 systemd의 privateTemp의 로그 때문있었다.


[root@www var]# du -sk *

0       adm

129020  cache

0       crash

8       db

0       empty

0       games

0       gopher

0       kerberos

362712  lib

0       local

0       lock

179548  log

0       mail

0       nis

0       opt

0       preserve

0       run

32      spool

28616976        tmp

4       www

0       yp



systemd를 사용하는 리눅스에는 Private Temp를 적용시킬 수 있는 것 같습니다.


systemd의 서비스 구동 스크립트에 PrivateTmp=true 라고 되어있다면 /tmp 디렉토리에 systemd-private-XXXXXXXX 라는 하위 디렉토리를 생성 후, 서비스 실행 프로세스가 방금 systemd가 생성한 임시 디렉토리를 /tmp 로 인식하도록 마운트 시켜버립니다. 

마운트를 프로세스 개별적으로 적용이 가능한가 봅니다. 

findmnt -N <PID>로 확인 가능합니다.


이 기능의 좋은 점은 /tmp 디렉토리에 포괄적인 권한을 가진 파일을 생성하면 다른 계정의 사용자가 읽고 수정할 수 있지만 프로세스 각각 디렉토리를 배정해 줌으로써 충돌, 무단 수정을 불가능하게 됩니다.


예를들어 예전에 웹 해킹 문제로 나온 것 중 하나로 알고있는데, SQL Injection으로 PHP 세션파일을 생성시켜서 자신이 해당 쿠키를 사용하여 계정 탈취를 하는 방식이 있었습니다만 이 기능이 켜져있는 DBMS와 PHP 서비스를 사용중이라면 다른 서비스의 임시파일에 접근이 불가능하므로 이런 방식의 공격 자체가 불가능해 집니다.


mysql의 tmp 폴더를 확인합니다.


# ps axf


21465 ?        Ss     0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr

21641 ?        Sl     0:00  \_ /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql 



# findmnt -N 21465

TARGET                                SOURCE      FSTYPE      OPTIONS

/                                     /dev/sda4   xfs         rw,relatime,attr2,inode64,noquota

├─/dev                                devtmpfs    devtmpfs    rw,nosuid,size=32766044k,nr_inodes=8191511,mode=755

│ ├─/dev/shm                          tmpfs       tmpfs       rw,nosuid,nodev

│ ├─/dev/pts                          devpts      devpts      rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=0

│ ├─/dev/mqueue                       mqueue      mqueue      rw,relatime

│ └─/dev/hugepages                    hugetlbfs   hugetlbfs   rw,relatime

├─/proc                               proc        proc        rw,nosuid,nodev,noexec,relatime

│ └─/proc/sys/fs/binfmt_misc          systemd-1   autofs      rw,relatime,fd=30,pgrp=1,timeout=0,minproto=5,maxpr

│   └─/proc/sys/fs/binfmt_misc        binfmt_misc binfmt_misc rw,relatime

├─/sys                                sysfs       sysfs       rw,nosuid,nodev,noexec,relatime

│ ├─/sys/kernel/security              securityfs  securityfs  rw,nosuid,nodev,noexec,relatime

│ ├─/sys/fs/cgroup                    tmpfs       tmpfs       ro,nosuid,nodev,noexec,mode=755

│ │ ├─/sys/fs/cgroup/systemd          cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,xattr,release_agent

│ │ ├─/sys/fs/cgroup/pids             cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,pids

│ │ ├─/sys/fs/cgroup/devices          cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,devices

│ │ ├─/sys/fs/cgroup/net_cls,net_prio cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,net_prio,net_cls

│ │ ├─/sys/fs/cgroup/freezer          cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,freezer

│ │ ├─/sys/fs/cgroup/cpu,cpuacct      cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,cpuacct,cpu

│ │ ├─/sys/fs/cgroup/blkio            cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,blkio

│ │ ├─/sys/fs/cgroup/perf_event       cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,perf_event

│ │ ├─/sys/fs/cgroup/hugetlb          cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,hugetlb

│ │ ├─/sys/fs/cgroup/memory           cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,memory

│ │ └─/sys/fs/cgroup/cpuset           cgroup      cgroup      rw,nosuid,nodev,noexec,relatime,cpuset

│ ├─/sys/fs/pstore                    pstore      pstore      rw,nosuid,nodev,noexec,relatime

│ ├─/sys/firmware/efi/efivars         efivarfs    efivarfs    rw,nosuid,nodev,noexec,relatime

│ ├─/sys/kernel/config                configfs    configfs    rw,relatime

│ └─/sys/kernel/debug                 debugfs     debugfs     rw,relatime

├─/run                                tmpfs       tmpfs       rw,nosuid,nodev,mode=755

│ └─/run/user/1000                    tmpfs       tmpfs       rw,nosuid,nodev,relatime,size=6555348k,mode=700,uid

├─/boot                               /dev/sda2   xfs         rw,relatime,attr2,inode64,noquota

│ └─/boot/efi                         /dev/sda1   vfat        rw,relatime,fmask=0077,dmask=0077,codepage=437,ioch

├─/home                               /dev/sda5   xfs         rw,relatime,attr2,inode64,noquota

├─/tmp                                /dev/sda4[/tmp/systemd-private-7dc4fcde0ba549f58901216fa3960a50-mariadb.service-FR6pfm/tmp]

                                                  xfs         rw,relatime,attr2,inode64,noquota

└─/var/tmp                            /dev/sda4[/var/tmp/systemd-private-7dc4fcde0ba549f58901216fa3960a50-mariadb.service-updwAZ/tmp]

                                                  xfs         rw,relatime,attr2,inode64,noquota



mariadb systemd 서비스 파일을 확인해보겠습니다.



# cat /usr/lib/systemd/system/mariadb.service


# It's not recommended to modify this file in-place, because it will be

# overwritten during package upgrades.  If you want to customize, the

# best way is to create a file "/etc/systemd/system/mariadb.service",

# containing

#       .include /lib/systemd/system/mariadb.service

#       ...make your changes here...

# or create a file "/etc/systemd/system/mariadb.service.d/foo.conf",

# which doesn't need to include ".include" call and which will be parsed

# after the file mariadb.service itself is parsed.

#

# For more info about custom unit files, see systemd.unit(5) or

# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F


# For example, if you want to increase mariadb's open-files-limit to 10000,

# you need to increase systemd's LimitNOFILE setting, so create a file named

# "/etc/systemd/system/mariadb.service.d/limits.conf" containing:

#       [Service]

#       LimitNOFILE=10000


# Note: /usr/lib/... is recommended in the .include line though /lib/...

# still works.

# Don't forget to reload systemd daemon after you change unit configuration:

# root> systemctl --system daemon-reload


[Unit]

Description=MariaDB database server

After=syslog.target

After=network.target


[Service]

Type=simple

User=mysql

Group=mysql


ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n

# Note: we set --basedir to prevent probes that might trigger SELinux alarms,

# per bug #547485

ExecStart=/usr/bin/mysqld_safe --basedir=/usr

ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID


# Give a reasonable amount of time for the server to start up/shut down

TimeoutSec=300


# Place temp files in a secure directory, not /tmp

PrivateTmp=true


[Install]

WantedBy=multi-user.target

[root@lang tmpfiles.d]#



이 tmp파일은 자동으로 삭제됩니다. 기본값은 30일입니다. 10일로 수정하면 /(루트)파티션이 100% 차는 것을 막을 수 있다.


# cat /usr/lib/tmpfiles.d/tmp.conf


#  This file is part of systemd.

#

#  systemd is free software; you can redistribute it and/or modify it

#  under the terms of the GNU Lesser General Public License as published by

#  the Free Software Foundation; either version 2.1 of the License, or

#  (at your option) any later version.


# See tmpfiles.d(5) for details


# Clear tmp directories separately, to make them easier to override

v /tmp 1777 root root 10d

v /var/tmp 1777 root root 10d


# Exclude namespace mountpoints created with PrivateTmp=yes

x /tmp/systemd-private-%b-*

X /tmp/systemd-private-%b-*/tmp

x /var/tmp/systemd-private-%b-*

X /var/tmp/systemd-private-%b-*/tmp


댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.