Blame view

fs/ecryptfs/messaging.c 17.3 KB
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
1
2
3
  /**
   * eCryptfs: Linux filesystem encryption layer
   *
f66e883eb   Michael Halcrow   eCryptfs: integra...
4
   * Copyright (C) 2004-2008 International Business Machines Corp.
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
   *		Tyler Hicks <tyhicks@ou.edu>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License version
   * 2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   * 02111-1307, USA.
   */
e8edc6e03   Alexey Dobriyan   Detach sched.h fr...
22
  #include <linux/sched.h>
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
23
24
  #include <linux/user_namespace.h>
  #include <linux/nsproxy.h>
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
25
  #include "ecryptfs_kernel.h"
dd2a3b7ad   Michael Halcrow   [PATCH] eCryptfs:...
26
27
28
  static LIST_HEAD(ecryptfs_msg_ctx_free_list);
  static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
  static struct mutex ecryptfs_msg_ctx_lists_mux;
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
29

f66e883eb   Michael Halcrow   eCryptfs: integra...
30
31
  static struct hlist_head *ecryptfs_daemon_hash;
  struct mutex ecryptfs_daemon_hash_mux;
dd2a3b7ad   Michael Halcrow   [PATCH] eCryptfs:...
32
33
34
  static int ecryptfs_hash_buckets;
  #define ecryptfs_uid_hash(uid) \
          hash_long((unsigned long)uid, ecryptfs_hash_buckets)
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
35

f66e883eb   Michael Halcrow   eCryptfs: integra...
36
  static u32 ecryptfs_msg_counter;
dd2a3b7ad   Michael Halcrow   [PATCH] eCryptfs:...
37
  static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
38
39
40
41
42
43
  
  /**
   * ecryptfs_acquire_free_msg_ctx
   * @msg_ctx: The context that was acquired from the free list
   *
   * Acquires a context element from the free list and locks the mutex
f66e883eb   Michael Halcrow   eCryptfs: integra...
44
45
46
47
   * on the context.  Sets the msg_ctx task to current.  Returns zero on
   * success; non-zero on error or upon failure to acquire a free
   * context element.  Must be called with ecryptfs_msg_ctx_lists_mux
   * held.
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
48
49
50
51
52
53
54
   */
  static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
  {
  	struct list_head *p;
  	int rc;
  
  	if (list_empty(&ecryptfs_msg_ctx_free_list)) {
f66e883eb   Michael Halcrow   eCryptfs: integra...
55
56
57
58
59
60
  		printk(KERN_WARNING "%s: The eCryptfs free "
  		       "context list is empty.  It may be helpful to "
  		       "specify the ecryptfs_message_buf_len "
  		       "parameter to be greater than the current "
  		       "value of [%d]
  ", __func__, ecryptfs_message_buf_len);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  		rc = -ENOMEM;
  		goto out;
  	}
  	list_for_each(p, &ecryptfs_msg_ctx_free_list) {
  		*msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
  		if (mutex_trylock(&(*msg_ctx)->mux)) {
  			(*msg_ctx)->task = current;
  			rc = 0;
  			goto out;
  		}
  	}
  	rc = -ENOMEM;
  out:
  	return rc;
  }
  
  /**
   * ecryptfs_msg_ctx_free_to_alloc
   * @msg_ctx: The context to move from the free list to the alloc list
   *
f66e883eb   Michael Halcrow   eCryptfs: integra...
81
   * Must be called with ecryptfs_msg_ctx_lists_mux held.
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
82
83
84
85
86
87
88
89
90
91
92
93
   */
  static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
  {
  	list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
  	msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
  	msg_ctx->counter = ++ecryptfs_msg_counter;
  }
  
  /**
   * ecryptfs_msg_ctx_alloc_to_free
   * @msg_ctx: The context to move from the alloc list to the free list
   *
f66e883eb   Michael Halcrow   eCryptfs: integra...
94
   * Must be called with ecryptfs_msg_ctx_lists_mux held.
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
95
   */
f66e883eb   Michael Halcrow   eCryptfs: integra...
96
  void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
97
98
99
100
  {
  	list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
  	if (msg_ctx->msg)
  		kfree(msg_ctx->msg);
f66e883eb   Michael Halcrow   eCryptfs: integra...
101
  	msg_ctx->msg = NULL;
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
102
103
104
105
  	msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
  }
  
  /**
f66e883eb   Michael Halcrow   eCryptfs: integra...
106
107
   * ecryptfs_find_daemon_by_euid
   * @euid: The effective user id which maps to the desired daemon id
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
108
   * @user_ns: The namespace in which @euid applies
f66e883eb   Michael Halcrow   eCryptfs: integra...
109
   * @daemon: If return value is zero, points to the desired daemon pointer
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
110
   *
f66e883eb   Michael Halcrow   eCryptfs: integra...
111
112
113
114
115
   * Must be called with ecryptfs_daemon_hash_mux held.
   *
   * Search the hash list for the given user id.
   *
   * Returns zero if the user id exists in the list; non-zero otherwise.
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
116
   */
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
117
118
  int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
  				 struct user_namespace *user_ns)
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
119
120
121
  {
  	struct hlist_node *elem;
  	int rc;
f66e883eb   Michael Halcrow   eCryptfs: integra...
122
123
124
  	hlist_for_each_entry(*daemon, elem,
  			     &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
  			     euid_chain) {
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
125
  		if ((*daemon)->euid == euid && (*daemon)->user_ns == user_ns) {
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
126
127
128
129
130
131
132
133
  			rc = 0;
  			goto out;
  		}
  	}
  	rc = -EINVAL;
  out:
  	return rc;
  }
