2020 * 02111-1307, USA.
2121 */
2222#include <linux/sched.h>
23+ #include <linux/user_namespace.h>
24+ #include <linux/nsproxy.h>
2325#include "ecryptfs_kernel.h"
2426
2527static LIST_HEAD (ecryptfs_msg_ctx_free_list );
@@ -103,6 +105,7 @@ void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
103105/**
104106 * ecryptfs_find_daemon_by_euid
105107 * @euid: The effective user id which maps to the desired daemon id
108+ * @user_ns: The namespace in which @euid applies
106109 * @daemon: If return value is zero, points to the desired daemon pointer
107110 *
108111 * Must be called with ecryptfs_daemon_hash_mux held.
@@ -111,15 +114,16 @@ void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
111114 *
112115 * Returns zero if the user id exists in the list; non-zero otherwise.
113116 */
114- int ecryptfs_find_daemon_by_euid (struct ecryptfs_daemon * * daemon , uid_t euid )
117+ int ecryptfs_find_daemon_by_euid (struct ecryptfs_daemon * * daemon , uid_t euid ,
118+ struct user_namespace * user_ns )
115119{
116120 struct hlist_node * elem ;
117121 int rc ;
118122
119123 hlist_for_each_entry (* daemon , elem ,
120124 & ecryptfs_daemon_hash [ecryptfs_uid_hash (euid )],
121125 euid_chain ) {
122- if ((* daemon )-> euid == euid ) {
126+ if ((* daemon )-> euid == euid && ( * daemon ) -> user_ns == user_ns ) {
123127 rc = 0 ;
124128 goto out ;
125129 }
@@ -186,6 +190,7 @@ static int ecryptfs_send_raw_message(unsigned int transport, u8 msg_type,
186190 * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
187191 * @daemon: Pointer to set to newly allocated daemon struct
188192 * @euid: Effective user id for the daemon
193+ * @user_ns: The namespace in which @euid applies
189194 * @pid: Process id for the daemon
190195 *
191196 * Must be called ceremoniously while in possession of
@@ -194,7 +199,8 @@ static int ecryptfs_send_raw_message(unsigned int transport, u8 msg_type,
194199 * Returns zero on success; non-zero otherwise
195200 */
196201int
197- ecryptfs_spawn_daemon (struct ecryptfs_daemon * * daemon , uid_t euid , pid_t pid )
202+ ecryptfs_spawn_daemon (struct ecryptfs_daemon * * daemon , uid_t euid ,
203+ struct user_namespace * user_ns , struct pid * pid )
198204{
199205 int rc = 0 ;
200206
@@ -206,7 +212,8 @@ ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, pid_t pid)
206212 goto out ;
207213 }
208214 (* daemon )-> euid = euid ;
209- (* daemon )-> pid = pid ;
215+ (* daemon )-> user_ns = get_user_ns (user_ns );
216+ (* daemon )-> pid = get_pid (pid );
210217 (* daemon )-> task = current ;
211218 mutex_init (& (* daemon )-> mux );
212219 INIT_LIST_HEAD (& (* daemon )-> msg_ctx_out_queue );
@@ -222,6 +229,7 @@ ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, pid_t pid)
222229 * ecryptfs_process_helo
223230 * @transport: The underlying transport (netlink, etc.)
224231 * @euid: The user ID owner of the message
232+ * @user_ns: The namespace in which @euid applies
225233 * @pid: The process ID for the userspace program that sent the
226234 * message
227235 *
@@ -231,32 +239,33 @@ ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, pid_t pid)
231239 * Returns zero after adding a new daemon to the hash list;
232240 * non-zero otherwise.
233241 */
234- int ecryptfs_process_helo (unsigned int transport , uid_t euid , pid_t pid )
242+ int ecryptfs_process_helo (unsigned int transport , uid_t euid ,
243+ struct user_namespace * user_ns , struct pid * pid )
235244{
236245 struct ecryptfs_daemon * new_daemon ;
237246 struct ecryptfs_daemon * old_daemon ;
238247 int rc ;
239248
240249 mutex_lock (& ecryptfs_daemon_hash_mux );
241- rc = ecryptfs_find_daemon_by_euid (& old_daemon , euid );
250+ rc = ecryptfs_find_daemon_by_euid (& old_daemon , euid , user_ns );
242251 if (rc != 0 ) {
243252 printk (KERN_WARNING "Received request from user [%d] "
244- "to register daemon [%d ]; unregistering daemon "
245- "[%d ]\n" , euid , pid , old_daemon -> pid );
253+ "to register daemon [0x%p ]; unregistering daemon "
254+ "[0x%p ]\n" , euid , pid , old_daemon -> pid );
246255 rc = ecryptfs_send_raw_message (transport , ECRYPTFS_MSG_QUIT ,
247256 old_daemon );
248257 if (rc )
249258 printk (KERN_WARNING "Failed to send QUIT "
250- "message to daemon [%d ]; rc = [%d]\n" ,
259+ "message to daemon [0x%p ]; rc = [%d]\n" ,
251260 old_daemon -> pid , rc );
252261 hlist_del (& old_daemon -> euid_chain );
253262 kfree (old_daemon );
254263 }
255- rc = ecryptfs_spawn_daemon (& new_daemon , euid , pid );
264+ rc = ecryptfs_spawn_daemon (& new_daemon , euid , user_ns , pid );
256265 if (rc )
257266 printk (KERN_ERR "%s: The gods are displeased with this attempt "
258- "to create a new daemon object for euid [%d]; pid [%d]; "
259- "rc = [%d]\n" , __func__ , euid , pid , rc );
267+ "to create a new daemon object for euid [%d]; pid "
268+ "[0x%p]; rc = [%d]\n" , __func__ , euid , pid , rc );
260269 mutex_unlock (& ecryptfs_daemon_hash_mux );
261270 return rc ;
262271}
@@ -277,7 +286,7 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
277286 || (daemon -> flags & ECRYPTFS_DAEMON_IN_POLL )) {
278287 rc = - EBUSY ;
279288 printk (KERN_WARNING "%s: Attempt to destroy daemon with pid "
280- "[%d ], but it is in the midst of a read or a poll\n" ,
289+ "[0x%p ], but it is in the midst of a read or a poll\n" ,
281290 __func__ , daemon -> pid );
282291 mutex_unlock (& daemon -> mux );
283292 goto out ;
@@ -293,6 +302,10 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
293302 hlist_del (& daemon -> euid_chain );
294303 if (daemon -> task )
295304 wake_up_process (daemon -> task );
305+ if (daemon -> pid )
306+ put_pid (daemon -> pid );
307+ if (daemon -> user_ns )
308+ put_user_ns (daemon -> user_ns );
296309 mutex_unlock (& daemon -> mux );
297310 memset (daemon , 0 , sizeof (* daemon ));
298311 kfree (daemon );
@@ -303,24 +316,26 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
303316/**
304317 * ecryptfs_process_quit
305318 * @euid: The user ID owner of the message
319+ * @user_ns: The namespace in which @euid applies
306320 * @pid: The process ID for the userspace program that sent the
307321 * message
308322 *
309323 * Deletes the corresponding daemon for the given euid and pid, if
310324 * it is the registered that is requesting the deletion. Returns zero
311325 * after deleting the desired daemon; non-zero otherwise.
312326 */
313- int ecryptfs_process_quit (uid_t euid , pid_t pid )
327+ int ecryptfs_process_quit (uid_t euid , struct user_namespace * user_ns ,
328+ struct pid * pid )
314329{
315330 struct ecryptfs_daemon * daemon ;
316331 int rc ;
317332
318333 mutex_lock (& ecryptfs_daemon_hash_mux );
319- rc = ecryptfs_find_daemon_by_euid (& daemon , euid );
334+ rc = ecryptfs_find_daemon_by_euid (& daemon , euid , user_ns );
320335 if (rc || !daemon ) {
321336 rc = - EINVAL ;
322337 printk (KERN_ERR "Received request from user [%d] to "
323- "unregister unrecognized daemon [%d ]\n" , euid , pid );
338+ "unregister unrecognized daemon [0x%p ]\n" , euid , pid );
324339 goto out_unlock ;
325340 }
326341 rc = ecryptfs_exorcise_daemon (daemon );
@@ -354,11 +369,14 @@ int ecryptfs_process_quit(uid_t euid, pid_t pid)
354369 * Returns zero on success; non-zero otherwise
355370 */
356371int ecryptfs_process_response (struct ecryptfs_message * msg , uid_t euid ,
357- pid_t pid , u32 seq )
372+ struct user_namespace * user_ns , struct pid * pid ,
373+ u32 seq )
358374{
359375 struct ecryptfs_daemon * daemon ;
360376 struct ecryptfs_msg_ctx * msg_ctx ;
361377 size_t msg_size ;
378+ struct nsproxy * nsproxy ;
379+ struct user_namespace * current_user_ns ;
362380 int rc ;
363381
364382 if (msg -> index >= ecryptfs_message_buf_len ) {
@@ -372,12 +390,25 @@ int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
372390 msg_ctx = & ecryptfs_msg_ctx_arr [msg -> index ];
373391 mutex_lock (& msg_ctx -> mux );
374392 mutex_lock (& ecryptfs_daemon_hash_mux );
375- rc = ecryptfs_find_daemon_by_euid (& daemon , msg_ctx -> task -> euid );
393+ rcu_read_lock ();
394+ nsproxy = task_nsproxy (msg_ctx -> task );
395+ if (nsproxy == NULL ) {
396+ rc = - EBADMSG ;
397+ printk (KERN_ERR "%s: Receiving process is a zombie. Dropping "
398+ "message.\n" , __func__ );
399+ rcu_read_unlock ();
400+ mutex_unlock (& ecryptfs_daemon_hash_mux );
401+ goto wake_up ;
402+ }
403+ current_user_ns = nsproxy -> user_ns ;
404+ rc = ecryptfs_find_daemon_by_euid (& daemon , msg_ctx -> task -> euid ,
405+ current_user_ns );
406+ rcu_read_unlock ();
376407 mutex_unlock (& ecryptfs_daemon_hash_mux );
377408 if (rc ) {
378409 rc = - EBADMSG ;
379410 printk (KERN_WARNING "%s: User [%d] received a "
380- "message response from process [%d ] but does "
411+ "message response from process [0x%p ] but does "
381412 "not have a registered daemon\n" , __func__ ,
382413 msg_ctx -> task -> euid , pid );
383414 goto wake_up ;
@@ -389,10 +420,17 @@ int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
389420 euid , msg_ctx -> task -> euid );
390421 goto unlock ;
391422 }
423+ if (current_user_ns != user_ns ) {
424+ rc = - EBADMSG ;
425+ printk (KERN_WARNING "%s: Received message from user_ns "
426+ "[0x%p]; expected message from user_ns [0x%p]\n" ,
427+ __func__ , user_ns , nsproxy -> user_ns );
428+ goto unlock ;
429+ }
392430 if (daemon -> pid != pid ) {
393431 rc = - EBADMSG ;
394432 printk (KERN_ERR "%s: User [%d] sent a message response "
395- "from an unrecognized process [%d ]\n" ,
433+ "from an unrecognized process [0x%p ]\n" ,
396434 __func__ , msg_ctx -> task -> euid , pid );
397435 goto unlock ;
398436 }
@@ -446,7 +484,8 @@ ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
446484 struct ecryptfs_daemon * daemon ;
447485 int rc ;
448486
449- rc = ecryptfs_find_daemon_by_euid (& daemon , current -> euid );
487+ rc = ecryptfs_find_daemon_by_euid (& daemon , current -> euid ,
488+ current -> nsproxy -> user_ns );
450489 if (rc || !daemon ) {
451490 rc = - ENOTCONN ;
452491 printk (KERN_ERR "%s: User [%d] does not have a daemon "
0 commit comments