Merge commit 'v2.6.30' into for-2.6.31
[safe/jmp/linux-2.6] / fs / ecryptfs / messaging.c
index c6038bd..f1c17e8 100644 (file)
@@ -20,6 +20,8 @@
  * 02111-1307, USA.
  */
 #include <linux/sched.h>
+#include <linux/user_namespace.h>
+#include <linux/nsproxy.h>
 #include "ecryptfs_kernel.h"
 
 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)
 /**
  * ecryptfs_find_daemon_by_euid
  * @euid: The effective user id which maps to the desired daemon id
+ * @user_ns: The namespace in which @euid applies
  * @daemon: If return value is zero, points to the desired daemon pointer
  *
  * Must be called with ecryptfs_daemon_hash_mux held.
@@ -111,7 +114,8 @@ void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
  *
  * Returns zero if the user id exists in the list; non-zero otherwise.
  */
-int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid)
+int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
+                                struct user_namespace *user_ns)
 {
        struct hlist_node *elem;
        int rc;
@@ -119,7 +123,7 @@ int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid)
        hlist_for_each_entry(*daemon, elem,
                             &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
                             euid_chain) {
-               if ((*daemon)->euid == euid) {
+               if ((*daemon)->euid == euid && (*daemon)->user_ns == user_ns) {
                        rc = 0;
                        goto out;
                }
@@ -129,63 +133,11 @@ out:
        return rc;
 }
 
-static int
-ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
-                            u8 msg_type, struct ecryptfs_msg_ctx **msg_ctx);
-
-/**
- * ecryptfs_send_raw_message
- * @transport: Transport type
- * @msg_type: Message type
- * @daemon: Daemon struct for recipient of message
- *
- * A raw message is one that does not include an ecryptfs_message
- * struct. It simply has a type.
- *
- * Must be called with ecryptfs_daemon_hash_mux held.
- *
- * Returns zero on success; non-zero otherwise
- */
-static int ecryptfs_send_raw_message(unsigned int transport, u8 msg_type,
-                                    struct ecryptfs_daemon *daemon)
-{
-       struct ecryptfs_msg_ctx *msg_ctx;
-       int rc;
-
-       switch(transport) {
-       case ECRYPTFS_TRANSPORT_NETLINK:
-               rc = ecryptfs_send_netlink(NULL, 0, NULL, msg_type, 0,
-                                          daemon->pid);
-               break;
-       case ECRYPTFS_TRANSPORT_MISCDEV:
-               rc = ecryptfs_send_message_locked(transport, NULL, 0, msg_type,
-                                                 &msg_ctx);
-               if (rc) {
-                       printk(KERN_ERR "%s: Error whilst attempting to send "
-                              "message via procfs; rc = [%d]\n", __func__, rc);
-                       goto out;
-               }
-               /* Raw messages are logically context-free (e.g., no
-                * reply is expected), so we set the state of the
-                * ecryptfs_msg_ctx object to indicate that it should
-                * be freed as soon as the transport sends out the message. */
-               mutex_lock(&msg_ctx->mux);
-               msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_NO_REPLY;
-               mutex_unlock(&msg_ctx->mux);
-               break;
-       case ECRYPTFS_TRANSPORT_CONNECTOR:
-       case ECRYPTFS_TRANSPORT_RELAYFS:
-       default:
-               rc = -ENOSYS;
-       }
-out:
-       return rc;
-}
-
 /**
  * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
  * @daemon: Pointer to set to newly allocated daemon struct
  * @euid: Effective user id for the daemon
+ * @user_ns: The namespace in which @euid applies
  * @pid: Process id for the daemon
  *
  * Must be called ceremoniously while in possession of
@@ -194,19 +146,21 @@ out:
  * Returns zero on success; non-zero otherwise
  */
 int
-ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, pid_t pid)
+ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
+                     struct user_namespace *user_ns, struct pid *pid)
 {
        int rc = 0;
 
        (*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
        if (!(*daemon)) {
                rc = -ENOMEM;
-               printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
+               printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
                       "GFP_KERNEL memory\n", __func__, sizeof(**daemon));
                goto out;
        }
        (*daemon)->euid = euid;
-       (*daemon)->pid = pid;
+       (*daemon)->user_ns = get_user_ns(user_ns);
+       (*daemon)->pid = get_pid(pid);
        (*daemon)->task = current;
        mutex_init(&(*daemon)->mux);
        INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
@@ -219,49 +173,6 @@ out:
 }
 
 /**
- * ecryptfs_process_helo
- * @transport: The underlying transport (netlink, etc.)
- * @euid: The user ID owner of the message
- * @pid: The process ID for the userspace program that sent the
- *       message
- *
- * Adds the euid and pid values to the daemon euid hash.  If an euid
- * already has a daemon pid registered, the daemon will be
- * unregistered before the new daemon is put into the hash list.
- * Returns zero after adding a new daemon to the hash list;
- * non-zero otherwise.
- */
-int ecryptfs_process_helo(unsigned int transport, uid_t euid, pid_t pid)
-{
-       struct ecryptfs_daemon *new_daemon;
-       struct ecryptfs_daemon *old_daemon;
-       int rc;
-
-       mutex_lock(&ecryptfs_daemon_hash_mux);
-       rc = ecryptfs_find_daemon_by_euid(&old_daemon, euid);
-       if (rc != 0) {
-               printk(KERN_WARNING "Received request from user [%d] "
-                      "to register daemon [%d]; unregistering daemon "
-                      "[%d]\n", euid, pid, old_daemon->pid);
-               rc = ecryptfs_send_raw_message(transport, ECRYPTFS_MSG_QUIT,
-                                              old_daemon);
-               if (rc)
-                       printk(KERN_WARNING "Failed to send QUIT "
-                              "message to daemon [%d]; rc = [%d]\n",
-                              old_daemon->pid, rc);
-               hlist_del(&old_daemon->euid_chain);
-               kfree(old_daemon);
-       }
-       rc = ecryptfs_spawn_daemon(&new_daemon, euid, pid);
-       if (rc)
-               printk(KERN_ERR "%s: The gods are displeased with this attempt "
-                      "to create a new daemon object for euid [%d]; pid [%d]; "
-                      "rc = [%d]\n", __func__, euid, pid, rc);
-       mutex_unlock(&ecryptfs_daemon_hash_mux);
-       return rc;
-}
-
-/**
  * ecryptfs_exorcise_daemon - Destroy the daemon struct
  *
  * Must be called ceremoniously while in possession of
@@ -277,7 +188,7 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
            || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
                rc = -EBUSY;
                printk(KERN_WARNING "%s: Attempt to destroy daemon with pid "
-                      "[%d], but it is in the midst of a read or a poll\n",
+                      "[0x%p], but it is in the midst of a read or a poll\n",
                       __func__, daemon->pid);
                mutex_unlock(&daemon->mux);
                goto out;
@@ -293,9 +204,12 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
        hlist_del(&daemon->euid_chain);
        if (daemon->task)
                wake_up_process(daemon->task);
+       if (daemon->pid)
+               put_pid(daemon->pid);
+       if (daemon->user_ns)
+               put_user_ns(daemon->user_ns);
        mutex_unlock(&daemon->mux);
-       memset(daemon, 0, sizeof(*daemon));
-       kfree(daemon);
+       kzfree(daemon);
 out:
        return rc;
 }
@@ -303,6 +217,7 @@ out:
 /**
  * ecryptfs_process_quit
  * @euid: The user ID owner of the message
+ * @user_ns: The namespace in which @euid applies
  * @pid: The process ID for the userspace program that sent the
  *       message
  *
@@ -310,17 +225,18 @@ out:
  * it is the registered that is requesting the deletion. Returns zero
  * after deleting the desired daemon; non-zero otherwise.
  */
-int ecryptfs_process_quit(uid_t euid, pid_t pid)
+int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
+                         struct pid *pid)
 {
        struct ecryptfs_daemon *daemon;
        int rc;
 
        mutex_lock(&ecryptfs_daemon_hash_mux);
-       rc = ecryptfs_find_daemon_by_euid(&daemon, euid);
+       rc = ecryptfs_find_daemon_by_euid(&daemon, euid, user_ns);
        if (rc || !daemon) {
                rc = -EINVAL;
                printk(KERN_ERR "Received request from user [%d] to "
-                      "unregister unrecognized daemon [%d]\n", euid, pid);
+                      "unregister unrecognized daemon [0x%p]\n", euid, pid);
                goto out_unlock;
        }
        rc = ecryptfs_exorcise_daemon(daemon);
