static int write_tag_67_packet(char *key, size_t key_size, struct ecryptfs_message **reply) { unsigned char *data; size_t data_len; size_t length_size; size_t i = 0; int rc = 0; data_len = key_size + 4; *reply = malloc(sizeof(struct ecryptfs_message) + data_len); if (!*reply) { rc = -errno; syslog(LOG_ERR, "Failed to allocate memory: %m\n"); goto out; } data = (*reply)->data; data[i++] = ECRYPTFS_TAG_67_PACKET; data[i++] = ECRYPTFS_PACKET_STATUS_GOOD; rc = ecryptfs_write_packet_length((char *)&data[i], key_size, &length_size); if (rc) { syslog(LOG_ERR, "Invalid packet format\n"); goto out; } i += length_size; memcpy(&data[i], key, key_size); i += key_size; (*reply)->data_len = data_len; out: return rc; }
/** * ecryptfs_miscdev_read - format and send message from queue * @file: fs/ecryptfs/euid miscdevfs handle (ignored) * @buf: User buffer into which to copy the next message on the daemon queue * @count: Amount of space available in @buf * @ppos: Offset in file (ignored) * * Pulls the most recent message from the daemon queue, formats it for * being sent via a miscdevfs handle, and copies it into @buf * * Returns the number of bytes copied into the user buffer */ static ssize_t ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct ecryptfs_daemon *daemon; struct ecryptfs_msg_ctx *msg_ctx; size_t packet_length_size; char packet_length[3]; size_t i; size_t total_length; uid_t euid = current_euid(); int rc; mutex_lock(&ecryptfs_daemon_hash_mux); /* TODO: Just use file->private_data? */ rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns()); if (rc || !daemon) { mutex_unlock(&ecryptfs_daemon_hash_mux); return -EINVAL; } mutex_lock(&daemon->mux); if (task_pid(current) != daemon->pid) { mutex_unlock(&daemon->mux); mutex_unlock(&ecryptfs_daemon_hash_mux); return -EPERM; } if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { rc = 0; mutex_unlock(&ecryptfs_daemon_hash_mux); printk(KERN_WARNING "%s: Attempt to read from zombified " "daemon\n", __func__); goto out_unlock_daemon; } if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) { rc = 0; mutex_unlock(&ecryptfs_daemon_hash_mux); goto out_unlock_daemon; } /* This daemon will not go away so long as this flag is set */ daemon->flags |= ECRYPTFS_DAEMON_IN_READ; mutex_unlock(&ecryptfs_daemon_hash_mux); check_list: if (list_empty(&daemon->msg_ctx_out_queue)) { mutex_unlock(&daemon->mux); rc = wait_event_interruptible( daemon->wait, !list_empty(&daemon->msg_ctx_out_queue)); mutex_lock(&daemon->mux); if (rc < 0) { rc = 0; goto out_unlock_daemon; } } if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { rc = 0; goto out_unlock_daemon; } if (list_empty(&daemon->msg_ctx_out_queue)) { /* Something else jumped in since the * wait_event_interruptable() and removed the * message from the queue; try again */ goto check_list; } msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue, struct ecryptfs_msg_ctx, daemon_out_list); BUG_ON(!msg_ctx); mutex_lock(&msg_ctx->mux); if (msg_ctx->msg) { rc = ecryptfs_write_packet_length(packet_length, msg_ctx->msg_size, &packet_length_size); if (rc) { rc = 0; printk(KERN_WARNING "%s: Error writing packet length; " "rc = [%d]\n", __func__, rc); goto out_unlock_msg_ctx; } } else { packet_length_size = 0; msg_ctx->msg_size = 0; } /* miscdevfs packet format: * Octet 0: Type * Octets 1-4: network byte order msg_ctx->counter * Octets 5-N0: Size of struct ecryptfs_message to follow * Octets N0-N1: struct ecryptfs_message (including data) * * Octets 5-N1 not written if the packet type does not * include a message */ total_length = (1 + 4 + packet_length_size + msg_ctx->msg_size); if (count < total_length) { rc = 0; printk(KERN_WARNING "%s: Only given user buffer of " "size [%zd], but we need [%zd] to read the " "pending message\n", __func__, count, total_length); goto out_unlock_msg_ctx; } rc = -EFAULT; if (put_user(msg_ctx->type, buf)) goto out_unlock_msg_ctx; if (put_user(cpu_to_be32(msg_ctx->counter), (__be32 __user *)(buf + 1))) goto out_unlock_msg_ctx; i = 5; if (msg_ctx->msg) { if (copy_to_user(&buf[i], packet_length, packet_length_size)) goto out_unlock_msg_ctx; i += packet_length_size; if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size)) goto out_unlock_msg_ctx; i += msg_ctx->msg_size; } rc = i; list_del(&msg_ctx->daemon_out_list); kfree(msg_ctx->msg); msg_ctx->msg = NULL; /* We do not expect a reply from the userspace daemon for any * message type other than ECRYPTFS_MSG_REQUEST */ if (msg_ctx->type != ECRYPTFS_MSG_REQUEST) ecryptfs_msg_ctx_alloc_to_free(msg_ctx); out_unlock_msg_ctx: mutex_unlock(&msg_ctx->mux); out_unlock_daemon: daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ; mutex_unlock(&daemon->mux); return rc; }
/** * ecryptfs_miscdev_read - format and send message from queue * @file: miscdevfs handle * @buf: User buffer into which to copy the next message on the daemon queue * @count: Amount of space available in @buf * @ppos: Offset in file (ignored) * * Pulls the most recent message from the daemon queue, formats it for * being sent via a miscdevfs handle, and copies it into @buf * * Returns the number of bytes copied into the user buffer */ static ssize_t ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct ecryptfs_daemon *daemon = file->private_data; struct ecryptfs_msg_ctx *msg_ctx; size_t packet_length_size; char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE]; size_t i; size_t total_length; int rc; mutex_lock(&daemon->mux); if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { rc = 0; printk(KERN_WARNING "%s: Attempt to read from zombified " "daemon\n", __func__); goto out_unlock_daemon; } if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) { rc = 0; goto out_unlock_daemon; } /* This daemon will not go away so long as this flag is set */ daemon->flags |= ECRYPTFS_DAEMON_IN_READ; check_list: if (list_empty(&daemon->msg_ctx_out_queue)) { mutex_unlock(&daemon->mux); rc = wait_event_interruptible( daemon->wait, !list_empty(&daemon->msg_ctx_out_queue)); mutex_lock(&daemon->mux); if (rc < 0) { rc = 0; goto out_unlock_daemon; } } if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { rc = 0; goto out_unlock_daemon; } if (list_empty(&daemon->msg_ctx_out_queue)) { /* Something else jumped in since the * wait_event_interruptable() and removed the * message from the queue; try again */ goto check_list; } msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue, struct ecryptfs_msg_ctx, daemon_out_list); BUG_ON(!msg_ctx); mutex_lock(&msg_ctx->mux); if (msg_ctx->msg) { rc = ecryptfs_write_packet_length(packet_length, msg_ctx->msg_size, &packet_length_size); if (rc) { rc = 0; printk(KERN_WARNING "%s: Error writing packet length; " "rc = [%d]\n", __func__, rc); goto out_unlock_msg_ctx; } } else { packet_length_size = 0; msg_ctx->msg_size = 0; } total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size + msg_ctx->msg_size); if (count < total_length) { rc = 0; printk(KERN_WARNING "%s: Only given user buffer of " "size [%zd], but we need [%zd] to read the " "pending message\n", __func__, count, total_length); goto out_unlock_msg_ctx; } rc = -EFAULT; if (put_user(msg_ctx->type, buf)) goto out_unlock_msg_ctx; if (put_user(cpu_to_be32(msg_ctx->counter), (__be32 __user *)(&buf[PKT_CTR_OFFSET]))) goto out_unlock_msg_ctx; i = PKT_TYPE_SIZE + PKT_CTR_SIZE; if (msg_ctx->msg) { if (packet_length_size > sizeof(packet_length) || copy_to_user(&buf[i], packet_length, packet_length_size)) goto out_unlock_msg_ctx; i += packet_length_size; if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size)) goto out_unlock_msg_ctx; i += msg_ctx->msg_size; } rc = i; list_del(&msg_ctx->daemon_out_list); kfree(msg_ctx->msg); msg_ctx->msg = NULL; /* We do not expect a reply from the userspace daemon for any * message type other than ECRYPTFS_MSG_REQUEST */ if (msg_ctx->type != ECRYPTFS_MSG_REQUEST) ecryptfs_msg_ctx_alloc_to_free(msg_ctx); out_unlock_msg_ctx: mutex_unlock(&msg_ctx->mux); out_unlock_daemon: daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ; mutex_unlock(&daemon->mux); return rc; }