messaging.c 15 KB

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