Skip to content

Commit 6a3fd92

Browse files
Michael Halcrowtorvalds
authored andcommitted
eCryptfs: make key module subsystem respect namespaces
Make eCryptfs key module subsystem respect namespaces. Since I will be removing the netlink interface in a future patch, I just made changes to the netlink.c code so that it will not break the build. With my recent patches, the kernel module currently defaults to the device handle interface rather than the netlink interface. [akpm@linux-foundation.org: export free_user_ns()] Signed-off-by: Michael Halcrow <mhalcrow@us.ibm.com> Acked-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f66e883 commit 6a3fd92

File tree

5 files changed

+136
-64
lines changed

5 files changed

+136
-64
lines changed

fs/ecryptfs/ecryptfs_kernel.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/namei.h>
3535
#include <linux/scatterlist.h>
3636
#include <linux/hash.h>
37+
#include <linux/nsproxy.h>
3738

3839
/* Version verification for shared data structures w/ userspace */
3940
#define ECRYPTFS_VERSION_MAJOR 0x00
@@ -410,8 +411,9 @@ struct ecryptfs_daemon {
410411
#define ECRYPTFS_DAEMON_MISCDEV_OPEN 0x00000008
411412
u32 flags;
412413
u32 num_queued_msg_ctx;
413-
pid_t pid;
414+
struct pid *pid;
414415
uid_t euid;
416+
struct user_namespace *user_ns;
415417
struct task_struct *task;
416418
struct mutex mux;
417419
struct list_head msg_ctx_out_queue;
@@ -610,10 +612,13 @@ int
610612
ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
611613
size_t size, int flags);
612614
int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode);
613-
int ecryptfs_process_helo(unsigned int transport, uid_t uid, pid_t pid);
614-
int ecryptfs_process_quit(uid_t uid, pid_t pid);
615-
int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t uid,
616-
pid_t pid, u32 seq);
615+
int ecryptfs_process_helo(unsigned int transport, uid_t euid,
616+
struct user_namespace *user_ns, struct pid *pid);
617+
int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
618+
struct pid *pid);
619+
int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
620+
struct user_namespace *user_ns, struct pid *pid,
621+
u32 seq);
617622
int ecryptfs_send_message(unsigned int transport, char *data, int data_len,
618623
struct ecryptfs_msg_ctx **msg_ctx);
619624
int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
@@ -623,13 +628,13 @@ void ecryptfs_release_messaging(unsigned int transport);
623628

624629
int ecryptfs_send_netlink(char *data, int data_len,
625630
struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
626-
u16 msg_flags, pid_t daemon_pid);
631+
u16 msg_flags, struct pid *daemon_pid);
627632
int ecryptfs_init_netlink(void);
628633
void ecryptfs_release_netlink(void);
629634

630635
int ecryptfs_send_connector(char *data, int data_len,
631636
struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
632-
u16 msg_flags, pid_t daemon_pid);
637+
u16 msg_flags, struct pid *daemon_pid);
633638
int ecryptfs_init_connector(void);
634639
void ecryptfs_release_connector(void);
635640
void
@@ -672,7 +677,8 @@ int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
672677
struct inode *ecryptfs_inode);
673678
struct page *ecryptfs_get_locked_page(struct file *file, loff_t index);
674679
int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon);
675-
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid);
680+
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
681+
struct user_namespace *user_ns);
676682
int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
677683
size_t *length_size);
678684
int ecryptfs_write_packet_length(char *dest, size_t size,
@@ -684,6 +690,7 @@ int ecryptfs_send_miscdev(char *data, size_t data_size,
684690
u16 msg_flags, struct ecryptfs_daemon *daemon);
685691
void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);
686692
int
687-
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, pid_t pid);
693+
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
694+
struct user_namespace *user_ns, struct pid *pid);
688695

689696
#endif /* #ifndef ECRYPTFS_KERNEL_H */

fs/ecryptfs/messaging.c

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
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

2527
static 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
*/
196201
int
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
*/
356371
int 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

Comments
 (0)