]>
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
29 #include <sys/mount.h>
30 #include <sys/param.h>
38 lxc_log_define(lsm
, lxc
);
40 static struct lsm_drv
*drv
= NULL
;
42 extern struct lsm_drv
*lsm_apparmor_drv_init(void);
43 extern struct lsm_drv
*lsm_selinux_drv_init(void);
44 extern struct lsm_drv
*lsm_nop_drv_init(void);
46 __attribute__((constructor
))
50 INFO("LSM security driver %s", drv
->name
);
55 drv
= lsm_apparmor_drv_init();
59 drv
= lsm_selinux_drv_init();
63 drv
= lsm_nop_drv_init();
64 INFO("Initialized LSM security driver %s", drv
->name
);
70 return drv
->enabled();
74 const char *lsm_name(void)
81 char *lsm_process_label_get(pid_t pid
)
84 ERROR("LSM driver not inited");
87 return drv
->process_label_get(pid
);
90 int lsm_process_label_fd_get(pid_t pid
, bool on_exec
)
95 char path
[LXC_LSMATTRLEN
];
99 if (strcmp(name
, "nop") == 0)
102 if (strcmp(name
, "none") == 0)
105 /* We don't support on-exec with AppArmor */
106 if (strcmp(name
, "AppArmor") == 0)
110 ret
= snprintf(path
, LXC_LSMATTRLEN
, "/proc/%d/attr/exec", pid
);
112 ret
= snprintf(path
, LXC_LSMATTRLEN
, "/proc/%d/attr/current", pid
);
113 if (ret
< 0 || ret
>= LXC_LSMATTRLEN
)
116 labelfd
= open(path
, O_RDWR
);
118 SYSERROR("Unable to %s LSM label file descriptor", name
);
125 int lsm_process_label_set_at(int label_fd
, const char *label
, bool on_exec
)
132 if (strcmp(name
, "nop") == 0)
135 if (strcmp(name
, "none") == 0)
138 /* We don't support on-exec with AppArmor */
139 if (strcmp(name
, "AppArmor") == 0)
142 if (strcmp(name
, "AppArmor") == 0) {
147 ERROR("Changing AppArmor profile on exec not supported");
151 len
= strlen(label
) + strlen("changeprofile ") + 1;
152 command
= malloc(len
);
156 ret
= snprintf(command
, len
, "changeprofile %s", label
);
157 if (ret
< 0 || (size_t)ret
>= len
) {
158 int saved_errno
= errno
;
164 ret
= lxc_write_nointr(label_fd
, command
, len
- 1);
166 } else if (strcmp(name
, "SELinux") == 0) {
167 ret
= lxc_write_nointr(label_fd
, label
, strlen(label
));
174 SYSERROR("Failed to set %s label \"%s\"", name
, label
);
178 INFO("Set %s label to \"%s\"", name
, label
);
182 int lsm_process_label_set(const char *label
, struct lxc_conf
*conf
,
186 ERROR("LSM driver not inited");
189 return drv
->process_label_set(label
, conf
, on_exec
);
192 int lsm_process_prepare(struct lxc_conf
*conf
, const char *lxcpath
)
195 ERROR("LSM driver not inited");
202 return drv
->prepare(conf
, lxcpath
);
205 void lsm_process_cleanup(struct lxc_conf
*conf
, const char *lxcpath
)
208 ERROR("LSM driver not inited");
215 drv
->cleanup(conf
, lxcpath
);