Skip to content

Commit 2ecaf55

Browse files
committed
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>
1 parent 5669688 commit 2ecaf55

3 files changed

Lines changed: 47 additions & 172 deletions

File tree

‎fs/ecryptfs/ecryptfs_kernel.h‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,7 @@ struct ecryptfs_daemon {
392392
#define ECRYPTFS_DAEMON_MISCDEV_OPEN 0x00000008
393393
u32 flags;
394394
u32 num_queued_msg_ctx;
395-
struct pid *pid;
396-
uid_t euid;
397-
struct user_namespace *user_ns;
398-
struct task_struct *task;
395+
struct file *file;
399396
struct mutex mux;
400397
struct list_head msg_ctx_out_queue;
401398
wait_queue_head_t wait;
@@ -619,9 +616,8 @@ int
619616
ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
620617
size_t size, int flags);
621618
int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode);
622-
int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
623-
struct user_namespace *user_ns, struct pid *pid,
624-
u32 seq);
619+
int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
620+
struct ecryptfs_message *msg, u32 seq);
625621
int ecryptfs_send_message(char *data, int data_len,
626622
struct ecryptfs_msg_ctx **msg_ctx);
627623
int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
@@ -666,8 +662,7 @@ int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
666662
struct inode *ecryptfs_inode);
667663
struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index);
668664
int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon);
669-
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
670-
struct user_namespace *user_ns);
665+
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon);
671666
int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
672667
size_t *length_size);
673668
int ecryptfs_write_packet_length(char *dest, size_t size,
@@ -679,8 +674,7 @@ int ecryptfs_send_miscdev(char *data, size_t data_size,
679674
u16 msg_flags, struct ecryptfs_daemon *daemon);
680675
void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);
681676
int
682-
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
683-
struct user_namespace *user_ns, struct pid *pid);
677+
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file);
684678
int ecryptfs_init_kthread(void);
685679
void ecryptfs_destroy_kthread(void);
686680
int ecryptfs_privileged_open(struct file **lower_file,

‎fs/ecryptfs/messaging.c‎

Lines changed: 16 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ static struct mutex ecryptfs_msg_ctx_lists_mux;
3232
static struct hlist_head *ecryptfs_daemon_hash;
3333
struct mutex ecryptfs_daemon_hash_mux;
3434
static int ecryptfs_hash_bits;
35-
#define ecryptfs_uid_hash(uid) \
36-
hash_long((unsigned long)uid, ecryptfs_hash_bits)
35+
#define ecryptfs_current_euid_hash(uid) \
36+
hash_long((unsigned long)current_euid(), ecryptfs_hash_bits)
3737

3838
static u32 ecryptfs_msg_counter;
3939
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)
105105

