This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathve_enter.c
133 lines (106 loc) · 2.64 KB
/
ve_enter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2009 by Marco d'Itri <[email protected]>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#define _GNU_SOURCE
/* System library */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
/* OpenVZ-related includes */
#include "linux/vzcalluser.h"
#include "vzsyscalls.h"
#include "types.h"
/* FIXME: this is the Debian default, other systems may use /vz/ */
#undef VZ_DIR
#define VZ_DIR "/var/lib/vz/"
/* Prototypes */
int ve_enter(const id_t veid);
static inline int setluid(const id_t uid);
#define log_e(x, ...) do { \
syslog(LOG_AUTHPRIV|LOG_ERR, "pam_vz: " x, ## __VA_ARGS__); \
} while(0);
int ve_enter(const id_t veid)
{
int vzfd;
int errcode;
int retry = 0;
struct vzctl_env_create env_create;
char *root;
if ((vzfd = open(VZCTLDEV, O_RDWR)) < 0) {
log_e("open(%s): %m", VZCTLDEV);
return 0;
}
if (asprintf(&root, "%sroot/%u/", VZ_DIR, veid) == -1) {
log_e("malloc: %m");
close(vzfd);
return 0;
}
if (chroot(root) < 0) {
log_e("chroot(%s): %m", root);
free(root);
close(vzfd);
return 0;
}
free(root);
if (chdir("/") < 0) {
log_e("chdir(/): %m");
close(vzfd);
return 0;
}
/* associate the process with its beancounter */
if (setluid(veid) < 0) {
log_e("setluid(%d): %m", veid);
close(vzfd);
return 0;
}
memset(&env_create, 0, sizeof(env_create));
env_create.veid = veid;
env_create.flags = VE_ENTER;
/* enter the VE */
do {
if (retry)
sleep(1);
errcode = ioctl(vzfd, VZCTL_ENV_CREATE, &env_create);
} while (errcode < 0 && errno == EBUSY && retry++ < 3);
close(vzfd);
if (errcode < 0) {
log_e("ioctl(VZCTL_ENV_CREATE): %m");
return 0;
}
return 1;
}
/****************************************************************************/
static inline int setluid(const id_t uid)
{
return syscall(__NR_setluid, uid);
}
/****************************************************************************/
#ifdef TEST
int main(int argc, char *argv[], char *env[])
{
id_t veid = 10000;
if (!*++argv) {
fprintf(stderr, "Usage: ve_enter PROGRAM [ARG]...\n");
exit(1);
}
if (!ve_enter(veid)) {
fprintf(stderr, "ve_enter failed!\n");
exit(1);
}
execv(argv[0], argv);
fprintf(stderr, "exec(%s): %s\n", argv[0], strerror(errno));
exit(1);
}
#endif