messaging.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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, pid_t pid, u32 seq)
  230. {
  231. struct ecryptfs_daemon_id *id;
  232. struct ecryptfs_msg_ctx *msg_ctx;
  233. int msg_size;
  234. int rc;
  235. if (msg->index >= ecryptfs_message_buf_len) {
  236. rc = -EINVAL;
  237. ecryptfs_printk(KERN_ERR, "Attempt to reference "
  238. "context buffer at index [%d]; maximum "
  239. "allowable is [%d]\n", msg->index,
  240. (ecryptfs_message_buf_len - 1));
  241. goto out;
  242. }
  243. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  244. mutex_lock(&msg_ctx->mux);
  245. if (ecryptfs_find_daemon_id(msg_ctx->task->euid, &id)) {
  246. rc = -EBADMSG;
  247. ecryptfs_printk(KERN_WARNING, "User [%d] received a "
  248. "message response from process [%d] but does "
  249. "not have a registered daemon\n",
  250. msg_ctx->task->euid, pid);
  251. goto wake_up;
  252. }
  253. if (id->pid != pid) {
  254. rc = -EBADMSG;
  255. ecryptfs_printk(KERN_ERR, "User [%d] received a "
  256. "message response from an unrecognized "
  257. "process [%d]\n", msg_ctx->task->euid, pid);
  258. goto unlock;
  259. }
  260. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  261. rc = -EINVAL;
  262. ecryptfs_printk(KERN_WARNING, "Desired context element is not "
  263. "pending a response\n");
  264. goto unlock;
  265. } else if (msg_ctx->counter != seq) {
  266. rc = -EINVAL;
  267. ecryptfs_printk(KERN_WARNING, "Invalid message sequence; "
  268. "expected [%d]; received [%d]\n",
  269. msg_ctx->counter, seq);
  270. goto unlock;
  271. }
  272. msg_size = sizeof(*msg) + msg->data_len;
  273. msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  274. if (!msg_ctx->msg) {
  275. rc = -ENOMEM;
  276. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  277. goto unlock;
  278. }
  279. memcpy(msg_ctx->msg, msg, msg_size);
  280. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  281. rc = 0;
  282. wake_up:
  283. wake_up_process(msg_ctx->task);
  284. unlock:
  285. mutex_unlock(&msg_ctx->mux);
  286. out:
  287. return rc;
  288. }
  289. /**
  290. * ecryptfs_send_message
  291. * @transport: The transport over which to send the message (i.e.,
  292. * netlink)
  293. * @data: The data to send
  294. * @data_len: The length of data
  295. * @msg_ctx: The message context allocated for the send
  296. */
  297. int ecryptfs_send_message(unsigned int transport, char *data, int data_len,
  298. struct ecryptfs_msg_ctx **msg_ctx)
  299. {
  300. struct ecryptfs_daemon_id *id;
  301. int rc;
  302. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  303. if (ecryptfs_find_daemon_id(current->euid, &id)) {
  304. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  305. rc = -ENOTCONN;
  306. ecryptfs_printk(KERN_ERR, "User [%d] does not have a daemon "
  307. "registered\n", current->euid);
  308. goto out;
  309. }
  310. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  311. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  312. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  313. if (rc) {
  314. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  315. ecryptfs_printk(KERN_WARNING, "Could not claim a free "
  316. "context element\n");
  317. goto out;
  318. }
  319. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  320. mutex_unlock(&(*msg_ctx)->mux);
  321. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  322. switch (transport) {
  323. case ECRYPTFS_TRANSPORT_NETLINK:
  324. rc = ecryptfs_send_netlink(data, data_len, *msg_ctx,
  325. ECRYPTFS_NLMSG_REQUEST, 0, id->pid);
  326. break;
  327. case ECRYPTFS_TRANSPORT_CONNECTOR:
  328. case ECRYPTFS_TRANSPORT_RELAYFS:
  329. default:
  330. rc = -ENOSYS;
  331. }
  332. if (rc) {
  333. printk(KERN_ERR "Error attempting to send message to userspace "
  334. "daemon; rc = [%d]\n", rc);
  335. }
  336. out:
  337. return rc;
  338. }
  339. /**
  340. * ecryptfs_wait_for_response
  341. * @msg_ctx: The context that was assigned when sending a message
  342. * @msg: The incoming message from userspace; not set if rc != 0
  343. *
  344. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  345. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  346. * returned, msg will point to a valid message from userspace; a
  347. * non-zero value is returned upon failure to receive a message or an
  348. * error occurs.
  349. */
  350. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  351. struct ecryptfs_message **msg)
  352. {
  353. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  354. int rc = 0;
  355. sleep:
  356. timeout = schedule_timeout_interruptible(timeout);
  357. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  358. mutex_lock(&msg_ctx->mux);
  359. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  360. if (timeout) {
  361. mutex_unlock(&msg_ctx->mux);
  362. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  363. goto sleep;
  364. }
  365. rc = -ENOMSG;
  366. } else {
  367. *msg = msg_ctx->msg;
  368. msg_ctx->msg = NULL;
  369. }
  370. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  371. mutex_unlock(&msg_ctx->mux);
  372. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  373. return rc;
  374. }
  375. int ecryptfs_init_messaging(unsigned int transport)
  376. {
  377. int i;
  378. int rc = 0;
  379. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  380. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  381. ecryptfs_printk(KERN_WARNING, "Specified number of users is "
  382. "too large, defaulting to [%d] users\n",
  383. ecryptfs_number_of_users);
  384. }
  385. mutex_init(&ecryptfs_daemon_id_hash_mux);
  386. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  387. ecryptfs_hash_buckets = 0;
  388. while (ecryptfs_number_of_users >> ++ecryptfs_hash_buckets);
  389. ecryptfs_daemon_id_hash = kmalloc(sizeof(struct hlist_head)
  390. * ecryptfs_hash_buckets, GFP_KERNEL);
  391. if (!ecryptfs_daemon_id_hash) {
  392. rc = -ENOMEM;
  393. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  394. goto out;
  395. }
  396. for (i = 0; i < ecryptfs_hash_buckets; i++)
  397. INIT_HLIST_HEAD(&ecryptfs_daemon_id_hash[i]);
  398. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  399. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  400. * ecryptfs_message_buf_len), GFP_KERNEL);
  401. if (!ecryptfs_msg_ctx_arr) {
  402. rc = -ENOMEM;
  403. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  404. goto out;
  405. }
  406. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  407. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  408. ecryptfs_msg_counter = 0;
  409. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  410. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  411. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  412. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  413. ecryptfs_msg_ctx_arr[i].index = i;
  414. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  415. ecryptfs_msg_ctx_arr[i].counter = 0;
  416. ecryptfs_msg_ctx_arr[i].task = NULL;
  417. ecryptfs_msg_ctx_arr[i].msg = NULL;
  418. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  419. &ecryptfs_msg_ctx_free_list);
  420. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  421. }
  422. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  423. switch(transport) {
  424. case ECRYPTFS_TRANSPORT_NETLINK:
  425. rc = ecryptfs_init_netlink();
  426. if (rc)
  427. ecryptfs_release_messaging(transport);
  428. break;
  429. case ECRYPTFS_TRANSPORT_CONNECTOR:
  430. case ECRYPTFS_TRANSPORT_RELAYFS:
  431. default:
  432. rc = -ENOSYS;
  433. }
  434. out:
  435. return rc;
  436. }
  437. void ecryptfs_release_messaging(unsigned int transport)
  438. {
  439. if (ecryptfs_msg_ctx_arr) {
  440. int i;
  441. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  442. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  443. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  444. if (ecryptfs_msg_ctx_arr[i].msg)
  445. kfree(ecryptfs_msg_ctx_arr[i].msg);
  446. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  447. }
  448. kfree(ecryptfs_msg_ctx_arr);
  449. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  450. }
  451. if (ecryptfs_daemon_id_hash) {
  452. struct hlist_node *elem;
  453. struct ecryptfs_daemon_id *id;
  454. int i;
  455. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  456. for (i = 0; i < ecryptfs_hash_buckets; i++) {
  457. hlist_for_each_entry(id, elem,
  458. &ecryptfs_daemon_id_hash[i],
  459. id_chain) {
  460. hlist_del(elem);
  461. kfree(id);
  462. }
  463. }
  464. kfree(ecryptfs_daemon_id_hash);
  465. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  466. }
  467. switch(transport) {
  468. case ECRYPTFS_TRANSPORT_NETLINK:
  469. ecryptfs_release_netlink();
  470. break;
  471. case ECRYPTFS_TRANSPORT_CONNECTOR:
  472. case ECRYPTFS_TRANSPORT_RELAYFS:
  473. default:
  474. break;
  475. }
  476. return;
  477. }