You've already forked linux-apfs
mirror of
https://github.com/linux-apfs/linux-apfs.git
synced 2026-05-01 15:00:59 -07:00
eCryptfs: Make all miscdev functions use daemon ptr in file private_data
Now that a pointer to a valid struct ecryptfs_daemon is stored in the private_data of an opened /dev/ecryptfs file, the remaining miscdev functions can utilize the pointer rather than looking up the ecryptfs_daemon at the beginning of each operation. The security model of /dev/ecryptfs is simplified a little bit with this patch. Upon opening /dev/ecryptfs, a per-user ecryptfs_daemon is registered. Another daemon cannot be registered for that user until the last file reference is released. During the lifetime of the ecryptfs_daemon, access checks are not performed on the /dev/ecryptfs operations because it is assumed that the application securely handles the opened file descriptor and does not unintentionally leak it to processes that are not trusted. Signed-off-by: Tyler Hicks <tyhicks@canonical.com> Cc: Sasha Levin <levinsasha928@gmail.com>
This commit is contained in:
+16
-89
@@ -32,8 +32,8 @@ static struct mutex ecryptfs_msg_ctx_lists_mux;
|
||||
static struct hlist_head *ecryptfs_daemon_hash;
|
||||
struct mutex ecryptfs_daemon_hash_mux;
|
||||
static int ecryptfs_hash_bits;
|
||||
#define ecryptfs_uid_hash(uid) \
|
||||
hash_long((unsigned long)uid, ecryptfs_hash_bits)
|
||||
#define ecryptfs_current_euid_hash(uid) \
|
||||
hash_long((unsigned long)current_euid(), ecryptfs_hash_bits)
|
||||
|
||||
static u32 ecryptfs_msg_counter;
|
||||
static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
|
||||
@@ -105,26 +105,24 @@ 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.
|
||||
*
|
||||
* Search the hash list for the given user id.
|
||||
* Search the hash list for the current effective user id.
|
||||
*
|
||||
* 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,
|
||||
struct user_namespace *user_ns)
|
||||
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon)
|
||||
{
|
||||
struct hlist_node *elem;
|
||||
int rc;
|
||||
|
||||
hlist_for_each_entry(*daemon, elem,
|
||||
&ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
|
||||
euid_chain) {
|
||||
if ((*daemon)->euid == euid && (*daemon)->user_ns == user_ns) {
|
||||
&ecryptfs_daemon_hash[ecryptfs_current_euid_hash()],
|
||||
euid_chain) {
|
||||
if ((*daemon)->file->f_cred->euid == current_euid() &&
|
||||
(*daemon)->file->f_cred->user_ns == current_user_ns()) {
|
||||
rc = 0;
|
||||
goto out;
|
||||
}
|
||||
@@ -137,9 +135,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
|
||||
* @file: File used when opening /dev/ecryptfs
|
||||
*
|
||||
* Must be called ceremoniously while in possession of
|
||||
* ecryptfs_sacred_daemon_hash_mux
|
||||
@@ -147,8 +143,7 @@ out:
|
||||
* Returns zero on success; non-zero otherwise
|
||||
*/
|
||||
int
|
||||
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
|
||||
struct user_namespace *user_ns, struct pid *pid)
|
||||
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
@@ -159,16 +154,13 @@ ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
|
||||
"GFP_KERNEL memory\n", __func__, sizeof(**daemon));
|
||||
goto out;
|
||||
}
|
||||
(*daemon)->euid = euid;
|
||||
(*daemon)->user_ns = get_user_ns(user_ns);
|
||||
(*daemon)->pid = get_pid(pid);
|
||||
(*daemon)->task = current;
|
||||
(*daemon)->file = file;
|
||||
mutex_init(&(*daemon)->mux);
|
||||
INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
|
||||
init_waitqueue_head(&(*daemon)->wait);
|
||||
(*daemon)->num_queued_msg_ctx = 0;
|
||||
hlist_add_head(&(*daemon)->euid_chain,
|
||||
&ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)]);
|
||||
&ecryptfs_daemon_hash[ecryptfs_current_euid_hash()]);
|
||||
out:
|
||||
return rc;
|
||||
}
|
||||
@@ -188,9 +180,6 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
|
||||
if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
|
||||
|| (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
|
||||
rc = -EBUSY;
|
||||
printk(KERN_WARNING "%s: Attempt to destroy daemon with pid "
|
||||
"[0x%p], but it is in the midst of a read or a poll\n",
|
||||
__func__, daemon->pid);
|
||||
mutex_unlock(&daemon->mux);
|
||||
goto out;
|
||||
}
|
||||
@@ -203,12 +192,6 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
|
||||
ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
|
||||
}
|
||||
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);
|
||||
kzfree(daemon);
|
||||
out:
|
||||
@@ -219,8 +202,6 @@ out:
|
||||
* ecryptfs_process_reponse
|
||||
* @msg: The ecryptfs message received; the caller should sanity check
|
||||
* msg->data_len and free the memory
|
||||
* @pid: The process ID of the userspace application that sent the
|
||||
* message
|
||||
* @seq: The sequence number of the message; must match the sequence
|
||||
* number for the existing message context waiting for this
|
||||
* response
|
||||
@@ -239,16 +220,11 @@ out:
|
||||
*
|
||||
* Returns zero on success; non-zero otherwise
|
||||
*/
|
||||
int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
|
||||
struct user_namespace *user_ns, struct pid *pid,
|
||||
u32 seq)
|
||||
int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
|
||||
struct ecryptfs_message *msg, u32 seq)
|
||||
{
|
||||
struct ecryptfs_daemon *uninitialized_var(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) {
|
||||
@@ -261,51 +237,6 @@ 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);
|
||||
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_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 [0x%p] but does "
|
||||
"not have a registered daemon\n", __func__,
|
||||
ctx_euid, pid);
|
||||
goto wake_up;
|
||||
}
|
||||
if (ctx_euid != euid) {
|
||||
rc = -EBADMSG;
|
||||
printk(KERN_WARNING "%s: Received message from user "
|
||||
"[%d]; expected message from user [%d]\n", __func__,
|
||||
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 [0x%p]\n",
|
||||
__func__, ctx_euid, pid);
|
||||
goto unlock;
|
||||
}
|
||||
if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
|
||||
rc = -EINVAL;
|
||||
printk(KERN_WARNING "%s: Desired context element is not "
|
||||
@@ -328,9 +259,8 @@ int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
|
||||
}
|
||||
memcpy(msg_ctx->msg, msg, msg_size);
|
||||
msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
|
||||
rc = 0;
|
||||
wake_up:
|
||||
wake_up_process(msg_ctx->task);
|
||||
rc = 0;
|
||||
unlock:
|
||||
mutex_unlock(&msg_ctx->mux);
|
||||
out:
|
||||
@@ -352,14 +282,11 @@ 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, euid, current_user_ns());
|
||||
rc = ecryptfs_find_daemon_by_euid(&daemon);
|
||||
if (rc || !daemon) {
|
||||
rc = -ENOTCONN;
|
||||
printk(KERN_ERR "%s: User [%d] does not have a daemon "
|
||||
"registered\n", __func__, euid);
|
||||
goto out;
|
||||
}
|
||||
mutex_lock(&ecryptfs_msg_ctx_lists_mux);
|
||||
|
||||
Reference in New Issue
Block a user