106106
/**
107107
* ecryptfs_find_daemon_by_euid
108-
* @euid: The effective user id which maps to the desired daemon id
109-
* @user_ns: The namespace in which @euid applies
110108
* @daemon: If return value is zero, points to the desired daemon pointer
111109
*
112110
* Must be called with ecryptfs_daemon_hash_mux held.
113111
*
114-
* Search the hash list for the given user id.
112+
* Search the hash list for the current effective user id.
115113
*
116114
* Returns zero if the user id exists in the list; non-zero otherwise.
117115
*/
118-
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
119-
struct user_namespace *user_ns)
116+
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon)
120117
{
121118
struct hlist_node *elem;
122119
int rc;
123120

124121
hlist_for_each_entry(*daemon, elem,
125-
&ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
126-
euid_chain) {
127-
if ((*daemon)->euid == euid && (*daemon)->user_ns == user_ns) {
122+
&ecryptfs_daemon_hash[ecryptfs_current_euid_hash()],
123+
euid_chain) {
124+
if ((*daemon)->file->f_cred->euid == current_euid() &&
125+
(*daemon)->file->f_cred->user_ns == current_user_ns()) {
128126
rc = 0;
129127
goto out;
130128
}
@@ -137,18 +135,15 @@ int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
137135
/**
138136
* ecryptfs_spawn_daemon - Create and initialize a new daemon struct
139137
* @daemon: Pointer to set to newly allocated daemon struct
140-
* @euid: Effective user id for the daemon
141-
* @user_ns: The namespace in which @euid applies
142-
* @pid: Process id for the daemon
138+
* @file: File used when opening /dev/ecryptfs
143139
*
144140
* Must be called ceremoniously while in possession of
145141
* ecryptfs_sacred_daemon_hash_mux
146142
*
147143
* Returns zero on success; non-zero otherwise
148144
*/
149145
int
150-
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
151-
struct user_namespace *user_ns, struct pid *pid)
146+
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file)
152147
{
153148
int rc = 0;
154149

@@ -159,16 +154,13 @@ ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
159154
"GFP_KERNEL memory\n", __func__, sizeof(**daemon));
160155
goto out;
161156
}
162-
(*daemon)->euid = euid;
163-
(*daemon)->user_ns = get_user_ns(user_ns);
164-
(*daemon)->pid = get_pid(pid);
165-
(*daemon)->task = current;
157+
(*daemon)->file = file;
166158
mutex_init(&(*daemon)->mux);
167159
INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
168160
init_waitqueue_head(&(*daemon)->wait);
169161
(*daemon)->num_queued_msg_ctx = 0;
170162
hlist_add_head(&(*daemon)->euid_chain,
171-
&ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)]);
163+
&ecryptfs_daemon_hash[ecryptfs_current_euid_hash()]);
172164
out:
173165
return rc;
174166
}
@@ -188,9 +180,6 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
188180
if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
189181
|| (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
190182
rc = -EBUSY;
191-
printk(KERN_WARNING "%s: Attempt to destroy daemon with pid "
192-
"[0x%p], but it is in the midst of a read or a poll\n",
193-
__func__, daemon->pid);
194183
mutex_unlock(&daemon->mux);
195184
goto out;
196185
}
@@ -203,12 +192,6 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
203192
ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
204193
}
205194
hlist_del(&daemon->euid_chain);
206-
if (daemon->task)
207-
wake_up_process(daemon->task);
208-
if (daemon->pid)
209-
put_pid(daemon->pid);
210-
if (daemon->user_ns)
211-
put_user_ns(daemon->user_ns);
212195
mutex_unlock(&daemon->mux);
213196
kzfree(daemon);
214197
out:
@@ -219,8 +202,6 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
219202
* ecryptfs_process_reponse
220203
* @msg: The ecryptfs message received; the caller should sanity check
221204
* msg->data_len and free the memory
222-
* @pid: The process ID of the userspace application that sent the
223-
* message
224205
* @seq: The sequence number of the message; must match the sequence
225206
* number for the existing message context waiting for this
226207
* response
@@ -239,16 +220,11 @@ int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
239220
*
240221
* Returns zero on success; non-zero otherwise
241222
*/
242-
int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
243-
struct user_namespace *user_ns, struct pid *pid,
244-
u32 seq)
223+
int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
224+
struct ecryptfs_message *msg, u32 seq)
245225
{
246-
struct ecryptfs_daemon *uninitialized_var(daemon);
247226
struct ecryptfs_msg_ctx *msg_ctx;
248227
size_t msg_size;
249-
struct nsproxy *nsproxy;
250-
struct user_namespace *tsk_user_ns;
251-
uid_t ctx_euid;
252228
int rc;
253229

254230
if (msg->index >= ecryptfs_message_buf_len) {
@@ -261,51 +237,6 @@ int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
261237
}
262238
msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
263239
mutex_lock(&msg_ctx->mux);
264-
mutex_lock(&ecryptfs_daemon_hash_mux);
265-
rcu_read_lock();
266-
nsproxy = task_nsproxy(msg_ctx->task);
267-
if (nsproxy == NULL) {
268-
rc = -EBADMSG;
269-
printk(KERN_ERR "%s: Receiving process is a zombie. Dropping "
270-
"message.\n", __func__);
271-
rcu_read_unlock();
272-
mutex_unlock(&ecryptfs_daemon_hash_mux);
273-
goto wake_up;
274-
}
275-
tsk_user_ns = __task_cred(msg_ctx->task)->user_ns;
276-
ctx_euid = task_euid(msg_ctx->task);
277-
rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, tsk_user_ns);
278-
rcu_read_unlock();
279-
mutex_unlock(&ecryptfs_daemon_hash_mux);
280-
if (rc) {
281-
rc = -EBADMSG;
282-
printk(KERN_WARNING "%s: User [%d] received a "
283-
"message response from process [0x%p] but does "
284-
"not have a registered daemon\n", __func__,
285-
ctx_euid, pid);
286-
goto wake_up;
287-
}
288-
if (ctx_euid != euid) {
289-
rc = -EBADMSG;
290-
printk(KERN_WARNING "%s: Received message from user "
291-
"[%d]; expected message from user [%d]\n", __func__,
292-
euid, ctx_euid);
293-
goto unlock;
294-
}
295-
if (tsk_user_ns != user_ns) {
296-
rc = -EBADMSG;
297-
printk(KERN_WARNING "%s: Received message from user_ns "
298-
"[0x%p]; expected message from user_ns [0x%p]\n",
299-
__func__, user_ns, tsk_user_ns);
300-
goto unlock;
301-
}
302-
if (daemon->pid != pid) {
303-
rc = -EBADMSG;
304-
printk(KERN_ERR "%s: User [%d] sent a message response "
305-
"from an unrecognized process [0x%p]\n",
306-
__func__, ctx_euid, pid);
307-
goto unlock;
308-
}
309240
if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
310241
rc = -EINVAL;
311242
printk(KERN_WARNING "%s: Desired context element is not "
@@ -328,9 +259,8 @@ int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
328259
}
329260
memcpy(msg_ctx->msg, msg, msg_size);
330261
msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
331-
rc = 0;
332-
wake_up:
333262
wake_up_process(msg_ctx->task);
263+
rc = 0;
334264
unlock:
335265
mutex_unlock(&msg_ctx->mux);
336266
out:
@@ -352,14 +282,11 @@ ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
352282
struct ecryptfs_msg_ctx **msg_ctx)
353283
{
354284
struct ecryptfs_daemon *daemon;
355-
uid_t euid = current_euid();
356285
int rc;
357286

358-
rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
287+
rc = ecryptfs_find_daemon_by_euid(&daemon);
359288
if (rc || !daemon) {
360289
rc = -ENOTCONN;
361-
printk(KERN_ERR "%s: User [%d] does not have a daemon "
362-
"registered\n", __func__, euid);
363290
goto out;
364291
}
365292
mutex_lock(&ecryptfs_msg_ctx_lists_mux);

0 commit comments

Comments
 (0)