attach: use __do_free

parent 83c11f1d
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
#include "lxcseccomp.h" #include "lxcseccomp.h"
#include "macro.h" #include "macro.h"
#include "mainloop.h" #include "mainloop.h"
#include "memory_utils.h"
#include "namespace.h" #include "namespace.h"
#include "raw_syscalls.h" #include "raw_syscalls.h"
#include "syscall_wrappers.h" #include "syscall_wrappers.h"
...@@ -76,31 +77,28 @@ static lxc_attach_options_t attach_static_default_options = LXC_ATTACH_OPTIONS_D ...@@ -76,31 +77,28 @@ static lxc_attach_options_t attach_static_default_options = LXC_ATTACH_OPTIONS_D
static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid) static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
{ {
__do_free char *line = NULL;
__do_fclose FILE *proc_file = NULL;
int ret; int ret;
bool found; bool found;
FILE *proc_file;
char proc_fn[LXC_PROC_STATUS_LEN]; char proc_fn[LXC_PROC_STATUS_LEN];
struct lxc_proc_context_info *info;
size_t line_bufsz = 0; size_t line_bufsz = 0;
char *line = NULL;
struct lxc_proc_context_info *info = NULL;
/* Read capabilities. */ /* Read capabilities. */
ret = snprintf(proc_fn, LXC_PROC_STATUS_LEN, "/proc/%d/status", pid); ret = snprintf(proc_fn, LXC_PROC_STATUS_LEN, "/proc/%d/status", pid);
if (ret < 0 || ret >= LXC_PROC_STATUS_LEN) if (ret < 0 || ret >= LXC_PROC_STATUS_LEN)
goto on_error; return NULL;
proc_file = fopen(proc_fn, "r"); proc_file = fopen(proc_fn, "r");
if (!proc_file) { if (!proc_file) {
SYSERROR("Could not open %s", proc_fn); SYSERROR("Failed to open %s", proc_fn);
goto on_error; return NULL;
} }
info = calloc(1, sizeof(*info)); info = calloc(1, sizeof(*info));
if (!info) { if (!info)
SYSERROR("Could not allocate memory");
fclose(proc_file);
return NULL; return NULL;
}
found = false; found = false;
...@@ -112,13 +110,10 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid) ...@@ -112,13 +110,10 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
} }
} }
free(line);
fclose(proc_file);
if (!found) { if (!found) {
ERROR("Could not read capability bounding set from %s", ERROR("Could not read capability bounding set from %s", proc_fn);
proc_fn); free(info);
goto on_error; return NULL;
} }
info->lsm_label = lsm_process_label_get(pid); info->lsm_label = lsm_process_label_get(pid);
...@@ -126,10 +121,6 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid) ...@@ -126,10 +121,6 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
memset(info->ns_fd, -1, sizeof(int) * LXC_NS_MAX); memset(info->ns_fd, -1, sizeof(int) * LXC_NS_MAX);
return info; return info;
on_error:
free(info);
return NULL;
} }
static inline void lxc_proc_close_ns_fd(struct lxc_proc_context_info *ctx) static inline void lxc_proc_close_ns_fd(struct lxc_proc_context_info *ctx)
...@@ -440,13 +431,14 @@ static int lxc_attach_set_environment(struct lxc_proc_context_info *init_ctx, ...@@ -440,13 +431,14 @@ static int lxc_attach_set_environment(struct lxc_proc_context_info *init_ctx,
static char *lxc_attach_getpwshell(uid_t uid) static char *lxc_attach_getpwshell(uid_t uid)
{ {
__do_free char *line = NULL;
__do_fclose FILE *pipe_f = NULL;
int fd, ret; int fd, ret;
pid_t pid; pid_t pid;
int pipes[2]; int pipes[2];
FILE *pipe_f;
bool found = false; bool found = false;
size_t line_bufsz = 0; size_t line_bufsz = 0;
char *line = NULL, *result = NULL; char *result = NULL;
/* We need to fork off a process that runs the getent program, and we /* We need to fork off a process that runs the getent program, and we
* need to capture its output, so we use a pipe for that purpose. * need to capture its output, so we use a pipe for that purpose.
...@@ -567,9 +559,6 @@ static char *lxc_attach_getpwshell(uid_t uid) ...@@ -567,9 +559,6 @@ static char *lxc_attach_getpwshell(uid_t uid)
found = true; found = true;
} }
free(line);
fclose(pipe_f);
ret = wait_for_pid(pid); ret = wait_for_pid(pid);
if (ret < 0) { if (ret < 0) {
free(result); free(result);
...@@ -586,10 +575,10 @@ static char *lxc_attach_getpwshell(uid_t uid) ...@@ -586,10 +575,10 @@ static char *lxc_attach_getpwshell(uid_t uid)
static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid) static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
{ {
FILE *proc_file; __do_free char *line = NULL;
__do_fclose FILE *proc_file = NULL;
char proc_fn[LXC_PROC_STATUS_LEN]; char proc_fn[LXC_PROC_STATUS_LEN];
int ret; int ret;
char *line = NULL;
size_t line_bufsz = 0; size_t line_bufsz = 0;
long value = -1; long value = -1;
uid_t uid = (uid_t)-1; uid_t uid = (uid_t)-1;
...@@ -620,9 +609,6 @@ static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid) ...@@ -620,9 +609,6 @@ static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
break; break;
} }
fclose(proc_file);
free(line);
/* Only override arguments if we found something. */ /* Only override arguments if we found something. */
if (uid != (uid_t)-1) if (uid != (uid_t)-1)
*init_uid = uid; *init_uid = uid;
...@@ -637,9 +623,9 @@ static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid) ...@@ -637,9 +623,9 @@ static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options) static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options)
{ {
__do_free char *path = NULL;
int ret; int ret;
bool bret; bool bret;
char *path;
if (!(options->namespaces & CLONE_NEWNS) || if (!(options->namespaces & CLONE_NEWNS) ||
!(options->attach_flags & LXC_ATTACH_LSM)) { !(options->attach_flags & LXC_ATTACH_LSM)) {
...@@ -667,7 +653,6 @@ static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options ...@@ -667,7 +653,6 @@ static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options
/* Copy the value into the new lxc_conf. */ /* Copy the value into the new lxc_conf. */
bret = c->set_config_item(c, "lxc.seccomp.profile", path); bret = c->set_config_item(c, "lxc.seccomp.profile", path);
free(path);
if (!bret) if (!bret)
return false; return false;
...@@ -684,8 +669,7 @@ static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options ...@@ -684,8 +669,7 @@ static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options
static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options) static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options)
{ {
bool bret; __do_free char *val = NULL;
char *val;
/* Remove current setting. */ /* Remove current setting. */
if (!c->set_config_item(c, "lxc.no_new_privs", "")) { if (!c->set_config_item(c, "lxc.no_new_privs", "")) {
...@@ -701,24 +685,18 @@ static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options) ...@@ -701,24 +685,18 @@ static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options)
} }
/* Set currently active setting. */ /* Set currently active setting. */
bret = c->set_config_item(c, "lxc.no_new_privs", val); return c->set_config_item(c, "lxc.no_new_privs", val);
free(val);
return bret;
} }
static signed long get_personality(const char *name, const char *lxcpath) static signed long get_personality(const char *name, const char *lxcpath)
{ {
char *p; __do_free char *p;
signed long ret;
p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath); p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath);
if (!p) if (!p)
return -1; return -1;
ret = lxc_config_parse_arch(p); return lxc_config_parse_arch(p);
free(p);
return ret;
} }
struct attach_clone_payload { struct attach_clone_payload {
...@@ -1547,11 +1525,11 @@ int lxc_attach_run_command(void *payload) ...@@ -1547,11 +1525,11 @@ int lxc_attach_run_command(void *payload)
int lxc_attach_run_shell(void* payload) int lxc_attach_run_shell(void* payload)
{ {
__do_free char *buf = NULL;
uid_t uid; uid_t uid;
struct passwd pwent; struct passwd pwent;
struct passwd *pwentp = NULL; struct passwd *pwentp = NULL;
char *user_shell; char *user_shell;
char *buf;
size_t bufsize; size_t bufsize;
int ret; int ret;
...@@ -1598,6 +1576,5 @@ int lxc_attach_run_shell(void* payload) ...@@ -1598,6 +1576,5 @@ int lxc_attach_run_shell(void* payload)
if (!pwentp) if (!pwentp)
free(user_shell); free(user_shell);
free(buf);
return -1; return -1;
} }
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