@@ -354,11 +270,15 @@ out_unlock:
  * Returns zero on success; non-zero otherwise
  */
 int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
-                             pid_t pid, u32 seq)
+                             struct user_namespace *user_ns, struct pid *pid,
+                             u32 seq)
 {
        struct ecryptfs_daemon *daemon;
        struct ecryptfs_msg_ctx *msg_ctx;
        size_t msg_size;
+       struct nsproxy *nsproxy;
+       struct user_namespace *tsk_user_ns;
+       uid_t ctx_euid;
        int rc;
 
        if (msg->index >= ecryptfs_message_buf_len) {
@@ -372,28 +292,48 @@ int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
        msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
        mutex_lock(&msg_ctx->mux);
        mutex_lock(&ecryptfs_daemon_hash_mux);
-       rc = ecryptfs_find_daemon_by_euid(&daemon, msg_ctx->task->euid);
+       rcu_read_lock();
+       nsproxy = task_nsproxy(msg_ctx->task);
+       if (nsproxy == NULL) {
+               rc = -EBADMSG;
+               printk(KERN_ERR "%s: Receiving process is a zombie. Dropping "
+                      "message.\n", __func__);
+               rcu_read_unlock();
+               mutex_unlock(&ecryptfs_daemon_hash_mux);
+               goto wake_up;
+       }
+       tsk_user_ns = __task_cred(msg_ctx->task)->user->user_ns;
+       ctx_euid = task_euid(msg_ctx->task);
+       rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, tsk_user_ns);
+       rcu_read_unlock();
        mutex_unlock(&ecryptfs_daemon_hash_mux);
        if (rc) {
                rc = -EBADMSG;
                printk(KERN_WARNING "%s: User [%d] received a "
-                      "message response from process [%d] but does "
+                      "message response from process [0x%p] but does "
                       "not have a registered daemon\n", __func__,
-                      msg_ctx->task->euid, pid);
+                      ctx_euid, pid);
                goto wake_up;
        }
-       if (msg_ctx->task->euid != euid) {
+       if (ctx_euid != euid) {
                rc = -EBADMSG;
                printk(KERN_WARNING "%s: Received message from user "
                       "[%d]; expected message from user [%d]\n", __func__,
-                      euid, msg_ctx->task->euid);
+                      euid, ctx_euid);
+               goto unlock;
+       }
+       if (tsk_user_ns != user_ns) {
+               rc = -EBADMSG;
+               printk(KERN_WARNING "%s: Received message from user_ns "
+                      "[0x%p]; expected message from user_ns [0x%p]\n",
+                      __func__, user_ns, tsk_user_ns);
                goto unlock;
        }
        if (daemon->pid != pid) {
                rc = -EBADMSG;
                printk(KERN_ERR "%s: User [%d] sent a message response "
-                      "from an unrecognized process [%d]\n",
-                      __func__, msg_ctx->task->euid, pid);
+                      "from an unrecognized process [0x%p]\n",
+                      __func__, ctx_euid, pid);
                goto unlock;
        }
        if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
@@ -412,7 +352,7 @@ int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
        msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
        if (!msg_ctx->msg) {
                rc = -ENOMEM;
-               printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
+               printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
                       "GFP_KERNEL memory\n", __func__, msg_size);
                goto unlock;
        }
@@ -429,8 +369,6 @@ out:
 
 /**
  * ecryptfs_send_message_locked
- * @transport: The transport over which to send the message (i.e.,
- *             netlink)
  * @data: The data to send
  * @data_len: The length of data
  * @msg_ctx: The message context allocated for the send
@@ -440,17 +378,18 @@ out:
  * Returns zero on success; non-zero otherwise
  */
 static int
-ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
-                            u8 msg_type, struct ecryptfs_msg_ctx **msg_ctx)
+ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
+                            struct ecryptfs_msg_ctx **msg_ctx)
 {
        struct ecryptfs_daemon *daemon;
+       uid_t euid = current_euid();
        int rc;
 
-       rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
+       rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
        if (rc || !daemon) {
                rc = -ENOTCONN;
                printk(KERN_ERR "%s: User [%d] does not have a daemon "
-                      "registered\n", __func__, current->euid);
+                      "registered\n", __func__, euid);
                goto out;
        }
        mutex_lock(&ecryptfs_msg_ctx_lists_mux);
