messaging.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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\n", pid, uid);
  159. goto unlock;
  160. }
  161. if (!ecryptfs_find_daemon_id(uid, &old_id)) {
  162. printk(KERN_WARNING "Received request from user [%d] "
  163. "to register daemon [%d]; unregistering daemon "
  164. "[%d]\n", uid, pid, old_id->pid);
  165. hlist_del(&old_id->id_chain);
  166. rc = ecryptfs_send_raw_message(transport, ECRYPTFS_NLMSG_QUIT,
  167. old_id->pid);
  168. if (rc)
  169. printk(KERN_WARNING "Failed to send QUIT "
  170. "message to daemon [%d]; rc = [%d]\n",
  171. old_id->pid, rc);
  172. kfree(old_id);
  173. }
  174. new_id->uid = uid;
  175. new_id->pid = pid;
  176. hlist_add_head(&new_id->id_chain,
  177. &ecryptfs_daemon_id_hash[ecryptfs_uid_hash(uid)]);
  178. rc = 0;
  179. unlock:
  180. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  181. return rc;
  182. }
  183. /**
  184. * ecryptfs_process_quit
  185. * @uid: The user ID owner of the message
  186. * @pid: The process ID for the userspace program that sent the
  187. * message
  188. *
  189. * Deletes the corresponding daemon id for the given uid and pid, if
  190. * it is the registered that is requesting the deletion. Returns zero
  191. * after deleting the desired daemon id; non-zero otherwise.
  192. */
  193. int ecryptfs_process_quit(uid_t uid, pid_t pid)
  194. {
  195. struct ecryptfs_daemon_id *id;
  196. int rc;
  197. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  198. if (ecryptfs_find_daemon_id(uid, &id)) {
  199. rc = -EINVAL;
  200. ecryptfs_printk(KERN_ERR, "Received request from user [%d] to "
  201. "unregister unrecognized daemon [%d]\n", uid,
  202. pid);
  203. goto unlock;
  204. }
  205. if (id->pid != pid) {
  206. rc = -EINVAL;
  207. ecryptfs_printk(KERN_WARNING, "Received request from user [%d] "
  208. "with pid [%d] to unregister daemon [%d]\n",
  209. uid, pid, id->pid);
  210. goto unlock;
  211. }
  212. hlist_del(&id->id_chain);
  213. kfree(id);
  214. rc = 0;
  215. unlock:
  216. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  217. return rc;
  218. }
  219. /**
  220. * ecryptfs_process_reponse
  221. * @msg: The ecryptfs message received; the caller should sanity check
  222. * msg->data_len
  223. * @pid: The process ID of the userspace application that sent the
  224. * message
  225. * @seq: The sequence number of the message
  226. *
  227. * Processes a response message after sending a operation request to
  228. * userspace. Returns zero upon delivery to desired context element;
  229. * non-zero upon delivery failure or error.
  230. */
  231. int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t uid,
  232. pid_t pid, u32 seq)
  233. {
  234. struct ecryptfs_daemon_id *id;
  235. struct ecryptfs_msg_ctx *msg_ctx;
  236. int msg_size;
  237. int rc;
  238. if (msg->index >= ecryptfs_message_buf_len) {
  239. rc = -EINVAL;
  240. ecryptfs_printk(KERN_ERR, "Attempt to reference "
  241. "context buffer at index [%d]; maximum "
  242. "allowable is [%d]\n", msg->index,
  243. (ecryptfs_message_buf_len - 1));
  244. goto out;
  245. }
  246. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  247. mutex_lock(&msg_ctx->mux);
  248. if (ecryptfs_find_daemon_id(msg_ctx->task->euid, &id)) {
  249. rc = -EBADMSG;
  250. ecryptfs_printk(KERN_WARNING, "User [%d] received a "
  251. "message response from process [%d] but does "
  252. "not have a registered daemon\n",
  253. msg_ctx->task->euid, pid);
  254. goto wake_up;
  255. }
  256. if (msg_ctx->task->euid != uid) {
  257. rc = -EBADMSG;
  258. ecryptfs_printk(KERN_WARNING, "Received message from user "
  259. "[%d]; expected message from user [%d]\n",
  260. uid, msg_ctx->task->euid);
  261. goto unlock;
  262. }
  263. if (id->pid != pid) {
  264. rc = -EBADMSG;
  265. ecryptfs_printk(KERN_ERR, "User [%d] received a "
  266. "message response from an unrecognized "
  267. "process [%d]\n", msg_ctx->task->euid, pid);
  268. goto unlock;
  269. }
  270. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  271. rc = -EINVAL;
  272. ecryptfs_printk(KERN_WARNING, "Desired context element is not "
  273. "pending a response\n");
  274. goto unlock;
  275. } else if (msg_ctx->counter != seq) {
  276. rc = -EINVAL;
  277. ecryptfs_printk(KERN_WARNING, "Invalid message sequence; "
  278. "expected [%d]; received [%d]\n",
  279. msg_ctx->counter, seq);
  280. goto unlock;
  281. }
  282. msg_size = sizeof(*msg) + msg->data_len;
  283. msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  284. if (!msg_ctx->msg) {
  285. rc = -ENOMEM;
  286. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  287. goto unlock;
  288. }
  289. memcpy(msg_ctx->msg, msg, msg_size);
  290. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  291. rc = 0;
  292. wake_up:
  293. wake_up_process(msg_ctx->task);
  294. unlock:
  295. mutex_unlock(&msg_ctx->mux);
  296. out:
  297. return rc;
  298. }
  299. /**
  300. * ecryptfs_send_message
  301. * @transport: The transport over which to send the message (i.e.,
  302. * netlink)
  303. * @data: The data to send
  304. * @data_len: The length of data
  305. * @msg_ctx: The message context allocated for the send
  306. */
  307. int ecryptfs_send_message(unsigned int transport, char *data, int data_len,
  308. struct ecryptfs_msg_ctx **msg_ctx)
  309. {
  310. struct ecryptfs_daemon_id *id;
  311. int rc;
  312. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  313. if (ecryptfs_find_daemon_id(current->euid, &id)) {
  314. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  315. rc = -ENOTCONN;
  316. ecryptfs_printk(KERN_ERR, "User [%d] does not have a daemon "
  317. "registered\n", current->euid);
  318. goto out;
  319. }
  320. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  321. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  322. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  323. if (rc) {
  324. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  325. ecryptfs_printk(KERN_WARNING, "Could not claim a free "
  326. "context element\n");
  327. goto out;
  328. }
  329. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  330. mutex_unlock(&(*msg_ctx)->mux);
  331. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  332. switch (transport) {
  333. case ECRYPTFS_TRANSPORT_NETLINK:
  334. rc = ecryptfs_send_netlink(data, data_len, *msg_ctx,
  335. ECRYPTFS_NLMSG_REQUEST, 0, id->pid);
  336. break;
  337. case ECRYPTFS_TRANSPORT_CONNECTOR:
  338. case ECRYPTFS_TRANSPORT_RELAYFS:
  339. default:
  340. rc = -ENOSYS;
  341. }
  342. if (rc) {
  343. printk(KERN_ERR "Error attempting to send message to userspace "
  344. "daemon; rc = [%d]\n", rc);
  345. }
  346. out:
  347. return rc;
  348. }
  349. /**
  350. * ecryptfs_wait_for_response
  351. * @msg_ctx: The context that was assigned when sending a message
  352. * @msg: The incoming message from userspace; not set if rc != 0
  353. *
  354. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  355. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  356. * returned, msg will point to a valid message from userspace; a
  357. * non-zero value is returned upon failure to receive a message or an
  358. * error occurs.
  359. */
  360. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  361. struct ecryptfs_message **msg)
  362. {
  363. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  364. int rc = 0;
  365. sleep:
  366. timeout = schedule_timeout_interruptible(timeout);
  367. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  368. mutex_lock(&msg_ctx->mux);
  369. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  370. if (timeout) {
  371. mutex_unlock(&msg_ctx->mux);
  372. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  373. goto sleep;
  374. }
  375. rc = -ENOMSG;
  376. } else {
  377. *msg = msg_ctx->msg;
  378. msg_ctx->msg = NULL;
  379. }
  380. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  381. mutex_unlock(&msg_ctx->mux);
  382. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  383. return rc;
  384. }
  385. int ecryptfs_init_messaging(unsigned int transport)
  386. {
  387. int i;
  388. int rc = 0;
  389. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  390. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  391. ecryptfs_printk(KERN_WARNING, "Specified number of users is "
  392. "too large, defaulting to [%d] users\n",
  393. ecryptfs_number_of_users);
  394. }
  395. mutex_init(&ecryptfs_daemon_id_hash_mux);
  396. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  397. ecryptfs_hash_buckets = 0;
  398. while (ecryptfs_number_of_users >> ++ecryptfs_hash_buckets);
  399. ecryptfs_daemon_id_hash = kmalloc(sizeof(struct hlist_head)
  400. * ecryptfs_hash_buckets, GFP_KERNEL);
  401. if (!ecryptfs_daemon_id_hash) {
  402. rc = -ENOMEM;
  403. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  404. goto out;
  405. }
  406. for (i = 0; i < ecryptfs_hash_buckets; i++)
  407. INIT_HLIST_HEAD(&ecryptfs_daemon_id_hash[i]);
  408. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  409. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  410. * ecryptfs_message_buf_len), GFP_KERNEL);
  411. if (!ecryptfs_msg_ctx_arr) {
  412. rc = -ENOMEM;
  413. ecryptfs_printk(KERN_ERR, "Failed to allocate memory\n");
  414. goto out;
  415. }
  416. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  417. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  418. ecryptfs_msg_counter = 0;
  419. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  420. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  421. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  422. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  423. ecryptfs_msg_ctx_arr[i].index = i;
  424. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  425. ecryptfs_msg_ctx_arr[i].counter = 0;
  426. ecryptfs_msg_ctx_arr[i].task = NULL;
  427. ecryptfs_msg_ctx_arr[i].msg = NULL;
  428. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  429. &ecryptfs_msg_ctx_free_list);
  430. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  431. }
  432. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  433. switch(transport) {
  434. case ECRYPTFS_TRANSPORT_NETLINK:
  435. rc = ecryptfs_init_netlink();
  436. if (rc)
  437. ecryptfs_release_messaging(transport);
  438. break;
  439. case ECRYPTFS_TRANSPORT_CONNECTOR:
  440. case ECRYPTFS_TRANSPORT_RELAYFS:
  441. default:
  442. rc = -ENOSYS;
  443. }
  444. out:
  445. return rc;
  446. }
  447. void ecryptfs_release_messaging(unsigned int transport)
  448. {
  449. if (ecryptfs_msg_ctx_arr) {
  450. int i;
  451. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  452. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  453. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  454. if (ecryptfs_msg_ctx_arr[i].msg)
  455. kfree(ecryptfs_msg_ctx_arr[i].msg);
  456. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  457. }
  458. kfree(ecryptfs_msg_ctx_arr);
  459. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  460. }
  461. if (ecryptfs_daemon_id_hash) {
  462. struct hlist_node *elem;
  463. struct ecryptfs_daemon_id *id;
  464. int i;
  465. mutex_lock(&ecryptfs_daemon_id_hash_mux);
  466. for (i = 0; i < ecryptfs_hash_buckets; i++) {
  467. hlist_for_each_entry(id, elem,
  468. &ecryptfs_daemon_id_hash[i],
  469. id_chain) {
  470. hlist_del(elem);
  471. kfree(id);
  472. }
  473. }
  474. kfree(ecryptfs_daemon_id_hash);
  475. mutex_unlock(&ecryptfs_daemon_id_hash_mux);
  476. }
  477. switch(transport) {
  478. case ECRYPTFS_TRANSPORT_NETLINK:
  479. ecryptfs_release_netlink();
  480. break;
  481. case ECRYPTFS_TRANSPORT_CONNECTOR:
  482. case ECRYPTFS_TRANSPORT_RELAYFS:
  483. default:
  484. break;
  485. }
  486. return;
  487. }