]> Pileus Git - ~andy/linux/blobdiff - fs/ecryptfs/messaging.c
Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/dvrabel/uwb
[~andy/linux] / fs / ecryptfs / messaging.c
index c6038bd60897334158441586261758c5f5e195e8..c6983978a31eda7218fa921b954848e2d4a5c42c 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;
                }
@@ -130,12 +134,11 @@ out:
 }
 
 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);
 
 /**
  * ecryptfs_send_raw_message
- * @transport: Transport type
  * @msg_type: Message type
  * @daemon: Daemon struct for recipient of message
  *
@@ -146,38 +149,25 @@ ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
  *
  * Returns zero on success; non-zero otherwise
  */
-static int ecryptfs_send_raw_message(unsigned int transport, u8 msg_type,
+static int ecryptfs_send_raw_message(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;
+       rc = ecryptfs_send_message_locked(NULL, 0, msg_type, &msg_ctx);
+       if (rc) {
+               printk(KERN_ERR "%s: Error whilst attempting to send "
+                      "message to ecryptfsd; 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 message is sent. */
+       mutex_lock(&msg_ctx->mux);
+       msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_NO_REPLY;
+       mutex_unlock(&msg_ctx->mux);
 out:
        return rc;
 }
@@ -186,6 +176,7 @@ out:
  * 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,7 +185,8 @@ 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;
 
@@ -206,7 +198,8 @@ ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, pid_t pid)
                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);
@@ -220,8 +213,8 @@ out:
 
 /**
  * ecryptfs_process_helo
- * @transport: The underlying transport (netlink, etc.)
  * @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
  *
@@ -231,32 +224,32 @@ out:
  * 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)
+int ecryptfs_process_helo(uid_t euid, struct user_namespace *user_ns,
+                         struct pid *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);
+       rc = ecryptfs_find_daemon_by_euid(&old_daemon, euid, user_ns);
        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);
+                      "to register daemon [0x%p]; unregistering daemon "
+                      "[0x%p]\n", euid, pid, old_daemon->pid);
+               rc = ecryptfs_send_raw_message(ECRYPTFS_MSG_QUIT, old_daemon);
                if (rc)
                        printk(KERN_WARNING "Failed to send QUIT "
-                              "message to daemon [%d]; rc = [%d]\n",
+                              "message to daemon [0x%p]; rc = [%d]\n",
                               old_daemon->pid, rc);
                hlist_del(&old_daemon->euid_chain);
                kfree(old_daemon);
        }
-       rc = ecryptfs_spawn_daemon(&new_daemon, euid, pid);
+       rc = ecryptfs_spawn_daemon(&new_daemon, euid, user_ns, 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);
+                      "to create a new daemon object for euid [%d]; pid "
+                      "[0x%p]; rc = [%d]\n", __func__, euid, pid, rc);
        mutex_unlock(&ecryptfs_daemon_hash_mux);
        return rc;
 }
@@ -277,7 +270,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,6 +286,10 @@ 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);
@@ -303,6 +300,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 +308,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 +353,14 @@ 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 *current_user_ns;
        int rc;
 
        if (msg->index >= ecryptfs_message_buf_len) {
@@ -372,12 +374,25 @@ 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;
+       }
+       current_user_ns = nsproxy->user_ns;
+       rc = ecryptfs_find_daemon_by_euid(&daemon, msg_ctx->task->euid,
+                                         current_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);
                goto wake_up;
@@ -389,10 +404,17 @@ int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
                       euid, msg_ctx->task->euid);
                goto unlock;
        }
+       if (current_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, nsproxy->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",
+                      "from an unrecognized process [0x%p]\n",
                       __func__, msg_ctx->task->euid, pid);
                goto unlock;
        }
@@ -429,8 +451,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,13 +460,14 @@ 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;
        int rc;
 
-       rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
+       rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid,
+                                         current->nsproxy->user_ns);
        if (rc || !daemon) {
                rc = -ENOTCONN;
                printk(KERN_ERR "%s: User [%d] does not have a daemon "
@@ -464,20 +485,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 +496,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 +504,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 +554,7 @@ sleep:
        return rc;
 }
 
-int ecryptfs_init_messaging(unsigned int transport)
+int ecryptfs_init_messaging(void)
 {
        int i;
        int rc = 0;
@@ -600,27 +607,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 +653,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;
 }