在apparmor配置文件下加载Django应用程序的设置

时间:2015-02-11 02:12:55

标签: python linux django security apparmor

所以我们有一个需要apparmor配置文件的django应用程序(客户端授权)。它在位于/opt/fact-virtual-environment的Python虚拟环境中运行。应用程序本身会安装到/usr/share/fact

以下是我到目前为止的个人资料(主要使用aa-genprof工具构建):

# Last Modified: Tue Jan 20 09:25:09 2015
#include <tunables/global>

/usr/bin/fact flags=(audit) {
  #include <abstractions/apache2-common>
  #include <abstractions/base>
  #include <abstractions/bash>


  capability setgid,
  capability sys_resource,

  /usr/local/lib/python2.7/dist-packages/**/ rm,
  /usr/local/lib/python2.7/dist-packages/** rm,
  /usr/local/lib/python2.7/site-packages/**/ rm,
  /usr/local/lib/python2.7/site-packages/** rm,
  /etc/apparmor.d/** r,
  #/usr/lib/python2.7/**[^/] r,

  /usr/bin/sudo Ux,

  #/usr/bin/strace ux,
  /bin/bash rix,
  /bin/dash rix,
  /bin/uname rix,
  /dev/tty rw,
  /etc/default/locale r,
  #/etc/nsswitch.conf r,
  #/etc/group r,
  #/etc/passwd r,
  /etc/environment r,
  /etc/login.defs r,
  /etc/lsb-release r,
  /etc/fact/fact.ini r,
  /etc/python2.7/sitecustomize.py r,
  /etc/security/pam_env.conf r,
  /lib{,32,64}/** mr,
  /opt/fact-virtual-environment/**/ mr,
  /opt/fact-virtual-environment/lib/python2.7/**/ mr,
  /opt/fact-virtual-environment/lib/python2.7/** mr,
  /opt/fact-virtual-environment/bin/python rix,
  /run/utmp rk,
  /sbin/ldconfig rix,
  /sbin/ldconfig.real rix,
  /usr/bin/fact rix,
  /usr/lib{,32,64}/** mr,
  /usr/share/fact/**/ r,
  /usr/share/fact/** r,
  /usr/share/pyshared/** r,
  /usr/share/pyshared/**/ r,
}

...而/usr/bin/fact命令是一个简单的包装器:

#!/bin/bash

## This script is for running the 'fact' command on staging/prod, it sudos to
## the 'fact' user before executing /opt/fact/bin/fact

## It is installed as '/usr/bin/fact'

PYTHONPATH=/usr/share/fact
PYTHON_BIN=/opt/fact-virtual-environment/bin/python
DJANGO_SETTINGS_MODULE=fact.settings.staging

if [ "$USER" != "fact" ];
then
  sudo -u fact $0 $*;
else
  PYTHONPATH=${PYTHONPATH} DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE}  ${PYTHON_BIN} -m fact.managecommand $*;
fi

我已经启用了很多读取权限,我知道我不需要,就像在调试尝试中设置的整个/usr/local/lib/*一样。关于这件事情让我感到震惊的是,当我运行service apparmor reload(或重新启动)时,我的个人资料似乎无法重新加载。如果我重新启动服务而不是aa-complain usr.bin.fact后跟aa-enforce usr.bin.fact,我似乎会得到不同的结果。

现在,当我运行命令并检查系统日志时,我看到以下内容:

Feb 11 15:08:48 spiffy kernel: [1228245.774660] type=1400 audit(1423620528.359:14142): apparmor="DENIED" operation="open" parent=12884 profile="/usr/bin/fact" name="/usr/local/lib/python2.7/site-packages/" pid=12885 comm="python" requested_mask="r" denied_mask="r" fsuid=111 ouid=0
Feb 11 15:08:48 spiffy kernel: [1228245.774855] type=1400 audit(1423620528.359:14143): apparmor="DENIED" operation="open" parent=12884 profile="/usr/bin/fact" name="/usr/local/lib/python2.7/dist-packages/" pid=12885 comm="python" requested_mask="r" denied_mask="r" fsuid=111 ouid=0
Feb 11 15:08:48 spiffy kernel: [1228245.775829] type=1400 audit(1423620528.359:14144): apparmor="DENIED" operation="open" parent=12884 profile="/usr/bin/fact" name="/usr/local/lib/python2.7/dist-packages/" pid=12885 comm="python" requested_mask="r" denied_mask="r" fsuid=111 ouid=0

..和我的python应用程序barfs:

$ fact
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/share/fact/fact/managecommand.py", line 6, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'fact.settings.staging' (Is it on sys.path? Is there an import error in the settings file?): cannot import name current_app

它正在寻找的设置文件位于/usr/share/fact/fact/settings/staging.py

我完全不知道为什么我无法让这个应用程序在其apparmor配置文件下运行。配置文件中的设置应该允许它。发生了什么事?

2 个答案:

答案 0 :(得分:2)

您的个人资料缺少阅读目录的权限   /usr/local/lib/python2.7/site-packages /

你需要添加   /usr/local/lib/python2.7/site-packages/ r,

您当前个人资料允许访问/usr/local/lib/python2.7/site-packages/下所有内容的权限,但不能实际访问目录本身

答案 1 :(得分:1)

所有拒绝都在寻找此特定目录:/usr/local/lib/python2.7/dist-packages/

您的个人资料中包含此目录中的一些孩子:

/usr/local/lib/python2.7/dist-packages/**/ rm,
/usr/local/lib/python2.7/dist-packages/** rm,
/usr/local/lib/python2.7/site-packages/**/ rm,
/usr/local/lib/python2.7/site-packages/** rm,

但它不包括此特定目录。因此,请将此添加到您的个人资料中,重新加载个人资料,然后重试:

/usr/local/lib/python2.7/dist-packages/ r,

AppArmor使用尾随/来确定管理员是否打算允许获取目录列表而不是允许读取文件。

由于