summaryrefslogtreecommitdiff
path: root/lib/rpmchroot.c
blob: 400e285b52de9472a26408541682732f68261f93 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "system.h"
#include <sched.h>
#include <stdlib.h>
#include <fcntl.h>
#include <rpm/rpmstring.h>
#include <rpm/rpmlog.h>
#include "lib/rpmchroot.h"
#include "lib/rpmug.h"
#include "debug.h"

int _rpm_nouserns = 0;

struct rootState_s {
    char *rootDir;
    int chrootDone;
    int cwd;
};

/* Process global chroot state */
static struct rootState_s rootState = {
   .rootDir = NULL,
   .chrootDone = 0,
   .cwd = -1,
}; 

#if defined(HAVE_UNSHARE) && defined(CLONE_NEWUSER)
/*
 * If setgroups file exists (Linux >= 3.19), we need to write "deny" to it,
 * otherwise gid_map will fail.
 */
static int deny_setgroups(void)
{
    int fd = open("/proc/self/setgroups", O_WRONLY, 0);
    int xx = -1;
    if (fd >= 0) {
	xx = write(fd, "deny\n", strlen("deny\n"));
	close (fd);
    }
    return (xx == -1);
}

static int setup_map(const char *path, unsigned int id, unsigned int oid)
{
    int xx = -1;
    int fd = open(path, O_WRONLY);
    if (fd >= 0) {
	char buf[256];
	int ret = snprintf(buf, sizeof(buf), "%u %u 1\n", id, oid);
	xx = write(fd, buf, ret);
	close (fd);
    }
    return (xx == -1);
}

/*
 * Try to become root by creating a user namespace. We don't really care
 * if this fails here because in that case chroot() will just fail as it
 * normally would.
 */
static void try_become_root(void)
{
    static int unshared = 0;
    uid_t uid = getuid();
    gid_t gid = getgid();
    if (!unshared && unshare(CLONE_NEWUSER | CLONE_NEWNS) == 0) {
	deny_setgroups();
	setup_map("/proc/self/uid_map", 0, uid);
	setup_map("/proc/self/gid_map", 0, gid);
	unshared = 1;
    }
    rpmlog(RPMLOG_DEBUG, "user ns: %d original user %d:%d current %d:%d\n",
	    unshared, uid, gid, getuid(), getgid());
}
#else
static void try_become_root(void)
{
}
#endif

int rpmChrootSet(const char *rootDir)
{
    int rc = 0;

    /* Setting same rootDir again is a no-op and not an error */
    if (rootDir && rootState.rootDir && rstreq(rootDir, rootState.rootDir))
	return 0;

    /* Resetting only permitted in neutral state */
    if (rootState.chrootDone != 0)
	return -1;

    rootState.rootDir = _free(rootState.rootDir);
    if (rootState.cwd >= 0) {
	close(rootState.cwd);
	rootState.cwd = -1;
    }

    if (rootDir != NULL) {
	rootState.rootDir = rstrdup(rootDir);
	rootState.cwd = open(".", O_RDONLY);
	if (rootState.cwd < 0) {
	    rpmlog(RPMLOG_ERR, _("Unable to open current directory: %m\n"));
	    rc = -1;
	}

	/* Force preloading of dlopen()'ed libraries before chroot */
	if (rpmugInit())
	    rc = -1;
    }

    return rc;
}

int rpmChrootIn(void)
{
    int rc = 0;

    if (rootState.rootDir == NULL || rstreq(rootState.rootDir, "/"))
	return 0;

    if (rootState.cwd < 0) {
	rpmlog(RPMLOG_ERR, _("%s: chroot directory not set\n"), __func__);
	return -1;
    }

    /* "refcounted" entry to chroot */
    if (rootState.chrootDone > 0) {
	rootState.chrootDone++;
    } else if (rootState.chrootDone == 0) {
	if (!_rpm_nouserns && getuid())
	    try_become_root();

	rpmlog(RPMLOG_DEBUG, "entering chroot %s\n", rootState.rootDir);
	if (chdir("/") == 0 && chroot(rootState.rootDir) == 0) {
	    rootState.chrootDone = 1;
	} else {
	    rpmlog(RPMLOG_ERR, _("Unable to change root directory: %m\n"));
	    rc = -1;
	}
    }
    return rc;
}

int rpmChrootOut(void)
{
    int rc = 0;
    if (rootState.rootDir == NULL || rstreq(rootState.rootDir, "/"))
	return 0;

    if (rootState.cwd < 0) {
	rpmlog(RPMLOG_ERR, _("%s: chroot directory not set\n"), __func__);
	return -1;
    }

    /* "refcounted" return from chroot */
    if (rootState.chrootDone > 1) {
	rootState.chrootDone--;
    } else if (rootState.chrootDone == 1) {
	rpmlog(RPMLOG_DEBUG, "exiting chroot %s\n", rootState.rootDir);
	if (chroot(".") == 0 && fchdir(rootState.cwd) == 0) {
	    rootState.chrootDone = 0;
	} else {
	    rpmlog(RPMLOG_ERR, _("Unable to restore root directory: %m\n"));
	    rc = -1;
	}
    }
    return rc;
}

int rpmChrootDone(void)
{
    return (rootState.chrootDone > 0);
}