f66e883eb   Michael Halcrow   eCryptfs: integra...
134
135
136
137
  /**
   * 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
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
138
   * @user_ns: The namespace in which @euid applies
f66e883eb   Michael Halcrow   eCryptfs: integra...
139
140
141
142
143
144
145
146
   * @pid: Process id for the daemon
   *
   * Must be called ceremoniously while in possession of
   * ecryptfs_sacred_daemon_hash_mux
   *
   * Returns zero on success; non-zero otherwise
   */
  int
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
147
148
  ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
  		      struct user_namespace *user_ns, struct pid *pid)
f66e883eb   Michael Halcrow   eCryptfs: integra...
149
150
151
152
153
154
  {
  	int rc = 0;
  
  	(*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
  	if (!(*daemon)) {
  		rc = -ENOMEM;
df261c52a   Michael Halcrow   eCryptfs: Replace...
155
  		printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
f66e883eb   Michael Halcrow   eCryptfs: integra...
156
157
158
159
160
  		       "GFP_KERNEL memory
  ", __func__, sizeof(**daemon));
  		goto out;
  	}
  	(*daemon)->euid = euid;
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
161
162
  	(*daemon)->user_ns = get_user_ns(user_ns);
  	(*daemon)->pid = get_pid(pid);
f66e883eb   Michael Halcrow   eCryptfs: integra...
163
164
165
166
167
168
169
170
  	(*daemon)->task = current;
  	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)]);
  out:
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
171
172
173
174
  	return rc;
  }
  
  /**
f66e883eb   Michael Halcrow   eCryptfs: integra...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
   * ecryptfs_exorcise_daemon - Destroy the daemon struct
   *
   * Must be called ceremoniously while in possession of
   * ecryptfs_daemon_hash_mux and the daemon's own mux.
   */
  int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
  {
  	struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
  	int rc = 0;
  
  	mutex_lock(&daemon->mux);
  	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 "
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
190
191
  		       "[0x%p], but it is in the midst of a read or a poll
  ",
f66e883eb   Michael Halcrow   eCryptfs: integra...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  		       __func__, daemon->pid);
  		mutex_unlock(&daemon->mux);
  		goto out;
  	}
  	list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
  				 &daemon->msg_ctx_out_queue, daemon_out_list) {
  		list_del(&msg_ctx->daemon_out_list);
  		daemon->num_queued_msg_ctx--;
  		printk(KERN_WARNING "%s: Warning: dropping message that is in "
  		       "the out queue of a dying daemon
  ", __func__);
  		ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  	}
  	hlist_del(&daemon->euid_chain);
  	if (daemon->task)
  		wake_up_process(daemon->task);
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
208
209
210
211
  	if (daemon->pid)
  		put_pid(daemon->pid);
  	if (daemon->user_ns)
  		put_user_ns(daemon->user_ns);
f66e883eb   Michael Halcrow   eCryptfs: integra...
212
  	mutex_unlock(&daemon->mux);
00fcf2cb6   Johannes Weiner   ecryptfs: use kzf...
213
  	kzfree(daemon);
f66e883eb   Michael Halcrow   eCryptfs: integra...
214
  out:
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
215
216
217
218
219
  	return rc;
  }
  
  /**
   * ecryptfs_process_quit
f66e883eb   Michael Halcrow   eCryptfs: integra...
220
   * @euid: The user ID owner of the message
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
221
   * @user_ns: The namespace in which @euid applies
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
222
223
224
   * @pid: The process ID for the userspace program that sent the
   *       message
   *
f66e883eb   Michael Halcrow   eCryptfs: integra...
225
   * Deletes the corresponding daemon for the given euid and pid, if
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
226
   * it is the registered that is requesting the deletion. Returns zero
f66e883eb   Michael Halcrow   eCryptfs: integra...
227
   * after deleting the desired daemon; non-zero otherwise.
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
228
   */
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
229
230
  int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
  			  struct pid *pid)
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
231
  {
f66e883eb   Michael Halcrow   eCryptfs: integra...
232
  	struct ecryptfs_daemon *daemon;
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
233
  	int rc;
f66e883eb   Michael Halcrow   eCryptfs: integra...
234
  	mutex_lock(&ecryptfs_daemon_hash_mux);
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
235
  	rc = ecryptfs_find_daemon_by_euid(&daemon, euid, user_ns);
f66e883eb   Michael Halcrow   eCryptfs: integra...
236
  	if (rc || !daemon) {
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
237
  		rc = -EINVAL;
f66e883eb   Michael Halcrow   eCryptfs: integra...
238
  		printk(KERN_ERR "Received request from user [%d] to "
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
239
240
  		       "unregister unrecognized daemon [0x%p]
  ", euid, pid);
f66e883eb   Michael Halcrow   eCryptfs: integra...
241
  		goto out_unlock;
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
242
  	}
f66e883eb   Michael Halcrow   eCryptfs: integra...
243
244
245
  	rc = ecryptfs_exorcise_daemon(daemon);
  out_unlock:
  	mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
246
247
248
249
250
251
  	return rc;
  }
  
  /**
   * ecryptfs_process_reponse
   * @msg: The ecryptfs message received; the caller should sanity check
f66e883eb   Michael Halcrow   eCryptfs: integra...
252
   *       msg->data_len and free the memory
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
253
254
   * @pid: The process ID of the userspace application that sent the
   *       message
f66e883eb   Michael Halcrow   eCryptfs: integra...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
   * @seq: The sequence number of the message; must match the sequence
   *       number for the existing message context waiting for this
   *       response
   *
   * Processes a response message after sending an operation request to
   * userspace. Some other process is awaiting this response. Before
   * sending out its first communications, the other process allocated a
   * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
   * response message contains this index so that we can copy over the
   * response message into the msg_ctx that the process holds a
   * reference to. The other process is going to wake up, check to see
   * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
   * proceed to read off and process the response message. Returns zero
   * upon delivery to desired context element; non-zero upon delivery
   * failure or error.
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
270
   *
f66e883eb   Michael Halcrow   eCryptfs: integra...
271
   * Returns zero on success; non-zero otherwise
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
272
   */
f66e883eb   Michael Halcrow   eCryptfs: integra...
273
  int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
274
275
  			      struct user_namespace *user_ns, struct pid *pid,
  			      u32 seq)
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
276
  {
f66e883eb   Michael Halcrow   eCryptfs: integra...
277
  	struct ecryptfs_daemon *daemon;
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
278
  	struct ecryptfs_msg_ctx *msg_ctx;
f66e883eb   Michael Halcrow   eCryptfs: integra...
279
  	size_t msg_size;
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
280
  	struct nsproxy *nsproxy;
18b6e0414   Serge Hallyn   User namespaces: ...
281
  	struct user_namespace *tsk_user_ns;
4eea03539   David Howells   CRED: Wrap task c...
282
  	uid_t ctx_euid;
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
283
284
285
286
  	int rc;
  
  	if (msg->index >= ecryptfs_message_buf_len) {
  		rc = -EINVAL;
f66e883eb   Michael Halcrow   eCryptfs: integra...
287
288
289
290
291
  		printk(KERN_ERR "%s: Attempt to reference "
  		       "context buffer at index [%d]; maximum "
  		       "allowable is [%d]
  ", __func__, msg->index,
  		       (ecryptfs_message_buf_len - 1));
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
292
293
294
295
  		goto out;
  	}
  	msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  	mutex_lock(&msg_ctx->mux);
f66e883eb   Michael Halcrow   eCryptfs: integra...
296
  	mutex_lock(&ecryptfs_daemon_hash_mux);
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
297
298
299
300
301
302
303
304
305
306
307
  	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.
  ", __func__);
  		rcu_read_unlock();
  		mutex_unlock(&ecryptfs_daemon_hash_mux);
  		goto wake_up;
  	}
