messaging.c 15 KB

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