@@ -120,6 +120,7 @@ void forkproxy(void)
120
120
121
121
if (pid == 0) {
122
122
int listen_nsfd;
123
+ int setns_flags;
123
124
124
125
whoami = FORKPROXY_CHILD;
125
126
@@ -134,20 +135,24 @@ void forkproxy(void)
134
135
_exit(EXIT_FAILURE);
135
136
}
136
137
137
- // Attach to the user namespace of the listener
138
- attach_userns_fd(listen_nsfd) ;
138
+ // Attach to the namespaces of the listener
139
+ setns_flags = CLONE_NEWNET ;
139
140
140
- // Attach to the network namespace of the listener
141
- if (!change_namespaces(listen_pidfd, listen_nsfd, CLONE_NEWNET)) {
142
- fprintf(stderr, "Error: %m - Failed setns to listener network namespace\n");
143
- _exit(EXIT_FAILURE);
144
- }
141
+ if (in_same_namespace(getpid(), listen_nsfd, "user") > 0)
142
+ setns_flags |= CLONE_NEWUSER;
145
143
146
- if ((needs_mntns & LISTEN_NEEDS_MNTNS) && !change_namespaces(listen_pidfd, listen_nsfd, CLONE_NEWNS)) {
147
- fprintf(stderr, "Error: %m - Failed setns to listener mount namespace\n");
144
+ if (needs_mntns & CONNECT_NEEDS_MNTNS)
145
+ setns_flags |= CLONE_NEWNS;
146
+
147
+ if (!change_namespaces(listen_pidfd, listen_nsfd, setns_flags)) {
148
+ fprintf(stderr, "Error: %m - Failed setns to listener namespaces\n");
148
149
_exit(EXIT_FAILURE);
149
150
}
150
151
152
+ // Complete switch to the user namespace of the connector
153
+ if (setns_flags & CLONE_NEWUSER)
154
+ finalize_userns();
155
+
151
156
close_prot_errno_disarm(listen_nsfd);
152
157
close_prot_errno_disarm(listen_pidfd);
153
158
@@ -166,6 +171,7 @@ void forkproxy(void)
166
171
} else {
167
172
pthread_t thread;
168
173
int connect_nsfd;
174
+ int setns_flags;
169
175
170
176
whoami = FORKPROXY_PARENT;
171
177
@@ -180,21 +186,24 @@ void forkproxy(void)
180
186
_exit(EXIT_FAILURE);
181
187
}
182
188
183
- // Attach to the user namespace of the connector
184
- attach_userns_fd(connect_nsfd) ;
189
+ // Attach to the namespaces of the connector
190
+ setns_flags = CLONE_NEWNET ;
185
191
186
- // Attach to the network namespace of the connector
187
- if (!change_namespaces(connect_pidfd, connect_nsfd, CLONE_NEWNET)) {
188
- fprintf(stderr, "Error: %m - Failed setns to connector network namespace\n");
189
- _exit(EXIT_FAILURE);
190
- }
192
+ if (in_same_namespace(getpid(), connect_nsfd, "user") > 0)
193
+ setns_flags |= CLONE_NEWUSER;
191
194
192
- // Attach to the mount namespace of the connector
193
- if ((needs_mntns & CONNECT_NEEDS_MNTNS) && !change_namespaces(connect_pidfd, connect_nsfd, CLONE_NEWNS)) {
194
- fprintf(stderr, "Error: %m - Failed setns to connector mount namespace\n");
195
+ if (needs_mntns & CONNECT_NEEDS_MNTNS)
196
+ setns_flags |= CLONE_NEWNS;
197
+
198
+ if (!change_namespaces(connect_pidfd, connect_nsfd, setns_flags)) {
199
+ fprintf(stderr, "Error: %m - Failed setns to connector namespaces\n");
195
200
_exit(EXIT_FAILURE);
196
201
}
197
202
203
+ // Complete switch to the user namespace of the connector
204
+ if (setns_flags & CLONE_NEWUSER)
205
+ finalize_userns();
206
+
198
207
close_prot_errno_disarm(connect_nsfd);
199
208
close_prot_errno_disarm(connect_pidfd);
200
209
0 commit comments