Skip to content

Commit 86ca3df

Browse files
committed
lib: add chrooting execvp like function
1 parent 3ca652b commit 86ca3df

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

include/xbps_api_impl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ char HIDDEN *xbps_get_remote_repo_string(const char *);
103103
int HIDDEN xbps_repo_sync(struct xbps_handle *, const char *);
104104
int HIDDEN xbps_file_hash_check_dictionary(struct xbps_handle *,
105105
xbps_dictionary_t, const char *, const char *);
106-
int HIDDEN xbps_file_exec(struct xbps_handle *, const char *, ...);
106+
int HIDDEN xbps_file_exec(const struct xbps_handle *, const char *, ...);
107+
int HIDDEN xbps_file_exec_argv(const struct xbps_handle *xhp, const char **argv);
108+
int HIDDEN xbps_file_execp_argv(const struct xbps_handle *xhp, const char **argv);
107109
void HIDDEN xbps_set_cb_fetch(struct xbps_handle *, off_t, off_t, off_t,
108110
const char *, bool, bool, bool);
109111
int HIDDEN xbps_set_cb_state(struct xbps_handle *, xbps_state_t, int,

lib/external/fexec.c

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include "xbps_api_impl.h"
4141

4242
static int
43-
pfcexec(struct xbps_handle *xhp, const char *file, const char **argv)
43+
pfcexec(const struct xbps_handle *xhp, const char *file, const char **argv)
4444
{
4545
pid_t child;
4646
int status;
@@ -86,7 +86,54 @@ pfcexec(struct xbps_handle *xhp, const char *file, const char **argv)
8686
}
8787

8888
static int
89-
vfcexec(struct xbps_handle *xhp, const char *arg, va_list ap)
89+
pfcexecp(const struct xbps_handle *xhp, const char *file, const char **argv)
90+
{
91+
pid_t child;
92+
int status;
93+
94+
child = fork();
95+
switch (child) {
96+
case 0:
97+
/*
98+
* If rootdir != / and uid==0 and bin/sh exists,
99+
* change root directory and exec command.
100+
*/
101+
if (strcmp(xhp->rootdir, "/")) {
102+
if ((geteuid() == 0) && (access("bin/sh", X_OK) == 0)) {
103+
if (chroot(xhp->rootdir) == -1) {
104+
xbps_dbg_printf("%s: chroot() "
105+
"failed: %s\n", *argv, strerror(errno));
106+
_exit(errno);
107+
}
108+
if (chdir("/") == -1) {
109+
xbps_dbg_printf("%s: chdir() "
110+
"failed: %s\n", *argv, strerror(errno));
111+
_exit(errno);
112+
}
113+
}
114+
}
115+
umask(022);
116+
(void)execvp(file, __UNCONST(argv));
117+
_exit(errno);
118+
/* NOTREACHED */
119+
case -1:
120+
return -1;
121+
}
122+
123+
while (waitpid(child, &status, 0) < 0) {
124+
if (errno != EINTR)
125+
return -1;
126+
}
127+
128+
if (!WIFEXITED(status))
129+
return -1;
130+
131+
return WEXITSTATUS(status);
132+
}
133+
134+
135+
static int
136+
vfcexec(const struct xbps_handle *xhp, const char *arg, va_list ap)
90137
{
91138
const char **argv;
92139
size_t argv_size, argc;
@@ -122,8 +169,9 @@ vfcexec(struct xbps_handle *xhp, const char *arg, va_list ap)
122169
return retval;
123170
}
124171

172+
125173
int HIDDEN
126-
xbps_file_exec(struct xbps_handle *xhp, const char *arg, ...)
174+
xbps_file_exec(const struct xbps_handle *xhp, const char *arg, ...)
127175
{
128176
va_list ap;
129177
int result;
@@ -134,3 +182,15 @@ xbps_file_exec(struct xbps_handle *xhp, const char *arg, ...)
134182

135183
return result;
136184
}
185+
186+
int HIDDEN
187+
xbps_file_exec_argv(const struct xbps_handle *xhp, const char **argv)
188+
{
189+
return pfcexecp(xhp, argv[0], argv);
190+
}
191+
192+
int HIDDEN
193+
xbps_file_execp_argv(const struct xbps_handle *xhp, const char **argv)
194+
{
195+
return pfcexecp(xhp, argv[0], argv);
196+
}

0 commit comments

Comments
 (0)