18b6e0414   Serge Hallyn   User namespaces: ...
308
  	tsk_user_ns = __task_cred(msg_ctx->task)->user->user_ns;
4eea03539   David Howells   CRED: Wrap task c...
309
  	ctx_euid = task_euid(msg_ctx->task);
18b6e0414   Serge Hallyn   User namespaces: ...
310
  	rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, tsk_user_ns);
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
311
  	rcu_read_unlock();
f66e883eb   Michael Halcrow   eCryptfs: integra...
312
313
  	mutex_unlock(&ecryptfs_daemon_hash_mux);
  	if (rc) {
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
314
  		rc = -EBADMSG;
f66e883eb   Michael Halcrow   eCryptfs: integra...
315
  		printk(KERN_WARNING "%s: User [%d] received a "
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
316
  		       "message response from process [0x%p] but does "
f66e883eb   Michael Halcrow   eCryptfs: integra...
317
318
  		       "not have a registered daemon
  ", __func__,
4eea03539   David Howells   CRED: Wrap task c...
319
  		       ctx_euid, pid);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
320
321
  		goto wake_up;
  	}
4eea03539   David Howells   CRED: Wrap task c...
322
  	if (ctx_euid != euid) {
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
323
  		rc = -EBADMSG;
f66e883eb   Michael Halcrow   eCryptfs: integra...
324
325
326
  		printk(KERN_WARNING "%s: Received message from user "
  		       "[%d]; expected message from user [%d]
  ", __func__,
4eea03539   David Howells   CRED: Wrap task c...
327
  		       euid, ctx_euid);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
328
329
  		goto unlock;
  	}
18b6e0414   Serge Hallyn   User namespaces: ...
330
  	if (tsk_user_ns != user_ns) {
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
331
332
333
334
  		rc = -EBADMSG;
  		printk(KERN_WARNING "%s: Received message from user_ns "
  		       "[0x%p]; expected message from user_ns [0x%p]
  ",
18b6e0414   Serge Hallyn   User namespaces: ...
335
  		       __func__, user_ns, tsk_user_ns);
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
336
337
  		goto unlock;
  	}
f66e883eb   Michael Halcrow   eCryptfs: integra...
338
  	if (daemon->pid != pid) {
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
339
  		rc = -EBADMSG;
f66e883eb   Michael Halcrow   eCryptfs: integra...
340
  		printk(KERN_ERR "%s: User [%d] sent a message response "
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
341
342
  		       "from an unrecognized process [0x%p]
  ",
4eea03539   David Howells   CRED: Wrap task c...
343
  		       __func__, ctx_euid, pid);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
344
345
346
347
  		goto unlock;
  	}
  	if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  		rc = -EINVAL;
f66e883eb   Michael Halcrow   eCryptfs: integra...
348
349
350
  		printk(KERN_WARNING "%s: Desired context element is not "
  		       "pending a response
  ", __func__);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
351
352
353
  		goto unlock;
  	} else if (msg_ctx->counter != seq) {
  		rc = -EINVAL;
f66e883eb   Michael Halcrow   eCryptfs: integra...
354
355
356
357
  		printk(KERN_WARNING "%s: Invalid message sequence; "
  		       "expected [%d]; received [%d]
  ", __func__,
  		       msg_ctx->counter, seq);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
358
359
  		goto unlock;
  	}
f66e883eb   Michael Halcrow   eCryptfs: integra...
360
  	msg_size = (sizeof(*msg) + msg->data_len);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
361
362
363
  	msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  	if (!msg_ctx->msg) {
  		rc = -ENOMEM;
df261c52a   Michael Halcrow   eCryptfs: Replace...
364
  		printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
f66e883eb   Michael Halcrow   eCryptfs: integra...
365
366
  		       "GFP_KERNEL memory
  ", __func__, msg_size);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  		goto unlock;
  	}
  	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);
  unlock:
  	mutex_unlock(&msg_ctx->mux);
  out:
  	return rc;
  }
  
  /**
f66e883eb   Michael Halcrow   eCryptfs: integra...
381
   * ecryptfs_send_message_locked
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
382
383
384
   * @data: The data to send
   * @data_len: The length of data
   * @msg_ctx: The message context allocated for the send
f66e883eb   Michael Halcrow   eCryptfs: integra...
385
386
387
388
   *
   * Must be called with ecryptfs_daemon_hash_mux held.
   *
   * Returns zero on success; non-zero otherwise
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
389
   */
f66e883eb   Michael Halcrow   eCryptfs: integra...
390
  static int
624ae5284   Tyler Hicks   eCryptfs: remove ...
391
392
  ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  			     struct ecryptfs_msg_ctx **msg_ctx)
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
393
  {
f66e883eb   Michael Halcrow   eCryptfs: integra...
394
  	struct ecryptfs_daemon *daemon;
4eea03539   David Howells   CRED: Wrap task c...
395
  	uid_t euid = current_euid();
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
396
  	int rc;
18b6e0414   Serge Hallyn   User namespaces: ...
397
  	rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
f66e883eb   Michael Halcrow   eCryptfs: integra...
398
  	if (rc || !daemon) {
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
399
  		rc = -ENOTCONN;
f66e883eb   Michael Halcrow   eCryptfs: integra...
400
  		printk(KERN_ERR "%s: User [%d] does not have a daemon "
4eea03539   David Howells   CRED: Wrap task c...
401
402
  		       "registered
  ", __func__, euid);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
403
404
  		goto out;
  	}
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
405
406
407
408
  	mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  	rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  	if (rc) {
  		mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
f66e883eb   Michael Halcrow   eCryptfs: integra...
409
410
411
  		printk(KERN_WARNING "%s: Could not claim a free "
  		       "context element
  ", __func__);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
412
413
414
415
416
  		goto out;
  	}
  	ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  	mutex_unlock(&(*msg_ctx)->mux);
  	mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
624ae5284   Tyler Hicks   eCryptfs: remove ...
417
418
  	rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
  				   daemon);
f66e883eb   Michael Halcrow   eCryptfs: integra...
419
420
421
422
  	if (rc)
  		printk(KERN_ERR "%s: Error attempting to send message to "
  		       "userspace daemon; rc = [%d]
  ", __func__, rc);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
423
424
425
426
427
  out:
  	return rc;
  }
  
  /**
f66e883eb   Michael Halcrow   eCryptfs: integra...
428
   * ecryptfs_send_message
f66e883eb   Michael Halcrow   eCryptfs: integra...
429
430
431
432
433
434
435
436
   * @data: The data to send
   * @data_len: The length of data
   * @msg_ctx: The message context allocated for the send
   *
   * Grabs ecryptfs_daemon_hash_mux.
   *
   * Returns zero on success; non-zero otherwise
   */
624ae5284   Tyler Hicks   eCryptfs: remove ...
437
  int ecryptfs_send_message(char *data, int data_len,
f66e883eb   Michael Halcrow   eCryptfs: integra...
438
439
440
441
442
  			  struct ecryptfs_msg_ctx **msg_ctx)
  {
  	int rc;
  
  	mutex_lock(&ecryptfs_daemon_hash_mux);
624ae5284   Tyler Hicks   eCryptfs: remove ...
443
444
  	rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
  					  msg_ctx);
f66e883eb   Michael Halcrow   eCryptfs: integra...
445
446
447
448
449
  	mutex_unlock(&ecryptfs_daemon_hash_mux);
  	return rc;
  }
  
  /**
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
450
451
452
453
454
455
456
457
   * ecryptfs_wait_for_response
   * @msg_ctx: The context that was assigned when sending a message
   * @msg: The incoming message from userspace; not set if rc != 0
   *
   * Sleeps until awaken by ecryptfs_receive_message or until the amount
   * of time exceeds ecryptfs_message_wait_timeout.  If zero is
   * returned, msg will point to a valid message from userspace; a
   * non-zero value is returned upon failure to receive a message or an
f66e883eb   Michael Halcrow   eCryptfs: integra...
458
   * error occurs. Callee must free @msg on success.
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
   */
  int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  			       struct ecryptfs_message **msg)
  {
  	signed long timeout = ecryptfs_message_wait_timeout * HZ;
  	int rc = 0;
  
  sleep:
  	timeout = schedule_timeout_interruptible(timeout);
  	mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  	mutex_lock(&msg_ctx->mux);
  	if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  		if (timeout) {
  			mutex_unlock(&msg_ctx->mux);
  			mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  			goto sleep;
  		}
  		rc = -ENOMSG;
  	} else {
  		*msg = msg_ctx->msg;
  		msg_ctx->msg = NULL;
  	}
  	ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  	mutex_unlock(&msg_ctx->mux);
  	mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  	return rc;
  }
