messaging.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 2004-2006 International Business Machines Corp.
  5. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  6. * Tyler Hicks <tyhicks@ou.edu>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <linux/sched.h>
  23. #include "ecryptfs_kernel.h"
  24. static LIST_HEAD(ecryptfs_msg_ctx_free_list);
  25. static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
  26. static struct mutex ecryptfs_msg_ctx_lists_mux;
  27. static struct hlist_head *ecryptfs_daemon_id_hash;
  28. static struct mutex ecryptfs_daemon_id_hash_mux;
  29. static int ecryptfs_hash_buckets;
  30. #define ecryptfs_uid_hash(uid) \
  31. hash_long((unsigned long)uid, ecryptfs_hash_buckets)
  32. static unsigned int ecryptfs_msg_counter;
  33. static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
  34. /**
  35. * ecryptfs_acquire_free_msg_ctx
  36. * @msg_ctx: The context that was acquired from the free list
  37. *
  38. * Acquires a context element from the free list and locks the mutex
  39. * on the context. Returns zero on success; non-zero on error or upon
  40. * failure to acquire a free context element. Be sure to lock the
  41. * list mutex before calling.
  42. */
  43. static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
  44. {
  45. struct list_head *p;
  46. int rc;
  47. if (list_empty(&ecryptfs_msg_ctx_free_list)) {
  48. ecryptfs_printk(KERN_WARNING, "The eCryptfs free "
  49. "context list is empty. It may be helpful to "
  50. "specify the ecryptfs_message_buf_len "
  51. "parameter to be greater than the current "
  52. "value of [%d]\n", ecryptfs_message_buf_len);
  53. rc = -ENOMEM;
  54. goto out;
  55. }
  56. list_for_each(p, &ecryptfs_msg_ctx_free_list) {
  57. *msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
  58. if (mutex_trylock(&(*msg_ctx)->mux)) {
  59. (*msg_ctx)->task = current;
  60. rc = 0;
  61. goto out;
  62. }
  63. }
  64. rc = -ENOMEM;
  65. out:
  66. return rc;
  67. }
  68. /**
  69. * ecryptfs_msg_ctx_free_to_alloc
  70. * @msg_ctx: The context to move from the free list to the alloc list
  71. *
  72. * Be sure to lock the list mutex and the context mutex before
  73. * calling.
  74. */
  75. static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
  76. {
  77. list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
  78. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
  79. msg_ctx->counter = ++ecryptfs_msg_counter;
  80. }
  81. /**
  82. * ecryptfs_msg_ctx_alloc_to_free
  83. * @msg_ctx: The context to move from the alloc list to the free list
  84. *
  85. * Be sure to lock the list mutex and the context mutex before
  86. * calling.
  87. */
  88. static void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
  89. {
  90. list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
  91. if (msg_ctx->msg)
  92. kfree(msg_ctx->msg);
  93. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
  94. }
  95. /**
  96. * ecryptfs_find_daemon_id
  97. * @uid: The user id which maps to the desired daemon id
  98. * @id: If return value is zero, points to the desired daemon id
  99. * pointer
  100. *
  101. * Search the hash list for the given user id. Returns zero if the
  102. * user id exists in the list; non-zero otherwise. The daemon id hash
  103. * mutex should be held before calling this function.
  104. */
  105. static int ecryptfs_find_daemon_id(uid_t uid, struct ecryptfs_daemon_id **id)
  106. {
  107. struct hlist_node *elem;
  108. int rc;
  109. hlist_for_each_entry(*id, elem,
  110. &ecryptfs_daemon_id_hash[ecryptfs_uid_hash(uid)],
  111. id_chain) {
  112. if ((*id)->uid == uid) {
  113. rc = 0;
  114. goto out;
  115. }
  116. }
  117. rc = -EINVAL;
  118. out:
  119. return rc;
  120. }
  121. static int ecryptfs_send_raw_message(unsigned int transport, u16 msg_type,
  122. pid_t pid)
  123. {
  124. int rc;
  125. switch(transport) {
  126. case ECRYPTFS_TRANSPORT_NETLINK:
  127. rc = ecryptfs_send_netlink(NULL, 0, NULL, msg_type, 0, pid);
  128. break;
  129. case ECRYPTFS_TRANSPORT_CONNECTOR:
  130. case ECRYPTFS_TRANSPORT_RELAYFS:
  131. default:
  132. rc = -ENOSYS;
  133. }
  134. return rc;
  135. }
  136. /**
  137. * ecryptfs_process_helo
  138. * @transport: The underlying transport (netlink, etc.)
  139. * @uid: The user ID owner of the message
  140. * @pid: The process ID for the userspace program that sent the
  141. * message
  142. *
  143. * Adds the uid and pid values to the daemon id hash. If a uid
  144. * already has a daemon pid registered, the daemon will be
  145. * unregistered before the new daemon id is put into the hash list.
  146. * Returns zero after adding a new daemon id to the hash list;
  147. * non-zero otherwise.
  148. */
  149. int ecryptfs_process_helo(unsigned int transport, uid_t uid, pid_t pid)
  150. {
  151. struct ecryptfs_daemon_id *new_id;
  152. struct ecryptfs_daemon_id *old_id;
  153. int rc;
  154. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  155. new_id = kmalloc(sizeof(*new_id), GFP_KERNEL);
  156. if (!new_id) {
  157. rc = -ENOMEM;
  158. ecryptfs_printk(KERN_ERR, "Failed to allocate memory; unable "
  159. "to register daemon [%d] for user [%d]\n",
  160. pid, uid);
  161. goto unlock;
  162. }
  163. if (!ecryptfs_find_daemon_id(uid, &old_id)) {
  164. printk(KERN_WARNING "Received request from user [%d] "
  165. "to register daemon [%d]; unregistering daemon "
  166. "[%d]\n", uid, pid, old_id->pid);
  167. hlist_del(&old_id->id_chain);
  168. rc = ecryptfs_send_raw_message(transport, ECRYPTFS_NLMSG_QUIT,
  169. old_id->pid);
  170. if (rc)
  171. printk(KERN_WARNING "Failed to send QUIT "
  172. "message to daemon [%d]; rc = [%d]\n",
  173. old_id->pid, rc);
  174. kfree(old_id);
  175. }
  176. new_id->uid = uid;
  177. new_id->pid = pid;
  178. hlist_add_head(&new_id->id_chain,
  179. &ecryptfs_daemon_id_hash[ecryptfs_uid_hash(uid)]);
  180. rc = 0;
  181. unlock:
  182. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  183. return rc;
  184. }
  185. /**
  186. * ecryptfs_process_quit
  187. * @uid: The user ID owner of the message
  188. * @pid: The process ID for the userspace program that sent the
  189. * message
  190. *
  191. * Deletes the corresponding daemon id for the given uid and pid, if
  192. * it is the registered that is requesting the deletion. Returns zero
  193. * after deleting the desired daemon id; non-zero otherwise.
  194. */
  195. int ecryptfs_process_quit(uid_t uid, pid_t pid)
  196. {
  197. struct ecryptfs_daemon_id *id;
  198. int rc;
  199. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  200. if (ecryptfs_find_daemon_id(uid, &id)) {
  201. rc = -EINVAL;
  202. ecryptfs_printk(KERN_ERR, "Received request from user [%d] to "
  203. "unregister unrecognized daemon [%d]\n", uid,
  204. pid);
  205. goto unlock;
  206. }
  207. if (id->pid != pid) {
  208. rc = -EINVAL;
  209. ecryptfs_printk(KERN_WARNING, "Received request from user [%d] "
  210. "with pid [%d] to unregister daemon [%d]\n",
  211. uid, pid, id->pid);
  212. goto unlock;
  213. }
  214. hlist_del(&id->id_chain);
  215. kfree(id);
  216. rc = 0;
  217. unlock:
  218. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  219. return rc;
  220. }
  221. /**
  222. * ecryptfs_process_reponse
  223. * @msg: The ecryptfs message received; the caller should sanity check
  224. * msg->data_len
  225. * @pid: The process ID of the userspace application that sent the
  226. * message
  227. * @seq: The sequence number of the message
  228. *
  229. * Processes a response message after sending a operation request to
  230. * userspace. Returns zero upon delivery to desired context element;
  231. * non-zero upon delivery failure or error.
  232. */
  233. int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t uid,
  234. pid_t pid, u32 seq)
  235. {
  236. struct ecryptfs_daemon_id *id;
  237. struct ecryptfs_msg_ctx *msg_ctx;
  238. int msg_size;
  239. int rc;
  240. if (msg->index >= ecryptfs_message_buf_len) {
  241. rc = -EINVAL;
  242. ecryptfs_printk(KERN_ERR, "Attempt to reference "
  243. "context buffer at index [%d]; maximum "
  244. "allowable is [%d]\n", msg->index,
  245. (ecryptfs_message_buf_len - 1));
  246. goto out;
  247. }
  248. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  249. mutex_lock(&msg_ctx->mux);
  250. if (ecryptfs_find_daemon_id(msg_ctx->task->euid, &id)) {
  251. rc = -EBADMSG;
  252. ecryptfs_printk(KERN_WARNING, "User [%d] received a "
  253. "message response from process [%d] but does "
  254. "not have a registered daemon\n",
  255. msg_ctx->task->euid, pid);
  256. goto wake_up;
  257. }
  258. if (msg_ctx->task->euid != uid) {
  259. rc = -EBADMSG;
  260. ecryptfs_printk(KERN_WARNING, "Received message from user "
  261. "[%d]; expected message from user [%d]\n",
  262. uid, msg_ctx->task->euid);
  263. goto unlock;
  264. }
  265. if (id->pid != pid) {
  266. rc = -EBADMSG;
  267. ecryptfs_printk(KERN_ERR, "User [%d] received a "
  268. "message response from an unrecognized "
  269. "process [%d]\n", msg_ctx->task->euid, pid);
  270. goto unlock;
  271. }
  272. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  273. rc = -EINVAL;
  274. ecryptfs_printk(KERN_WARNING, "Desired context element is not "
  275. "pending a response\n");
  276. goto unlock;
  277. } else if (msg_ctx->counter != seq) {
  278. rc = -EINVAL;
  279. ecryptfs_printk(KERN_WARNING, "Invalid message sequence; "
  280. "expected [%d]; received [%d]\n",
  281. msg_ctx->counter, seq);
  282. goto unlock;
  283. }
  284. msg_size = sizeof(*msg) + msg->data_len;
  285. msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  286. if (!msg_ctx->msg) {
  287. rc = -ENOMEM;
  288. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  289. goto unlock;
  290. }
  291. memcpy(msg_ctx->msg, msg, msg_size);
  292. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  293. rc = 0;
  294. wake_up:
  295. wake_up_process(msg_ctx->task);
  296. unlock:
  297. mutex_unlock(&msg_ctx->mux);
  298. out:
  299. return rc;
  300. }
  301. /**
  302. * ecryptfs_send_message
  303. * @transport: The transport over which to send the message (i.e.,
  304. * netlink)
  305. * @data: The data to send
  306. * @data_len: The length of data
  307. * @msg_ctx: The message context allocated for the send
  308. */
  309. int ecryptfs_send_message(unsigned int transport, char *data, int data_len,
  310. struct ecryptfs_msg_ctx **msg_ctx)
  311. {
  312. struct ecryptfs_daemon_id *id;
  313. int rc;
  314. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  315. if (ecryptfs_find_daemon_id(current->euid, &id)) {
  316. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  317. rc = -ENOTCONN;
  318. ecryptfs_printk(KERN_ERR, "User [%d] does not have a daemon "
  319. "registered\n", current->euid);
  320. goto out;
  321. }
  322. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  323. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  324. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  325. if (rc) {
  326. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  327. ecryptfs_printk(KERN_WARNING, "Could not claim a free "
  328. "context element\n");
  329. goto out;
  330. }
  331. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  332. mutex_unlock(&(*msg_ctx)->mux);
  333. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  334. switch (transport) {
  335. case ECRYPTFS_TRANSPORT_NETLINK:
  336. rc = ecryptfs_send_netlink(data, data_len, *msg_ctx,
  337. ECRYPTFS_NLMSG_REQUEST, 0, id->pid);
  338. break;
  339. case ECRYPTFS_TRANSPORT_CONNECTOR:
  340. case ECRYPTFS_TRANSPORT_RELAYFS:
  341. default:
  342. rc = -ENOSYS;
  343. }
  344. if (rc) {
  345. printk(KERN_ERR "Error attempting to send message to userspace "
  346. "daemon; rc = [%d]\n", rc);
  347. }
  348. out:
  349. return rc;
  350. }
  351. /**
  352. * ecryptfs_wait_for_response
  353. * @msg_ctx: The context that was assigned when sending a message
  354. * @msg: The incoming message from userspace; not set if rc != 0
  355. *
  356. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  357. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  358. * returned, msg will point to a valid message from userspace; a
  359. * non-zero value is returned upon failure to receive a message or an
  360. * error occurs.
  361. */
  362. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  363. struct ecryptfs_message **msg)
  364. {
  365. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  366. int rc = 0;
  367. sleep:
  368. timeout = schedule_timeout_interruptible(timeout);
  369. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  370. mutex_lock(&msg_ctx->mux);
  371. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  372. if (timeout) {
  373. mutex_unlock(&msg_ctx->mux);
  374. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  375. goto sleep;
  376. }
  377. rc = -ENOMSG;
  378. } else {
  379. *msg = msg_ctx->msg;
  380. msg_ctx->msg = NULL;
  381. }
  382. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  383. mutex_unlock(&msg_ctx->mux);
  384. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  385. return rc;
  386. }
  387. int ecryptfs_init_messaging(unsigned int transport)
  388. {
  389. int i;
  390. int rc = 0;
  391. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  392. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  393. ecryptfs_printk(KERN_WARNING, "Specified number of users is "
  394. "too large, defaulting to [%d] users\n",
  395. ecryptfs_number_of_users);
  396. }
  397. mutex_init(&ecryptfs_daemon_id_hash_mux);
  398. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  399. ecryptfs_hash_buckets = 1;
  400. while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
  401. ecryptfs_hash_buckets++;
  402. ecryptfs_daemon_id_hash = kmalloc(sizeof(struct hlist_head)
  403. * ecryptfs_hash_buckets, GFP_KERNEL);
  404. if (!ecryptfs_daemon_id_hash) {
  405. rc = -ENOMEM;
  406. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  407. goto out;
  408. }
  409. for (i = 0; i < ecryptfs_hash_buckets; i++)
  410. INIT_HLIST_HEAD(&ecryptfs_daemon_id_hash[i]);
  411. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  412. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  413. * ecryptfs_message_buf_len), GFP_KERNEL);
  414. if (!ecryptfs_msg_ctx_arr) {
  415. rc = -ENOMEM;
  416. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  417. goto out;
  418. }
  419. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  420. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  421. ecryptfs_msg_counter = 0;
  422. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  423. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  424. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  425. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  426. ecryptfs_msg_ctx_arr[i].index = i;
  427. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  428. ecryptfs_msg_ctx_arr[i].counter = 0;
  429. ecryptfs_msg_ctx_arr[i].task = NULL;
  430. ecryptfs_msg_ctx_arr[i].msg = NULL;
  431. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  432. &ecryptfs_msg_ctx_free_list);
  433. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  434. }
  435. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  436. switch(transport) {
  437. case ECRYPTFS_TRANSPORT_NETLINK:
  438. rc = ecryptfs_init_netlink();
  439. if (rc)
  440. ecryptfs_release_messaging(transport);
  441. break;
  442. case ECRYPTFS_TRANSPORT_CONNECTOR:
  443. case ECRYPTFS_TRANSPORT_RELAYFS:
  444. default:
  445. rc = -ENOSYS;
  446. }
  447. out:
  448. return rc;
  449. }
  450. void ecryptfs_release_messaging(unsigned int transport)
  451. {
  452. if (ecryptfs_msg_ctx_arr) {
  453. int i;
  454. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  455. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  456. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  457. if (ecryptfs_msg_ctx_arr[i].msg)
  458. kfree(ecryptfs_msg_ctx_arr[i].msg);
  459. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  460. }
  461. kfree(ecryptfs_msg_ctx_arr);
  462. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  463. }
  464. if (ecryptfs_daemon_id_hash) {
  465. struct hlist_node *elem;
  466. struct ecryptfs_daemon_id *id;
  467. int i;
  468. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  469. for (i = 0; i < ecryptfs_hash_buckets; i++) {
  470. hlist_for_each_entry(id, elem,
  471. &ecryptfs_daemon_id_hash[i],
  472. id_chain) {
  473. hlist_del(elem);
  474. kfree(id);
  475. }
  476. }
  477. kfree(ecryptfs_daemon_id_hash);
  478. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  479. }
  480. switch(transport) {
  481. case ECRYPTFS_TRANSPORT_NETLINK:
  482. ecryptfs_release_netlink();
  483. break;
  484. case ECRYPTFS_TRANSPORT_CONNECTOR:
  485. case ECRYPTFS_TRANSPORT_RELAYFS:
  486. default:
  487. break;
  488. }
  489. return;
  490. }