lsm: add lsm_process_label_fd_get()

parent e6e89974
...@@ -88,44 +88,6 @@ ...@@ -88,44 +88,6 @@
lxc_log_define(lxc_attach, lxc); lxc_log_define(lxc_attach, lxc);
/* /proc/pid-to-str/current\0 = (5 + 21 + 7 + 1) */
#define __LSMATTRLEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
static int lsm_open(pid_t pid, int on_exec)
{
const char *name;
char path[__LSMATTRLEN];
int ret = -1;
int labelfd = -1;
name = lsm_name();
if (strcmp(name, "nop") == 0)
return 0;
if (strcmp(name, "none") == 0)
return 0;
/* We don't support on-exec with AppArmor */
if (strcmp(name, "AppArmor") == 0)
on_exec = 0;
if (on_exec)
ret = snprintf(path, __LSMATTRLEN, "/proc/%d/attr/exec", pid);
else
ret = snprintf(path, __LSMATTRLEN, "/proc/%d/attr/current", pid);
if (ret < 0 || ret >= __LSMATTRLEN)
return -1;
labelfd = open(path, O_RDWR);
if (labelfd < 0) {
SYSERROR("%s - Unable to open file descriptor to set LSM label",
strerror(errno));
return -1;
}
return labelfd;
}
static int lsm_set_label_at(int lsm_labelfd, int on_exec, char *lsm_label) static int lsm_set_label_at(int lsm_labelfd, int on_exec, char *lsm_label)
{ {
int fret = -1; int fret = -1;
...@@ -1396,11 +1358,12 @@ int lxc_attach(const char *name, const char *lxcpath, ...@@ -1396,11 +1358,12 @@ int lxc_attach(const char *name, const char *lxcpath,
if ((options->namespaces & CLONE_NEWNS) && if ((options->namespaces & CLONE_NEWNS) &&
(options->attach_flags & LXC_ATTACH_LSM) && (options->attach_flags & LXC_ATTACH_LSM) &&
init_ctx->lsm_label) { init_ctx->lsm_label) {
int labelfd, on_exec;
int ret = -1; int ret = -1;
int labelfd;
bool on_exec;
on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? 1 : 0; on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false;
labelfd = lsm_open(attached_pid, on_exec); labelfd = lsm_process_label_fd_get(attached_pid, on_exec);
if (labelfd < 0) if (labelfd < 0)
goto close_mainloop; goto close_mainloop;
TRACE("Opened LSM label file descriptor %d", labelfd); TRACE("Opened LSM label file descriptor %d", labelfd);
......
...@@ -85,6 +85,42 @@ char *lsm_process_label_get(pid_t pid) ...@@ -85,6 +85,42 @@ char *lsm_process_label_get(pid_t pid)
return drv->process_label_get(pid); return drv->process_label_get(pid);
} }
int lsm_process_label_fd_get(pid_t pid, bool on_exec)
{
int ret = -1;
int labelfd = -1;
const char *name;
char path[LXC_LSMATTRLEN];
name = lsm_name();
if (strcmp(name, "nop") == 0)
return 0;
if (strcmp(name, "none") == 0)
return 0;
/* We don't support on-exec with AppArmor */
if (strcmp(name, "AppArmor") == 0)
on_exec = 0;
if (on_exec)
ret = snprintf(path, LXC_LSMATTRLEN, "/proc/%d/attr/exec", pid);
else
ret = snprintf(path, LXC_LSMATTRLEN, "/proc/%d/attr/current", pid);
if (ret < 0 || ret >= LXC_LSMATTRLEN)
return -1;
labelfd = open(path, O_RDWR);
if (labelfd < 0) {
SYSERROR("%s - Unable to %s LSM label file descriptor",
name, strerror(errno));
return -1;
}
return labelfd;
}
int lsm_process_label_set(const char *label, struct lxc_conf *conf, int lsm_process_label_set(const char *label, struct lxc_conf *conf,
bool use_default, bool on_exec) bool use_default, bool on_exec)
{ {
......
...@@ -48,6 +48,7 @@ extern const char *lsm_name(void); ...@@ -48,6 +48,7 @@ extern const char *lsm_name(void);
extern char *lsm_process_label_get(pid_t pid); extern char *lsm_process_label_get(pid_t pid);
extern int lsm_process_label_set(const char *label, struct lxc_conf *conf, extern int lsm_process_label_set(const char *label, struct lxc_conf *conf,
bool use_default, bool on_exec); bool use_default, bool on_exec);
extern int lsm_process_label_fd_get(pid_t pid, bool on_exec);
#else #else
static inline void lsm_init(void) static inline void lsm_init(void)
{ {
...@@ -74,6 +75,11 @@ static inline int lsm_process_label_set(const char *label, ...@@ -74,6 +75,11 @@ static inline int lsm_process_label_set(const char *label,
{ {
return 0; return 0;
} }
static inline int lsm_process_label_fd_get(pid_t pid, bool on_exec)
{
return 0;
}
#endif #endif
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment