util.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * linux/ipc/util.c
  3. * Copyright (C) 1992 Krishna Balasubramanian
  4. *
  5. * Sep 1997 - Call suser() last after "normal" permission checks so we
  6. * get BSD style process accounting right.
  7. * Occurs in several places in the IPC code.
  8. * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
  9. * Nov 1999 - ipc helper functions, unified SMP locking
  10. * Manfred Spraul <manfreds@colorfullife.com>
  11. * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
  12. * Mingming Cao <cmm@us.ibm.com>
  13. */
  14. #include <linux/config.h>
  15. #include <linux/mm.h>
  16. #include <linux/shm.h>
  17. #include <linux/init.h>
  18. #include <linux/msg.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/slab.h>
  22. #include <linux/highuid.h>
  23. #include <linux/security.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/workqueue.h>
  26. #include <asm/unistd.h>
  27. #include "util.h"
  28. /**
  29. * ipc_init - initialise IPC subsystem
  30. *
  31. * The various system5 IPC resources (semaphores, messages and shared
  32. * memory are initialised
  33. */
  34. static int __init ipc_init(void)
  35. {
  36. sem_init();
  37. msg_init();
  38. shm_init();
  39. return 0;
  40. }
  41. __initcall(ipc_init);
  42. /**
  43. * ipc_init_ids - initialise IPC identifiers
  44. * @ids: Identifier set
  45. * @size: Number of identifiers
  46. *
  47. * Given a size for the ipc identifier range (limited below IPCMNI)
  48. * set up the sequence range to use then allocate and initialise the
  49. * array itself.
  50. */
  51. void __init ipc_init_ids(struct ipc_ids* ids, int size)
  52. {
  53. int i;
  54. sema_init(&ids->sem,1);
  55. if(size > IPCMNI)
  56. size = IPCMNI;
  57. ids->in_use = 0;
  58. ids->max_id = -1;
  59. ids->seq = 0;
  60. {
  61. int seq_limit = INT_MAX/SEQ_MULTIPLIER;
  62. if(seq_limit > USHRT_MAX)
  63. ids->seq_max = USHRT_MAX;
  64. else
  65. ids->seq_max = seq_limit;
  66. }
  67. ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
  68. sizeof(struct ipc_id_ary));
  69. if(ids->entries == NULL) {
  70. printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
  71. size = 0;
  72. ids->entries = &ids->nullentry;
  73. }
  74. ids->entries->size = size;
  75. for(i=0;i<size;i++)
  76. ids->entries->p[i] = NULL;
  77. }
  78. /**
  79. * ipc_findkey - find a key in an ipc identifier set
  80. * @ids: Identifier set
  81. * @key: The key to find
  82. *
  83. * Requires ipc_ids.sem locked.
  84. * Returns the identifier if found or -1 if not.
  85. */
  86. int ipc_findkey(struct ipc_ids* ids, key_t key)
  87. {
  88. int id;
  89. struct kern_ipc_perm* p;
  90. int max_id = ids->max_id;
  91. /*
  92. * rcu_dereference() is not needed here
  93. * since ipc_ids.sem is held
  94. */
  95. for (id = 0; id <= max_id; id++) {
  96. p = ids->entries->p[id];
  97. if(p==NULL)
  98. continue;
  99. if (key == p->key)
  100. return id;
  101. }
  102. return -1;
  103. }
  104. /*
  105. * Requires ipc_ids.sem locked
  106. */
  107. static int grow_ary(struct ipc_ids* ids, int newsize)
  108. {
  109. struct ipc_id_ary* new;
  110. struct ipc_id_ary* old;
  111. int i;
  112. int size = ids->entries->size;
  113. if(newsize > IPCMNI)
  114. newsize = IPCMNI;
  115. if(newsize <= size)
  116. return newsize;
  117. new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
  118. sizeof(struct ipc_id_ary));
  119. if(new == NULL)
  120. return size;
  121. new->size = newsize;
  122. memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size +
  123. sizeof(struct ipc_id_ary));
  124. for(i=size;i<newsize;i++) {
  125. new->p[i] = NULL;
  126. }
  127. old = ids->entries;
  128. /*
  129. * Use rcu_assign_pointer() to make sure the memcpyed contents
  130. * of the new array are visible before the new array becomes visible.
  131. */
  132. rcu_assign_pointer(ids->entries, new);
  133. ipc_rcu_putref(old);
  134. return newsize;
  135. }
  136. /**
  137. * ipc_addid - add an IPC identifier
  138. * @ids: IPC identifier set
  139. * @new: new IPC permission set
  140. * @size: new size limit for the id array
  141. *
  142. * Add an entry 'new' to the IPC arrays. The permissions object is
  143. * initialised and the first free entry is set up and the id assigned
  144. * is returned. The list is returned in a locked state on success.
  145. * On failure the list is not locked and -1 is returned.
  146. *
  147. * Called with ipc_ids.sem held.
  148. */
  149. int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
  150. {
  151. int id;
  152. size = grow_ary(ids,size);
  153. /*
  154. * rcu_dereference()() is not needed here since
  155. * ipc_ids.sem is held
  156. */
  157. for (id = 0; id < size; id++) {
  158. if(ids->entries->p[id] == NULL)
  159. goto found;
  160. }
  161. return -1;
  162. found:
  163. ids->in_use++;
  164. if (id > ids->max_id)
  165. ids->max_id = id;
  166. new->cuid = new->uid = current->euid;
  167. new->gid = new->cgid = current->egid;
  168. new->seq = ids->seq++;
  169. if(ids->seq > ids->seq_max)
  170. ids->seq = 0;
  171. spin_lock_init(&new->lock);
  172. new->deleted = 0;
  173. rcu_read_lock();
  174. spin_lock(&new->lock);
  175. ids->entries->p[id] = new;
  176. return id;
  177. }
  178. /**
  179. * ipc_rmid - remove an IPC identifier
  180. * @ids: identifier set
  181. * @id: Identifier to remove
  182. *
  183. * The identifier must be valid, and in use. The kernel will panic if
  184. * fed an invalid identifier. The entry is removed and internal
  185. * variables recomputed. The object associated with the identifier
  186. * is returned.
  187. * ipc_ids.sem and the spinlock for this ID is hold before this function
  188. * is called, and remain locked on the exit.
  189. */
  190. struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
  191. {
  192. struct kern_ipc_perm* p;
  193. int lid = id % SEQ_MULTIPLIER;
  194. if(lid >= ids->entries->size)
  195. BUG();
  196. /*
  197. * do not need a rcu_dereference()() here to force ordering
  198. * on Alpha, since the ipc_ids.sem is held.
  199. */
  200. p = ids->entries->p[lid];
  201. ids->entries->p[lid] = NULL;
  202. if(p==NULL)
  203. BUG();
  204. ids->in_use--;
  205. if (lid == ids->max_id) {
  206. do {
  207. lid--;
  208. if(lid == -1)
  209. break;
  210. } while (ids->entries->p[lid] == NULL);
  211. ids->max_id = lid;
  212. }
  213. p->deleted = 1;
  214. return p;
  215. }
  216. /**
  217. * ipc_alloc - allocate ipc space
  218. * @size: size desired
  219. *
  220. * Allocate memory from the appropriate pools and return a pointer to it.
  221. * NULL is returned if the allocation fails
  222. */
  223. void* ipc_alloc(int size)
  224. {
  225. void* out;
  226. if(size > PAGE_SIZE)
  227. out = vmalloc(size);
  228. else
  229. out = kmalloc(size, GFP_KERNEL);
  230. return out;
  231. }
  232. /**
  233. * ipc_free - free ipc space
  234. * @ptr: pointer returned by ipc_alloc
  235. * @size: size of block
  236. *
  237. * Free a block created with ipc_alloc. The caller must know the size
  238. * used in the allocation call.
  239. */
  240. void ipc_free(void* ptr, int size)
  241. {
  242. if(size > PAGE_SIZE)
  243. vfree(ptr);
  244. else
  245. kfree(ptr);
  246. }
  247. /*
  248. * rcu allocations:
  249. * There are three headers that are prepended to the actual allocation:
  250. * - during use: ipc_rcu_hdr.
  251. * - during the rcu grace period: ipc_rcu_grace.
  252. * - [only if vmalloc]: ipc_rcu_sched.
  253. * Their lifetime doesn't overlap, thus the headers share the same memory.
  254. * Unlike a normal union, they are right-aligned, thus some container_of
  255. * forward/backward casting is necessary:
  256. */
  257. struct ipc_rcu_hdr
  258. {
  259. int refcount;
  260. int is_vmalloc;
  261. void *data[0];
  262. };
  263. struct ipc_rcu_grace
  264. {
  265. struct rcu_head rcu;
  266. /* "void *" makes sure alignment of following data is sane. */
  267. void *data[0];
  268. };
  269. struct ipc_rcu_sched
  270. {
  271. struct work_struct work;
  272. /* "void *" makes sure alignment of following data is sane. */
  273. void *data[0];
  274. };
  275. #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
  276. sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
  277. #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
  278. sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
  279. static inline int rcu_use_vmalloc(int size)
  280. {
  281. /* Too big for a single page? */
  282. if (HDRLEN_KMALLOC + size > PAGE_SIZE)
  283. return 1;
  284. return 0;
  285. }
  286. /**
  287. * ipc_rcu_alloc - allocate ipc and rcu space
  288. * @size: size desired
  289. *
  290. * Allocate memory for the rcu header structure + the object.
  291. * Returns the pointer to the object.
  292. * NULL is returned if the allocation fails.
  293. */
  294. void* ipc_rcu_alloc(int size)
  295. {
  296. void* out;
  297. /*
  298. * We prepend the allocation with the rcu struct, and
  299. * workqueue if necessary (for vmalloc).
  300. */
  301. if (rcu_use_vmalloc(size)) {
  302. out = vmalloc(HDRLEN_VMALLOC + size);
  303. if (out) {
  304. out += HDRLEN_VMALLOC;
  305. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
  306. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  307. }
  308. } else {
  309. out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
  310. if (out) {
  311. out += HDRLEN_KMALLOC;
  312. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
  313. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  314. }
  315. }
  316. return out;
  317. }
  318. void ipc_rcu_getref(void *ptr)
  319. {
  320. container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
  321. }
  322. /**
  323. * ipc_schedule_free - free ipc + rcu space
  324. *
  325. * Since RCU callback function is called in bh,
  326. * we need to defer the vfree to schedule_work
  327. */
  328. static void ipc_schedule_free(struct rcu_head *head)
  329. {
  330. struct ipc_rcu_grace *grace =
  331. container_of(head, struct ipc_rcu_grace, rcu);
  332. struct ipc_rcu_sched *sched =
  333. container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
  334. INIT_WORK(&sched->work, vfree, sched);
  335. schedule_work(&sched->work);
  336. }
  337. /**
  338. * ipc_immediate_free - free ipc + rcu space
  339. *
  340. * Free from the RCU callback context
  341. *
  342. */
  343. static void ipc_immediate_free(struct rcu_head *head)
  344. {
  345. struct ipc_rcu_grace *free =
  346. container_of(head, struct ipc_rcu_grace, rcu);
  347. kfree(free);
  348. }
  349. void ipc_rcu_putref(void *ptr)
  350. {
  351. if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
  352. return;
  353. if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
  354. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  355. ipc_schedule_free);
  356. } else {
  357. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  358. ipc_immediate_free);
  359. }
  360. }
  361. /**
  362. * ipcperms - check IPC permissions
  363. * @ipcp: IPC permission set
  364. * @flag: desired permission set.
  365. *
  366. * Check user, group, other permissions for access
  367. * to ipc resources. return 0 if allowed
  368. */
  369. int ipcperms (struct kern_ipc_perm *ipcp, short flag)
  370. { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
  371. int requested_mode, granted_mode;
  372. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  373. granted_mode = ipcp->mode;
  374. if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
  375. granted_mode >>= 6;
  376. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  377. granted_mode >>= 3;
  378. /* is there some bit set in requested_mode but not in granted_mode? */
  379. if ((requested_mode & ~granted_mode & 0007) &&
  380. !capable(CAP_IPC_OWNER))
  381. return -1;
  382. return security_ipc_permission(ipcp, flag);
  383. }
  384. /*
  385. * Functions to convert between the kern_ipc_perm structure and the
  386. * old/new ipc_perm structures
  387. */
  388. /**
  389. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  390. * @in: kernel permissions
  391. * @out: new style IPC permissions
  392. *
  393. * Turn the kernel object 'in' into a set of permissions descriptions
  394. * for returning to userspace (out).
  395. */
  396. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  397. {
  398. out->key = in->key;
  399. out->uid = in->uid;
  400. out->gid = in->gid;
  401. out->cuid = in->cuid;
  402. out->cgid = in->cgid;
  403. out->mode = in->mode;
  404. out->seq = in->seq;
  405. }
  406. /**
  407. * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
  408. * @in: new style IPC permissions
  409. * @out: old style IPC permissions
  410. *
  411. * Turn the new style permissions object in into a compatibility
  412. * object and store it into the 'out' pointer.
  413. */
  414. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  415. {
  416. out->key = in->key;
  417. SET_UID(out->uid, in->uid);
  418. SET_GID(out->gid, in->gid);
  419. SET_UID(out->cuid, in->cuid);
  420. SET_GID(out->cgid, in->cgid);
  421. out->mode = in->mode;
  422. out->seq = in->seq;
  423. }
  424. /*
  425. * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
  426. * is called with shm_ids.sem locked. Since grow_ary() is also called with
  427. * shm_ids.sem down(for Shared Memory), there is no need to add read
  428. * barriers here to gurantee the writes in grow_ary() are seen in order
  429. * here (for Alpha).
  430. *
  431. * However ipc_get() itself does not necessary require ipc_ids.sem down. So
  432. * if in the future ipc_get() is used by other places without ipc_ids.sem
  433. * down, then ipc_get() needs read memery barriers as ipc_lock() does.
  434. */
  435. struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
  436. {
  437. struct kern_ipc_perm* out;
  438. int lid = id % SEQ_MULTIPLIER;
  439. if(lid >= ids->entries->size)
  440. return NULL;
  441. out = ids->entries->p[lid];
  442. return out;
  443. }
  444. struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
  445. {
  446. struct kern_ipc_perm* out;
  447. int lid = id % SEQ_MULTIPLIER;
  448. struct ipc_id_ary* entries;
  449. rcu_read_lock();
  450. entries = rcu_dereference(ids->entries);
  451. if(lid >= entries->size) {
  452. rcu_read_unlock();
  453. return NULL;
  454. }
  455. out = entries->p[lid];
  456. if(out == NULL) {
  457. rcu_read_unlock();
  458. return NULL;
  459. }
  460. spin_lock(&out->lock);
  461. /* ipc_rmid() may have already freed the ID while ipc_lock
  462. * was spinning: here verify that the structure is still valid
  463. */
  464. if (out->deleted) {
  465. spin_unlock(&out->lock);
  466. rcu_read_unlock();
  467. return NULL;
  468. }
  469. return out;
  470. }
  471. void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
  472. {
  473. rcu_read_lock();
  474. spin_lock(&perm->lock);
  475. }
  476. void ipc_unlock(struct kern_ipc_perm* perm)
  477. {
  478. spin_unlock(&perm->lock);
  479. rcu_read_unlock();
  480. }
  481. int ipc_buildid(struct ipc_ids* ids, int id, int seq)
  482. {
  483. return SEQ_MULTIPLIER*seq + id;
  484. }
  485. int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
  486. {
  487. if(uid/SEQ_MULTIPLIER != ipcp->seq)
  488. return 1;
  489. return 0;
  490. }
  491. #ifdef __ARCH_WANT_IPC_PARSE_VERSION
  492. /**
  493. * ipc_parse_version - IPC call version
  494. * @cmd: pointer to command
  495. *
  496. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  497. * The cmd value is turned from an encoding command and version into
  498. * just the command code.
  499. */
  500. int ipc_parse_version (int *cmd)
  501. {
  502. if (*cmd & IPC_64) {
  503. *cmd ^= IPC_64;
  504. return IPC_64;
  505. } else {
  506. return IPC_OLD;
  507. }
  508. }
  509. #endif /* __ARCH_WANT_IPC_PARSE_VERSION */