624ae5284   Tyler Hicks   eCryptfs: remove ...
486
  int ecryptfs_init_messaging(void)
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
487
488
489
490
491
492
  {
  	int i;
  	int rc = 0;
  
  	if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  		ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
f66e883eb   Michael Halcrow   eCryptfs: integra...
493
494
495
496
  		printk(KERN_WARNING "%s: Specified number of users is "
  		       "too large, defaulting to [%d] users
  ", __func__,
  		       ecryptfs_number_of_users);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
497
  	}
f66e883eb   Michael Halcrow   eCryptfs: integra...
498
499
  	mutex_init(&ecryptfs_daemon_hash_mux);
  	mutex_lock(&ecryptfs_daemon_hash_mux);
5dda6992a   Michael Halcrow   eCryptfs: remove ...
500
501
502
  	ecryptfs_hash_buckets = 1;
  	while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
  		ecryptfs_hash_buckets++;
f66e883eb   Michael Halcrow   eCryptfs: integra...
503
504
505
  	ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
  					* ecryptfs_hash_buckets), GFP_KERNEL);
  	if (!ecryptfs_daemon_hash) {
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
506
  		rc = -ENOMEM;
f66e883eb   Michael Halcrow   eCryptfs: integra...
507
508
509
  		printk(KERN_ERR "%s: Failed to allocate memory
  ", __func__);
  		mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
510
511
512
  		goto out;
  	}
  	for (i = 0; i < ecryptfs_hash_buckets; i++)
f66e883eb   Michael Halcrow   eCryptfs: integra...
513
514
  		INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
  	mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
515
  	ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
f66e883eb   Michael Halcrow   eCryptfs: integra...
516
517
  					* ecryptfs_message_buf_len),
  				       GFP_KERNEL);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