@@ -464,20 +403,8 @@ ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
        ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
        mutex_unlock(&(*msg_ctx)->mux);
        mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
-       switch (transport) {
-       case ECRYPTFS_TRANSPORT_NETLINK:
-               rc = ecryptfs_send_netlink(data, data_len, *msg_ctx, msg_type,
-                                          0, daemon->pid);
-               break;
-       case ECRYPTFS_TRANSPORT_MISCDEV:
-               rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type,
-                                          0, daemon);
-               break;
-       case ECRYPTFS_TRANSPORT_CONNECTOR:
-       case ECRYPTFS_TRANSPORT_RELAYFS:
-       default:
-               rc = -ENOSYS;
-       }
+       rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
+                                  daemon);
        if (rc)
                printk(KERN_ERR "%s: Error attempting to send message to "
                       "userspace daemon; rc = [%d]\n", __func__, rc);
@@ -487,8 +414,6 @@ out:
 
 /**
  * ecryptfs_send_message
- * @transport: The transport over which to send the message (i.e.,
- *             netlink)
  * @data: The data to send
  * @data_len: The length of data
  * @msg_ctx: The message context allocated for the send
@@ -497,14 +422,14 @@ out:
  *
  * Returns zero on success; non-zero otherwise
  */
-int ecryptfs_send_message(unsigned int transport, char *data, int data_len,
+int ecryptfs_send_message(char *data, int data_len,
                          struct ecryptfs_msg_ctx **msg_ctx)
 {
        int rc;
 
        mutex_lock(&ecryptfs_daemon_hash_mux);
-       rc = ecryptfs_send_message_locked(transport, data, data_len,
-                                         ECRYPTFS_MSG_REQUEST, msg_ctx);
+       rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
+                                         msg_ctx);
        mutex_unlock(&ecryptfs_daemon_hash_mux);
        return rc;
 }
@@ -547,7 +472,7 @@ sleep:
        return rc;
 }
 
-int ecryptfs_init_messaging(unsigned int transport)
+int ecryptfs_init_messaging(void)
 {
        int i;
        int rc = 0;
@@ -600,27 +525,14 @@ int ecryptfs_init_messaging(unsigned int transport)
                mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
        }
        mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
-       switch(transport) {
-       case ECRYPTFS_TRANSPORT_NETLINK:
-               rc = ecryptfs_init_netlink();
-               if (rc)
-                       ecryptfs_release_messaging(transport);
-               break;
-       case ECRYPTFS_TRANSPORT_MISCDEV:
-               rc = ecryptfs_init_ecryptfs_miscdev();
-               if (rc)
-                       ecryptfs_release_messaging(transport);
-               break;
-       case ECRYPTFS_TRANSPORT_CONNECTOR:
-       case ECRYPTFS_TRANSPORT_RELAYFS:
-       default:
-               rc = -ENOSYS;
-       }
+       rc = ecryptfs_init_ecryptfs_miscdev();
+       if (rc)
+               ecryptfs_release_messaging();
 out:
        return rc;
 }
 
-void ecryptfs_release_messaging(unsigned int transport)
+void ecryptfs_release_messaging(void)
 {
        if (ecryptfs_msg_ctx_arr) {
                int i;
@@ -659,17 +571,6 @@ void ecryptfs_release_messaging(unsigned int transport)
                kfree(ecryptfs_daemon_hash);
                mutex_unlock(&ecryptfs_daemon_hash_mux);
        }
-       switch(transport) {
-       case ECRYPTFS_TRANSPORT_NETLINK:
-               ecryptfs_release_netlink();
-               break;
-       case ECRYPTFS_TRANSPORT_MISCDEV:
-               ecryptfs_destroy_ecryptfs_miscdev();
-               break;
-       case ECRYPTFS_TRANSPORT_CONNECTOR:
-       case ECRYPTFS_TRANSPORT_RELAYFS:
-       default:
-               break;
-       }
+       ecryptfs_destroy_ecryptfs_miscdev();
        return;
 }