aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ecryptfs/miscdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ecryptfs/miscdev.c')
-rw-r--r--fs/ecryptfs/miscdev.c98
1 files changed, 26 insertions, 72 deletions
diff --git a/fs/ecryptfs/miscdev.c b/fs/ecryptfs/miscdev.c
index c0038f6566d4..412e6eda25f8 100644
--- a/fs/ecryptfs/miscdev.c
+++ b/fs/ecryptfs/miscdev.c
@@ -33,7 +33,7 @@ static atomic_t ecryptfs_num_miscdev_opens;
33 33
34/** 34/**
35 * ecryptfs_miscdev_poll 35 * ecryptfs_miscdev_poll
36 * @file: dev file (ignored) 36 * @file: dev file
37 * @pt: dev poll table (ignored) 37 * @pt: dev poll table (ignored)
38 * 38 *
39 * Returns the poll mask 39 * Returns the poll mask
@@ -41,20 +41,10 @@ static atomic_t ecryptfs_num_miscdev_opens;
41static unsigned int 41static unsigned int
42ecryptfs_miscdev_poll(struct file *file, poll_table *pt) 42ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
43{ 43{
44 struct ecryptfs_daemon *daemon; 44 struct ecryptfs_daemon *daemon = file->private_data;
45 unsigned int mask = 0; 45 unsigned int mask = 0;
46 uid_t euid = current_euid();
47 int rc;
48 46
49 mutex_lock(&ecryptfs_daemon_hash_mux);
50 /* TODO: Just use file->private_data? */
51 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
52 if (rc || !daemon) {
53 mutex_unlock(&ecryptfs_daemon_hash_mux);
54 return -EINVAL;
55 }
56 mutex_lock(&daemon->mux); 47 mutex_lock(&daemon->mux);
57 mutex_unlock(&ecryptfs_daemon_hash_mux);
58 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { 48 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
59 printk(KERN_WARNING "%s: Attempt to poll on zombified " 49 printk(KERN_WARNING "%s: Attempt to poll on zombified "
60 "daemon\n", __func__); 50 "daemon\n", __func__);
@@ -79,7 +69,7 @@ out_unlock_daemon:
79/** 69/**
80 * ecryptfs_miscdev_open 70 * ecryptfs_miscdev_open
81 * @inode: inode of miscdev handle (ignored) 71 * @inode: inode of miscdev handle (ignored)
82 * @file: file for miscdev handle (ignored) 72 * @file: file for miscdev handle
83 * 73 *
84 * Returns zero on success; non-zero otherwise 74 * Returns zero on success; non-zero otherwise
85 */ 75 */
@@ -87,7 +77,6 @@ static int
87ecryptfs_miscdev_open(struct inode *inode, struct file *file) 77ecryptfs_miscdev_open(struct inode *inode, struct file *file)
88{ 78{
89 struct ecryptfs_daemon *daemon = NULL; 79 struct ecryptfs_daemon *daemon = NULL;
90 uid_t euid = current_euid();
91 int rc; 80 int rc;
92 81
93 mutex_lock(&ecryptfs_daemon_hash_mux); 82 mutex_lock(&ecryptfs_daemon_hash_mux);
@@ -98,30 +87,20 @@ ecryptfs_miscdev_open(struct inode *inode, struct file *file)
98 "count; rc = [%d]\n", __func__, rc); 87 "count; rc = [%d]\n", __func__, rc);
99 goto out_unlock_daemon_list; 88 goto out_unlock_daemon_list;
100 } 89 }
101 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns()); 90 rc = ecryptfs_find_daemon_by_euid(&daemon);
102 if (rc || !daemon) { 91 if (!rc) {
103 rc = ecryptfs_spawn_daemon(&daemon, euid, current_user_ns(),
104 task_pid(current));
105 if (rc) {
106 printk(KERN_ERR "%s: Error attempting to spawn daemon; "
107 "rc = [%d]\n", __func__, rc);
108 goto out_module_put_unlock_daemon_list;
109 }
110 }
111 mutex_lock(&daemon->mux);
112 if (daemon->pid != task_pid(current)) {
113 rc = -EINVAL; 92 rc = -EINVAL;
114 printk(KERN_ERR "%s: pid [0x%p] has registered with euid [%d], " 93 goto out_unlock_daemon_list;
115 "but pid [0x%p] has attempted to open the handle " 94 }
116 "instead\n", __func__, daemon->pid, daemon->euid, 95 rc = ecryptfs_spawn_daemon(&daemon, file);
117 task_pid(current)); 96 if (rc) {
118 goto out_unlock_daemon; 97 printk(KERN_ERR "%s: Error attempting to spawn daemon; "
98 "rc = [%d]\n", __func__, rc);
99 goto out_module_put_unlock_daemon_list;
119 } 100 }
101 mutex_lock(&daemon->mux);
120 if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) { 102 if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
121 rc = -EBUSY; 103 rc = -EBUSY;
122 printk(KERN_ERR "%s: Miscellaneous device handle may only be "
123 "opened once per daemon; pid [0x%p] already has this "
124 "handle open\n", __func__, daemon->pid);
125 goto out_unlock_daemon; 104 goto out_unlock_daemon;
126 } 105 }
127 daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN; 106 daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
@@ -140,7 +119,7 @@ out_unlock_daemon_list:
140/** 119/**
141 * ecryptfs_miscdev_release 120 * ecryptfs_miscdev_release
142 * @inode: inode of fs/ecryptfs/euid handle (ignored) 121 * @inode: inode of fs/ecryptfs/euid handle (ignored)
143 * @file: file for fs/ecryptfs/euid handle (ignored) 122 * @file: file for fs/ecryptfs/euid handle
144 * 123 *
145 * This keeps the daemon registered until the daemon sends another 124 * This keeps the daemon registered until the daemon sends another
146 * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters. 125 * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
@@ -150,20 +129,18 @@ out_unlock_daemon_list:
150static int 129static int
151ecryptfs_miscdev_release(struct inode *inode, struct file *file) 130ecryptfs_miscdev_release(struct inode *inode, struct file *file)
152{ 131{
153 struct ecryptfs_daemon *daemon = NULL; 132 struct ecryptfs_daemon *daemon = file->private_data;
154 uid_t euid = current_euid();
155 int rc; 133 int rc;
156 134
157 mutex_lock(&ecryptfs_daemon_hash_mux);
158 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
159 if (rc || !daemon)
160 daemon = file->private_data;
161 mutex_lock(&daemon->mux); 135 mutex_lock(&daemon->mux);
162 BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN)); 136 BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
163 daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN; 137 daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
164 atomic_dec(&ecryptfs_num_miscdev_opens); 138 atomic_dec(&ecryptfs_num_miscdev_opens);
165 mutex_unlock(&daemon->mux); 139 mutex_unlock(&daemon->mux);
140
141 mutex_lock(&ecryptfs_daemon_hash_mux);
166 rc = ecryptfs_exorcise_daemon(daemon); 142 rc = ecryptfs_exorcise_daemon(daemon);
143 mutex_unlock(&ecryptfs_daemon_hash_mux);
167 if (rc) { 144 if (rc) {
168 printk(KERN_CRIT "%s: Fatal error whilst attempting to " 145 printk(KERN_CRIT "%s: Fatal error whilst attempting to "
169 "shut down daemon; rc = [%d]. Please report this " 146 "shut down daemon; rc = [%d]. Please report this "
@@ -171,7 +148,6 @@ ecryptfs_miscdev_release(struct inode *inode, struct file *file)
171 BUG(); 148 BUG();
172 } 149 }
173 module_put(THIS_MODULE); 150 module_put(THIS_MODULE);
174 mutex_unlock(&ecryptfs_daemon_hash_mux);
175 return rc; 151 return rc;
176} 152}
177 153
@@ -248,7 +224,7 @@ int ecryptfs_send_miscdev(char *data, size_t data_size,
248 224
249/** 225/**
250 * ecryptfs_miscdev_read - format and send message from queue 226 * ecryptfs_miscdev_read - format and send message from queue
251 * @file: fs/ecryptfs/euid miscdevfs handle (ignored) 227 * @file: miscdevfs handle
252 * @buf: User buffer into which to copy the next message on the daemon queue 228 * @buf: User buffer into which to copy the next message on the daemon queue
253 * @count: Amount of space available in @buf 229 * @count: Amount of space available in @buf
254 * @ppos: Offset in file (ignored) 230 * @ppos: Offset in file (ignored)
@@ -262,43 +238,27 @@ static ssize_t
262ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count, 238ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
263 loff_t *ppos) 239 loff_t *ppos)
264{ 240{
265 struct ecryptfs_daemon *daemon; 241 struct ecryptfs_daemon *daemon = file->private_data;
266 struct ecryptfs_msg_ctx *msg_ctx; 242 struct ecryptfs_msg_ctx *msg_ctx;
267 size_t packet_length_size; 243 size_t packet_length_size;
268 char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE]; 244 char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE];
269 size_t i; 245 size_t i;
270 size_t total_length; 246 size_t total_length;
271 uid_t euid = current_euid();
272 int rc; 247 int rc;
273 248
274 mutex_lock(&ecryptfs_daemon_hash_mux);
275 /* TODO: Just use file->private_data? */
276 rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
277 if (rc || !daemon) {
278 mutex_unlock(&ecryptfs_daemon_hash_mux);
279 return -EINVAL;
280 }
281 mutex_lock(&daemon->mux); 249 mutex_lock(&daemon->mux);
282 if (task_pid(current) != daemon->pid) {
283 mutex_unlock(&daemon->mux);
284 mutex_unlock(&ecryptfs_daemon_hash_mux);
285 return -EPERM;
286 }
287 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) { 250 if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
288 rc = 0; 251 rc = 0;
289 mutex_unlock(&ecryptfs_daemon_hash_mux);
290 printk(KERN_WARNING "%s: Attempt to read from zombified " 252 printk(KERN_WARNING "%s: Attempt to read from zombified "
291 "daemon\n", __func__); 253 "daemon\n", __func__);
292 goto out_unlock_daemon; 254 goto out_unlock_daemon;
293 } 255 }
294 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) { 256 if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
295 rc = 0; 257 rc = 0;
296 mutex_unlock(&ecryptfs_daemon_hash_mux);
297 goto out_unlock_daemon; 258 goto out_unlock_daemon;
298 } 259 }
299 /* This daemon will not go away so long as this flag is set */ 260 /* This daemon will not go away so long as this flag is set */
300 daemon->flags |= ECRYPTFS_DAEMON_IN_READ; 261 daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
301 mutex_unlock(&ecryptfs_daemon_hash_mux);
302check_list: 262check_list:
303 if (list_empty(&daemon->msg_ctx_out_queue)) { 263 if (list_empty(&daemon->msg_ctx_out_queue)) {
304 mutex_unlock(&daemon->mux); 264 mutex_unlock(&daemon->mux);
@@ -382,16 +342,12 @@ out_unlock_daemon:
382 * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon 342 * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
383 * @data: Bytes comprising struct ecryptfs_message 343 * @data: Bytes comprising struct ecryptfs_message
384 * @data_size: sizeof(struct ecryptfs_message) + data len 344 * @data_size: sizeof(struct ecryptfs_message) + data len
385 * @euid: Effective user id of miscdevess sending the miscdev response
386 * @user_ns: The namespace in which @euid applies
387 * @pid: Miscdevess id of miscdevess sending the miscdev response
388 * @seq: Sequence number for miscdev response packet 345 * @seq: Sequence number for miscdev response packet
389 * 346 *
390 * Returns zero on success; non-zero otherwise 347 * Returns zero on success; non-zero otherwise
391 */ 348 */
392static int ecryptfs_miscdev_response(char *data, size_t data_size, 349static int ecryptfs_miscdev_response(struct ecryptfs_daemon *daemon, char *data,
393 uid_t euid, struct user_namespace *user_ns, 350 size_t data_size, u32 seq)
394 struct pid *pid, u32 seq)
395{ 351{
396 struct ecryptfs_message *msg = (struct ecryptfs_message *)data; 352 struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
397 int rc; 353 int rc;
@@ -403,7 +359,7 @@ static int ecryptfs_miscdev_response(char *data, size_t data_size,
403 rc = -EINVAL; 359 rc = -EINVAL;
404 goto out; 360 goto out;
405 } 361 }
406 rc = ecryptfs_process_response(msg, euid, user_ns, pid, seq); 362 rc = ecryptfs_process_response(daemon, msg, seq);
407 if (rc) 363 if (rc)
408 printk(KERN_ERR 364 printk(KERN_ERR
409 "Error processing response message; rc = [%d]\n", rc); 365 "Error processing response message; rc = [%d]\n", rc);
@@ -413,7 +369,7 @@ out:
413 369
414/** 370/**
415 * ecryptfs_miscdev_write - handle write to daemon miscdev handle 371 * ecryptfs_miscdev_write - handle write to daemon miscdev handle
416 * @file: File for misc dev handle (ignored) 372 * @file: File for misc dev handle
417 * @buf: Buffer containing user data 373 * @buf: Buffer containing user data
418 * @count: Amount of data in @buf 374 * @count: Amount of data in @buf
419 * @ppos: Pointer to offset in file (ignored) 375 * @ppos: Pointer to offset in file (ignored)
@@ -428,7 +384,6 @@ ecryptfs_miscdev_write(struct file *file, const char __user *buf,
428 u32 seq; 384 u32 seq;
429 size_t packet_size, packet_size_length; 385 size_t packet_size, packet_size_length;
430 char *data; 386 char *data;
431 uid_t euid = current_euid();
432 unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE]; 387 unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE];
433 ssize_t rc; 388 ssize_t rc;
434 389
@@ -488,10 +443,9 @@ memdup:
488 } 443 }
489 memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE); 444 memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE);
490 seq = be32_to_cpu(counter_nbo); 445 seq = be32_to_cpu(counter_nbo);
491 rc = ecryptfs_miscdev_response( 446 rc = ecryptfs_miscdev_response(file->private_data,
492 &data[PKT_LEN_OFFSET + packet_size_length], 447 &data[PKT_LEN_OFFSET + packet_size_length],
493 packet_size, euid, current_user_ns(), 448 packet_size, seq);
494 task_pid(current), seq);
495 if (rc) { 449 if (rc) {
496 printk(KERN_WARNING "%s: Failed to deliver miscdev " 450 printk(KERN_WARNING "%s: Failed to deliver miscdev "
497 "response to requesting operation; rc = [%zd]\n", 451 "response to requesting operation; rc = [%zd]\n",