]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lsm/lsm.c
2 * lxc: linux Container library
5 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>
6 * Copyright © 2012 Canonical Ltd.
7 * Dwight Engen <dwight.engen@oracle.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/mount.h>
29 #include <sys/param.h>
35 lxc_log_define(lxc_lsm
, lxc
);
37 static struct lsm_drv
*drv
= NULL
;
39 extern struct lsm_drv
*lsm_apparmor_drv_init(void);
40 extern struct lsm_drv
*lsm_selinux_drv_init(void);
41 extern struct lsm_drv
*lsm_nop_drv_init(void);
43 __attribute__((constructor
))
47 INFO("LSM security driver %s", drv
->name
);
52 drv
= lsm_apparmor_drv_init();
56 drv
= lsm_selinux_drv_init();
60 drv
= lsm_nop_drv_init();
61 INFO("Initialized LSM security driver %s", drv
->name
);
67 return drv
->enabled();
71 const char *lsm_name(void)
78 char *lsm_process_label_get(pid_t pid
)
81 ERROR("LSM driver not inited");
84 return drv
->process_label_get(pid
);
87 int lsm_process_label_fd_get(pid_t pid
, bool on_exec
)
92 char path
[LXC_LSMATTRLEN
];
96 if (strcmp(name
, "nop") == 0)
99 if (strcmp(name
, "none") == 0)
102 /* We don't support on-exec with AppArmor */
103 if (strcmp(name
, "AppArmor") == 0)
107 ret
= snprintf(path
, LXC_LSMATTRLEN
, "/proc/%d/attr/exec", pid
);
109 ret
= snprintf(path
, LXC_LSMATTRLEN
, "/proc/%d/attr/current", pid
);
110 if (ret
< 0 || ret
>= LXC_LSMATTRLEN
)
113 labelfd
= open(path
, O_RDWR
);
115 SYSERROR("Unable to %s LSM label file descriptor", name
);
122 int lsm_process_label_set_at(int label_fd
, const char *label
, bool on_exec
)
129 if (strcmp(name
, "nop") == 0)
132 if (strcmp(name
, "none") == 0)
135 /* We don't support on-exec with AppArmor */
136 if (strcmp(name
, "AppArmor") == 0)
139 if (strcmp(name
, "AppArmor") == 0) {
144 ERROR("Changing AppArmor profile on exec not supported");
148 len
= strlen(label
) + strlen("changeprofile ") + 1;
149 command
= malloc(len
);
153 ret
= snprintf(command
, len
, "changeprofile %s", label
);
154 if (ret
< 0 || (size_t)ret
>= len
) {
159 ret
= lxc_write_nointr(label_fd
, command
, len
- 1);
161 } else if (strcmp(name
, "SELinux") == 0) {
162 ret
= lxc_write_nointr(label_fd
, label
, strlen(label
));
167 SYSERROR("Failed to set %s label \"%s\"", name
, label
);
171 INFO("Set %s label to \"%s\"", name
, label
);
175 int lsm_process_label_set(const char *label
, struct lxc_conf
*conf
,
176 bool use_default
, bool on_exec
)
179 ERROR("LSM driver not inited");
182 return drv
->process_label_set(label
, conf
, use_default
, on_exec
);