518
519
  	if (!ecryptfs_msg_ctx_arr) {
  		rc = -ENOMEM;
f66e883eb   Michael Halcrow   eCryptfs: integra...
520
521
  		printk(KERN_ERR "%s: Failed to allocate memory
  ", __func__);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
522
523
524
525
526
527
528
  		goto out;
  	}
  	mutex_init(&ecryptfs_msg_ctx_lists_mux);
  	mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  	ecryptfs_msg_counter = 0;
  	for (i = 0; i < ecryptfs_message_buf_len; i++) {
  		INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
f66e883eb   Michael Halcrow   eCryptfs: integra...
529
  		INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
530
531
532
533
534
535
536
537
538
539
540
541
  		mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  		mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  		ecryptfs_msg_ctx_arr[i].index = i;
  		ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  		ecryptfs_msg_ctx_arr[i].counter = 0;
  		ecryptfs_msg_ctx_arr[i].task = NULL;
  		ecryptfs_msg_ctx_arr[i].msg = NULL;
  		list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  			      &ecryptfs_msg_ctx_free_list);
  		mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  	}
  	mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
624ae5284   Tyler Hicks   eCryptfs: remove ...
542
543
544
  	rc = ecryptfs_init_ecryptfs_miscdev();
  	if (rc)
  		ecryptfs_release_messaging();
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
545
546
547
  out:
  	return rc;
  }
624ae5284   Tyler Hicks   eCryptfs: remove ...
548
  void ecryptfs_release_messaging(void)
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
549
550
551
552
553
554
555
556
557
558
559
560
561
562
  {
  	if (ecryptfs_msg_ctx_arr) {
  		int i;
  
  		mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  		for (i = 0; i < ecryptfs_message_buf_len; i++) {
  			mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  			if (ecryptfs_msg_ctx_arr[i].msg)
  				kfree(ecryptfs_msg_ctx_arr[i].msg);
  			mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  		}
  		kfree(ecryptfs_msg_ctx_arr);
  		mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  	}
f66e883eb   Michael Halcrow   eCryptfs: integra...
563
  	if (ecryptfs_daemon_hash) {
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
564
  		struct hlist_node *elem;
f66e883eb   Michael Halcrow   eCryptfs: integra...
565
  		struct ecryptfs_daemon *daemon;
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
566
  		int i;
f66e883eb   Michael Halcrow   eCryptfs: integra...
567
  		mutex_lock(&ecryptfs_daemon_hash_mux);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
568
  		for (i = 0; i < ecryptfs_hash_buckets; i++) {
f66e883eb   Michael Halcrow   eCryptfs: integra...
569
570
571
572
573
574
575
576
577
578
579
580
581
  			int rc;
  
  			hlist_for_each_entry(daemon, elem,
  					     &ecryptfs_daemon_hash[i],
  					     euid_chain) {
  				rc = ecryptfs_exorcise_daemon(daemon);
  				if (rc)
  					printk(KERN_ERR "%s: Error whilst "
  					       "attempting to destroy daemon; "
  					       "rc = [%d]. Dazed and confused, "
  					       "but trying to continue.
  ",
  					       __func__, rc);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
582
583
  			}
  		}
f66e883eb   Michael Halcrow   eCryptfs: integra...
584
585
  		kfree(ecryptfs_daemon_hash);
  		mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
586
  	}
624ae5284   Tyler Hicks   eCryptfs: remove ...
587
  	ecryptfs_destroy_ecryptfs_miscdev();
88b4a07e6   Michael Halcrow   [PATCH] eCryptfs:...
588
589
  	return;
  }