pipe.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/pipe.c
  4. *
  5. * Copyright (C) 1991, 1992, 1999 Linus Torvalds
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/file.h>
  9. #include <linux/poll.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/fs.h>
  14. #include <linux/log2.h>
  15. #include <linux/mount.h>
  16. #include <linux/magic.h>
  17. #include <linux/pipe_fs_i.h>
  18. #include <linux/uio.h>
  19. #include <linux/highmem.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/audit.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/memcontrol.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/ioctls.h>
  27. #include "internal.h"
  28. /*
  29. * The max size that a non-root user is allowed to grow the pipe. Can
  30. * be set by root in /proc/sys/fs/pipe-max-size
  31. */
  32. unsigned int pipe_max_size = 1048576;
  33. /* Maximum allocatable pages per user. Hard limit is unset by default, soft
  34. * matches default values.
  35. */
  36. unsigned long pipe_user_pages_hard;
  37. unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
  38. /*
  39. * We use a start+len construction, which provides full use of the
  40. * allocated memory.
  41. * -- Florian Coosmann (FGC)
  42. *
  43. * Reads with count = 0 should always return 0.
  44. * -- Julian Bradfield 1999-06-07.
  45. *
  46. * FIFOs and Pipes now generate SIGIO for both readers and writers.
  47. * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
  48. *
  49. * pipe_read & write cleanup
  50. * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
  51. */
  52. static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
  53. {
  54. if (pipe->files)
  55. mutex_lock_nested(&pipe->mutex, subclass);
  56. }
  57. void pipe_lock(struct pipe_inode_info *pipe)
  58. {
  59. /*
  60. * pipe_lock() nests non-pipe inode locks (for writing to a file)
  61. */
  62. pipe_lock_nested(pipe, I_MUTEX_PARENT);
  63. }
  64. EXPORT_SYMBOL(pipe_lock);
  65. void pipe_unlock(struct pipe_inode_info *pipe)
  66. {
  67. if (pipe->files)
  68. mutex_unlock(&pipe->mutex);
  69. }
  70. EXPORT_SYMBOL(pipe_unlock);
  71. static inline void __pipe_lock(struct pipe_inode_info *pipe)
  72. {
  73. mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
  74. }
  75. static inline void __pipe_unlock(struct pipe_inode_info *pipe)
  76. {
  77. mutex_unlock(&pipe->mutex);
  78. }
  79. void pipe_double_lock(struct pipe_inode_info *pipe1,
  80. struct pipe_inode_info *pipe2)
  81. {
  82. BUG_ON(pipe1 == pipe2);
  83. if (pipe1 < pipe2) {
  84. pipe_lock_nested(pipe1, I_MUTEX_PARENT);
  85. pipe_lock_nested(pipe2, I_MUTEX_CHILD);
  86. } else {
  87. pipe_lock_nested(pipe2, I_MUTEX_PARENT);
  88. pipe_lock_nested(pipe1, I_MUTEX_CHILD);
  89. }
  90. }
  91. /* Drop the inode semaphore and wait for a pipe event, atomically */
  92. void pipe_wait(struct pipe_inode_info *pipe)
  93. {
  94. DEFINE_WAIT(wait);
  95. /*
  96. * Pipes are system-local resources, so sleeping on them
  97. * is considered a noninteractive wait:
  98. */
  99. prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
  100. pipe_unlock(pipe);
  101. schedule();
  102. finish_wait(&pipe->wait, &wait);
  103. pipe_lock(pipe);
  104. }
  105. static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
  106. struct pipe_buffer *buf)
  107. {
  108. struct page *page = buf->page;
  109. /*
  110. * If nobody else uses this page, and we don't already have a
  111. * temporary page, let's keep track of it as a one-deep
  112. * allocation cache. (Otherwise just release our reference to it)
  113. */
  114. if (page_count(page) == 1 && !pipe->tmp_page)
  115. pipe->tmp_page = page;
  116. else
  117. put_page(page);
  118. }
  119. static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
  120. struct pipe_buffer *buf)
  121. {
  122. struct page *page = buf->page;
  123. if (page_count(page) == 1) {
  124. if (memcg_kmem_enabled())
  125. memcg_kmem_uncharge(page, 0);
  126. __SetPageLocked(page);
  127. return 0;
  128. }
  129. return 1;
  130. }
  131. /**
  132. * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
  133. * @pipe: the pipe that the buffer belongs to
  134. * @buf: the buffer to attempt to steal
  135. *
  136. * Description:
  137. * This function attempts to steal the &struct page attached to
  138. * @buf. If successful, this function returns 0 and returns with
  139. * the page locked. The caller may then reuse the page for whatever
  140. * he wishes; the typical use is insertion into a different file
  141. * page cache.
  142. */
  143. int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
  144. struct pipe_buffer *buf)
  145. {
  146. struct page *page = buf->page;
  147. /*
  148. * A reference of one is golden, that means that the owner of this
  149. * page is the only one holding a reference to it. lock the page
  150. * and return OK.
  151. */
  152. if (page_count(page) == 1) {
  153. lock_page(page);
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. EXPORT_SYMBOL(generic_pipe_buf_steal);
  159. /**
  160. * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
  161. * @pipe: the pipe that the buffer belongs to
  162. * @buf: the buffer to get a reference to
  163. *
  164. * Description:
  165. * This function grabs an extra reference to @buf. It's used in
  166. * in the tee() system call, when we duplicate the buffers in one
  167. * pipe into another.
  168. */
  169. void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
  170. {
  171. get_page(buf->page);
  172. }
  173. EXPORT_SYMBOL(generic_pipe_buf_get);
  174. /**
  175. * generic_pipe_buf_confirm - verify contents of the pipe buffer
  176. * @info: the pipe that the buffer belongs to
  177. * @buf: the buffer to confirm
  178. *
  179. * Description:
  180. * This function does nothing, because the generic pipe code uses
  181. * pages that are always good when inserted into the pipe.
  182. */
  183. int generic_pipe_buf_confirm(struct pipe_inode_info *info,
  184. struct pipe_buffer *buf)
  185. {
  186. return 0;
  187. }
  188. EXPORT_SYMBOL(generic_pipe_buf_confirm);
  189. /**
  190. * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
  191. * @pipe: the pipe that the buffer belongs to
  192. * @buf: the buffer to put a reference to
  193. *
  194. * Description:
  195. * This function releases a reference to @buf.
  196. */
  197. void generic_pipe_buf_release(struct pipe_inode_info *pipe,
  198. struct pipe_buffer *buf)
  199. {
  200. put_page(buf->page);
  201. }
  202. EXPORT_SYMBOL(generic_pipe_buf_release);
  203. static const struct pipe_buf_operations anon_pipe_buf_ops = {
  204. .can_merge = 1,
  205. .confirm = generic_pipe_buf_confirm,
  206. .release = anon_pipe_buf_release,
  207. .steal = anon_pipe_buf_steal,
  208. .get = generic_pipe_buf_get,
  209. };
  210. static const struct pipe_buf_operations packet_pipe_buf_ops = {
  211. .can_merge = 0,
  212. .confirm = generic_pipe_buf_confirm,
  213. .release = anon_pipe_buf_release,
  214. .steal = anon_pipe_buf_steal,
  215. .get = generic_pipe_buf_get,
  216. };
  217. static ssize_t
  218. pipe_read(struct kiocb *iocb, struct iov_iter *to)
  219. {
  220. size_t total_len = iov_iter_count(to);
  221. struct file *filp = iocb->ki_filp;
  222. struct pipe_inode_info *pipe = filp->private_data;
  223. int do_wakeup;
  224. ssize_t ret;
  225. /* Null read succeeds. */
  226. if (unlikely(total_len == 0))
  227. return 0;
  228. do_wakeup = 0;
  229. ret = 0;
  230. __pipe_lock(pipe);
  231. for (;;) {
  232. int bufs = pipe->nrbufs;
  233. if (bufs) {
  234. int curbuf = pipe->curbuf;
  235. struct pipe_buffer *buf = pipe->bufs + curbuf;
  236. size_t chars = buf->len;
  237. size_t written;
  238. int error;
  239. if (chars > total_len)
  240. chars = total_len;
  241. error = pipe_buf_confirm(pipe, buf);
  242. if (error) {
  243. if (!ret)
  244. ret = error;
  245. break;
  246. }
  247. written = copy_page_to_iter(buf->page, buf->offset, chars, to);
  248. if (unlikely(written < chars)) {
  249. if (!ret)
  250. ret = -EFAULT;
  251. break;
  252. }
  253. ret += chars;
  254. buf->offset += chars;
  255. buf->len -= chars;
  256. /* Was it a packet buffer? Clean up and exit */
  257. if (buf->flags & PIPE_BUF_FLAG_PACKET) {
  258. total_len = chars;
  259. buf->len = 0;
  260. }
  261. if (!buf->len) {
  262. pipe_buf_release(pipe, buf);
  263. curbuf = (curbuf + 1) & (pipe->buffers - 1);
  264. pipe->curbuf = curbuf;
  265. pipe->nrbufs = --bufs;
  266. do_wakeup = 1;
  267. }
  268. total_len -= chars;
  269. if (!total_len)
  270. break; /* common path: read succeeded */
  271. }
  272. if (bufs) /* More to do? */
  273. continue;
  274. if (!pipe->writers)
  275. break;
  276. if (!pipe->waiting_writers) {
  277. /* syscall merging: Usually we must not sleep
  278. * if O_NONBLOCK is set, or if we got some data.
  279. * But if a writer sleeps in kernel space, then
  280. * we can wait for that data without violating POSIX.
  281. */
  282. if (ret)
  283. break;
  284. if (filp->f_flags & O_NONBLOCK) {
  285. ret = -EAGAIN;
  286. break;
  287. }
  288. }
  289. if (signal_pending(current)) {
  290. if (!ret)
  291. ret = -ERESTARTSYS;
  292. break;
  293. }
  294. if (do_wakeup) {
  295. wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
  296. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  297. }
  298. pipe_wait(pipe);
  299. }
  300. __pipe_unlock(pipe);
  301. /* Signal writers asynchronously that there is more room. */
  302. if (do_wakeup) {
  303. wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
  304. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  305. }
  306. if (ret > 0)
  307. file_accessed(filp);
  308. return ret;
  309. }
  310. static inline int is_packetized(struct file *file)
  311. {
  312. return (file->f_flags & O_DIRECT) != 0;
  313. }
  314. static ssize_t
  315. pipe_write(struct kiocb *iocb, struct iov_iter *from)
  316. {
  317. struct file *filp = iocb->ki_filp;
  318. struct pipe_inode_info *pipe = filp->private_data;
  319. ssize_t ret = 0;
  320. int do_wakeup = 0;
  321. size_t total_len = iov_iter_count(from);
  322. ssize_t chars;
  323. /* Null write succeeds. */
  324. if (unlikely(total_len == 0))
  325. return 0;
  326. __pipe_lock(pipe);
  327. if (!pipe->readers) {
  328. send_sig(SIGPIPE, current, 0);
  329. ret = -EPIPE;
  330. goto out;
  331. }
  332. /* We try to merge small writes */
  333. chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
  334. if (pipe->nrbufs && chars != 0) {
  335. int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
  336. (pipe->buffers - 1);
  337. struct pipe_buffer *buf = pipe->bufs + lastbuf;
  338. int offset = buf->offset + buf->len;
  339. if (buf->ops->can_merge && offset + chars <= PAGE_SIZE) {
  340. ret = pipe_buf_confirm(pipe, buf);
  341. if (ret)
  342. goto out;
  343. ret = copy_page_from_iter(buf->page, offset, chars, from);
  344. if (unlikely(ret < chars)) {
  345. ret = -EFAULT;
  346. goto out;
  347. }
  348. do_wakeup = 1;
  349. buf->len += ret;
  350. if (!iov_iter_count(from))
  351. goto out;
  352. }
  353. }
  354. for (;;) {
  355. int bufs;
  356. if (!pipe->readers) {
  357. send_sig(SIGPIPE, current, 0);
  358. if (!ret)
  359. ret = -EPIPE;
  360. break;
  361. }
  362. bufs = pipe->nrbufs;
  363. if (bufs < pipe->buffers) {
  364. int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
  365. struct pipe_buffer *buf = pipe->bufs + newbuf;
  366. struct page *page = pipe->tmp_page;
  367. int copied;
  368. if (!page) {
  369. page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
  370. if (unlikely(!page)) {
  371. ret = ret ? : -ENOMEM;
  372. break;
  373. }
  374. pipe->tmp_page = page;
  375. }
  376. /* Always wake up, even if the copy fails. Otherwise
  377. * we lock up (O_NONBLOCK-)readers that sleep due to
  378. * syscall merging.
  379. * FIXME! Is this really true?
  380. */
  381. do_wakeup = 1;
  382. copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
  383. if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
  384. if (!ret)
  385. ret = -EFAULT;
  386. break;
  387. }
  388. ret += copied;
  389. /* Insert it into the buffer array */
  390. buf->page = page;
  391. buf->ops = &anon_pipe_buf_ops;
  392. buf->offset = 0;
  393. buf->len = copied;
  394. buf->flags = 0;
  395. if (is_packetized(filp)) {
  396. buf->ops = &packet_pipe_buf_ops;
  397. buf->flags = PIPE_BUF_FLAG_PACKET;
  398. }
  399. pipe->nrbufs = ++bufs;
  400. pipe->tmp_page = NULL;
  401. if (!iov_iter_count(from))
  402. break;
  403. }
  404. if (bufs < pipe->buffers)
  405. continue;
  406. if (filp->f_flags & O_NONBLOCK) {
  407. if (!ret)
  408. ret = -EAGAIN;
  409. break;
  410. }
  411. if (signal_pending(current)) {
  412. if (!ret)
  413. ret = -ERESTARTSYS;
  414. break;
  415. }
  416. if (do_wakeup) {
  417. wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
  418. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  419. do_wakeup = 0;
  420. }
  421. pipe->waiting_writers++;
  422. pipe_wait(pipe);
  423. pipe->waiting_writers--;
  424. }
  425. out:
  426. __pipe_unlock(pipe);
  427. if (do_wakeup) {
  428. wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
  429. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  430. }
  431. if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
  432. int err = file_update_time(filp);
  433. if (err)
  434. ret = err;
  435. sb_end_write(file_inode(filp)->i_sb);
  436. }
  437. return ret;
  438. }
  439. static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  440. {
  441. struct pipe_inode_info *pipe = filp->private_data;
  442. int count, buf, nrbufs;
  443. switch (cmd) {
  444. case FIONREAD:
  445. __pipe_lock(pipe);
  446. count = 0;
  447. buf = pipe->curbuf;
  448. nrbufs = pipe->nrbufs;
  449. while (--nrbufs >= 0) {
  450. count += pipe->bufs[buf].len;
  451. buf = (buf+1) & (pipe->buffers - 1);
  452. }
  453. __pipe_unlock(pipe);
  454. return put_user(count, (int __user *)arg);
  455. default:
  456. return -ENOIOCTLCMD;
  457. }
  458. }
  459. static struct wait_queue_head *
  460. pipe_get_poll_head(struct file *filp, __poll_t events)
  461. {
  462. struct pipe_inode_info *pipe = filp->private_data;
  463. return &pipe->wait;
  464. }
  465. /* No kernel lock held - fine */
  466. static __poll_t pipe_poll_mask(struct file *filp, __poll_t events)
  467. {
  468. struct pipe_inode_info *pipe = filp->private_data;
  469. int nrbufs = pipe->nrbufs;
  470. __poll_t mask = 0;
  471. /* Reading only -- no need for acquiring the semaphore. */
  472. if (filp->f_mode & FMODE_READ) {
  473. mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0;
  474. if (!pipe->writers && filp->f_version != pipe->w_counter)
  475. mask |= EPOLLHUP;
  476. }
  477. if (filp->f_mode & FMODE_WRITE) {
  478. mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
  479. /*
  480. * Most Unices do not set EPOLLERR for FIFOs but on Linux they
  481. * behave exactly like pipes for poll().
  482. */
  483. if (!pipe->readers)
  484. mask |= EPOLLERR;
  485. }
  486. return mask;
  487. }
  488. static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
  489. {
  490. int kill = 0;
  491. spin_lock(&inode->i_lock);
  492. if (!--pipe->files) {
  493. inode->i_pipe = NULL;
  494. kill = 1;
  495. }
  496. spin_unlock(&inode->i_lock);
  497. if (kill)
  498. free_pipe_info(pipe);
  499. }
  500. static int
  501. pipe_release(struct inode *inode, struct file *file)
  502. {
  503. struct pipe_inode_info *pipe = file->private_data;
  504. __pipe_lock(pipe);
  505. if (file->f_mode & FMODE_READ)
  506. pipe->readers--;
  507. if (file->f_mode & FMODE_WRITE)
  508. pipe->writers--;
  509. if (pipe->readers || pipe->writers) {
  510. wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
  511. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  512. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  513. }
  514. __pipe_unlock(pipe);
  515. put_pipe_info(inode, pipe);
  516. return 0;
  517. }
  518. static int
  519. pipe_fasync(int fd, struct file *filp, int on)
  520. {
  521. struct pipe_inode_info *pipe = filp->private_data;
  522. int retval = 0;
  523. __pipe_lock(pipe);
  524. if (filp->f_mode & FMODE_READ)
  525. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  526. if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
  527. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  528. if (retval < 0 && (filp->f_mode & FMODE_READ))
  529. /* this can happen only if on == T */
  530. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  531. }
  532. __pipe_unlock(pipe);
  533. return retval;
  534. }
  535. static unsigned long account_pipe_buffers(struct user_struct *user,
  536. unsigned long old, unsigned long new)
  537. {
  538. return atomic_long_add_return(new - old, &user->pipe_bufs);
  539. }
  540. static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
  541. {
  542. unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
  543. return soft_limit && user_bufs > soft_limit;
  544. }
  545. static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
  546. {
  547. unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
  548. return hard_limit && user_bufs > hard_limit;
  549. }
  550. static bool is_unprivileged_user(void)
  551. {
  552. return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
  553. }
  554. struct pipe_inode_info *alloc_pipe_info(void)
  555. {
  556. struct pipe_inode_info *pipe;
  557. unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
  558. struct user_struct *user = get_current_user();
  559. unsigned long user_bufs;
  560. unsigned int max_size = READ_ONCE(pipe_max_size);
  561. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
  562. if (pipe == NULL)
  563. goto out_free_uid;
  564. if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
  565. pipe_bufs = max_size >> PAGE_SHIFT;
  566. user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
  567. if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
  568. user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
  569. pipe_bufs = 1;
  570. }
  571. if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
  572. goto out_revert_acct;
  573. pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
  574. GFP_KERNEL_ACCOUNT);
  575. if (pipe->bufs) {
  576. init_waitqueue_head(&pipe->wait);
  577. pipe->r_counter = pipe->w_counter = 1;
  578. pipe->buffers = pipe_bufs;
  579. pipe->user = user;
  580. mutex_init(&pipe->mutex);
  581. return pipe;
  582. }
  583. out_revert_acct:
  584. (void) account_pipe_buffers(user, pipe_bufs, 0);
  585. kfree(pipe);
  586. out_free_uid:
  587. free_uid(user);
  588. return NULL;
  589. }
  590. void free_pipe_info(struct pipe_inode_info *pipe)
  591. {
  592. int i;
  593. (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
  594. free_uid(pipe->user);
  595. for (i = 0; i < pipe->buffers; i++) {
  596. struct pipe_buffer *buf = pipe->bufs + i;
  597. if (buf->ops)
  598. pipe_buf_release(pipe, buf);
  599. }
  600. if (pipe->tmp_page)
  601. __free_page(pipe->tmp_page);
  602. kfree(pipe->bufs);
  603. kfree(pipe);
  604. }
  605. static struct vfsmount *pipe_mnt __read_mostly;
  606. /*
  607. * pipefs_dname() is called from d_path().
  608. */
  609. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  610. {
  611. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  612. d_inode(dentry)->i_ino);
  613. }
  614. static const struct dentry_operations pipefs_dentry_operations = {
  615. .d_dname = pipefs_dname,
  616. };
  617. static struct inode * get_pipe_inode(void)
  618. {
  619. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  620. struct pipe_inode_info *pipe;
  621. if (!inode)
  622. goto fail_inode;
  623. inode->i_ino = get_next_ino();
  624. pipe = alloc_pipe_info();
  625. if (!pipe)
  626. goto fail_iput;
  627. inode->i_pipe = pipe;
  628. pipe->files = 2;
  629. pipe->readers = pipe->writers = 1;
  630. inode->i_fop = &pipefifo_fops;
  631. /*
  632. * Mark the inode dirty from the very beginning,
  633. * that way it will never be moved to the dirty
  634. * list because "mark_inode_dirty()" will think
  635. * that it already _is_ on the dirty list.
  636. */
  637. inode->i_state = I_DIRTY;
  638. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  639. inode->i_uid = current_fsuid();
  640. inode->i_gid = current_fsgid();
  641. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  642. return inode;
  643. fail_iput:
  644. iput(inode);
  645. fail_inode:
  646. return NULL;
  647. }
  648. int create_pipe_files(struct file **res, int flags)
  649. {
  650. int err;
  651. struct inode *inode = get_pipe_inode();
  652. struct file *f;
  653. struct path path;
  654. if (!inode)
  655. return -ENFILE;
  656. err = -ENOMEM;
  657. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &empty_name);
  658. if (!path.dentry)
  659. goto err_inode;
  660. path.mnt = mntget(pipe_mnt);
  661. d_instantiate(path.dentry, inode);
  662. f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
  663. if (IS_ERR(f)) {
  664. err = PTR_ERR(f);
  665. goto err_dentry;
  666. }
  667. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  668. f->private_data = inode->i_pipe;
  669. res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
  670. if (IS_ERR(res[0])) {
  671. err = PTR_ERR(res[0]);
  672. goto err_file;
  673. }
  674. path_get(&path);
  675. res[0]->private_data = inode->i_pipe;
  676. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  677. res[1] = f;
  678. return 0;
  679. err_file:
  680. put_filp(f);
  681. err_dentry:
  682. free_pipe_info(inode->i_pipe);
  683. path_put(&path);
  684. return err;
  685. err_inode:
  686. free_pipe_info(inode->i_pipe);
  687. iput(inode);
  688. return err;
  689. }
  690. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  691. {
  692. int error;
  693. int fdw, fdr;
  694. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  695. return -EINVAL;
  696. error = create_pipe_files(files, flags);
  697. if (error)
  698. return error;
  699. error = get_unused_fd_flags(flags);
  700. if (error < 0)
  701. goto err_read_pipe;
  702. fdr = error;
  703. error = get_unused_fd_flags(flags);
  704. if (error < 0)
  705. goto err_fdr;
  706. fdw = error;
  707. audit_fd_pair(fdr, fdw);
  708. fd[0] = fdr;
  709. fd[1] = fdw;
  710. return 0;
  711. err_fdr:
  712. put_unused_fd(fdr);
  713. err_read_pipe:
  714. fput(files[0]);
  715. fput(files[1]);
  716. return error;
  717. }
  718. int do_pipe_flags(int *fd, int flags)
  719. {
  720. struct file *files[2];
  721. int error = __do_pipe_flags(fd, files, flags);
  722. if (!error) {
  723. fd_install(fd[0], files[0]);
  724. fd_install(fd[1], files[1]);
  725. }
  726. return error;
  727. }
  728. /*
  729. * sys_pipe() is the normal C calling standard for creating
  730. * a pipe. It's not the way Unix traditionally does this, though.
  731. */
  732. static int do_pipe2(int __user *fildes, int flags)
  733. {
  734. struct file *files[2];
  735. int fd[2];
  736. int error;
  737. error = __do_pipe_flags(fd, files, flags);
  738. if (!error) {
  739. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  740. fput(files[0]);
  741. fput(files[1]);
  742. put_unused_fd(fd[0]);
  743. put_unused_fd(fd[1]);
  744. error = -EFAULT;
  745. } else {
  746. fd_install(fd[0], files[0]);
  747. fd_install(fd[1], files[1]);
  748. }
  749. }
  750. return error;
  751. }
  752. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  753. {
  754. return do_pipe2(fildes, flags);
  755. }
  756. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  757. {
  758. return do_pipe2(fildes, 0);
  759. }
  760. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  761. {
  762. int cur = *cnt;
  763. while (cur == *cnt) {
  764. pipe_wait(pipe);
  765. if (signal_pending(current))
  766. break;
  767. }
  768. return cur == *cnt ? -ERESTARTSYS : 0;
  769. }
  770. static void wake_up_partner(struct pipe_inode_info *pipe)
  771. {
  772. wake_up_interruptible(&pipe->wait);
  773. }
  774. static int fifo_open(struct inode *inode, struct file *filp)
  775. {
  776. struct pipe_inode_info *pipe;
  777. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  778. int ret;
  779. filp->f_version = 0;
  780. spin_lock(&inode->i_lock);
  781. if (inode->i_pipe) {
  782. pipe = inode->i_pipe;
  783. pipe->files++;
  784. spin_unlock(&inode->i_lock);
  785. } else {
  786. spin_unlock(&inode->i_lock);
  787. pipe = alloc_pipe_info();
  788. if (!pipe)
  789. return -ENOMEM;
  790. pipe->files = 1;
  791. spin_lock(&inode->i_lock);
  792. if (unlikely(inode->i_pipe)) {
  793. inode->i_pipe->files++;
  794. spin_unlock(&inode->i_lock);
  795. free_pipe_info(pipe);
  796. pipe = inode->i_pipe;
  797. } else {
  798. inode->i_pipe = pipe;
  799. spin_unlock(&inode->i_lock);
  800. }
  801. }
  802. filp->private_data = pipe;
  803. /* OK, we have a pipe and it's pinned down */
  804. __pipe_lock(pipe);
  805. /* We can only do regular read/write on fifos */
  806. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  807. switch (filp->f_mode) {
  808. case FMODE_READ:
  809. /*
  810. * O_RDONLY
  811. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  812. * opened, even when there is no process writing the FIFO.
  813. */
  814. pipe->r_counter++;
  815. if (pipe->readers++ == 0)
  816. wake_up_partner(pipe);
  817. if (!is_pipe && !pipe->writers) {
  818. if ((filp->f_flags & O_NONBLOCK)) {
  819. /* suppress EPOLLHUP until we have
  820. * seen a writer */
  821. filp->f_version = pipe->w_counter;
  822. } else {
  823. if (wait_for_partner(pipe, &pipe->w_counter))
  824. goto err_rd;
  825. }
  826. }
  827. break;
  828. case FMODE_WRITE:
  829. /*
  830. * O_WRONLY
  831. * POSIX.1 says that O_NONBLOCK means return -1 with
  832. * errno=ENXIO when there is no process reading the FIFO.
  833. */
  834. ret = -ENXIO;
  835. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  836. goto err;
  837. pipe->w_counter++;
  838. if (!pipe->writers++)
  839. wake_up_partner(pipe);
  840. if (!is_pipe && !pipe->readers) {
  841. if (wait_for_partner(pipe, &pipe->r_counter))
  842. goto err_wr;
  843. }
  844. break;
  845. case FMODE_READ | FMODE_WRITE:
  846. /*
  847. * O_RDWR
  848. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  849. * This implementation will NEVER block on a O_RDWR open, since
  850. * the process can at least talk to itself.
  851. */
  852. pipe->readers++;
  853. pipe->writers++;
  854. pipe->r_counter++;
  855. pipe->w_counter++;
  856. if (pipe->readers == 1 || pipe->writers == 1)
  857. wake_up_partner(pipe);
  858. break;
  859. default:
  860. ret = -EINVAL;
  861. goto err;
  862. }
  863. /* Ok! */
  864. __pipe_unlock(pipe);
  865. return 0;
  866. err_rd:
  867. if (!--pipe->readers)
  868. wake_up_interruptible(&pipe->wait);
  869. ret = -ERESTARTSYS;
  870. goto err;
  871. err_wr:
  872. if (!--pipe->writers)
  873. wake_up_interruptible(&pipe->wait);
  874. ret = -ERESTARTSYS;
  875. goto err;
  876. err:
  877. __pipe_unlock(pipe);
  878. put_pipe_info(inode, pipe);
  879. return ret;
  880. }
  881. const struct file_operations pipefifo_fops = {
  882. .open = fifo_open,
  883. .llseek = no_llseek,
  884. .read_iter = pipe_read,
  885. .write_iter = pipe_write,
  886. .get_poll_head = pipe_get_poll_head,
  887. .poll_mask = pipe_poll_mask,
  888. .unlocked_ioctl = pipe_ioctl,
  889. .release = pipe_release,
  890. .fasync = pipe_fasync,
  891. };
  892. /*
  893. * Currently we rely on the pipe array holding a power-of-2 number
  894. * of pages. Returns 0 on error.
  895. */
  896. unsigned int round_pipe_size(unsigned long size)
  897. {
  898. if (size > (1U << 31))
  899. return 0;
  900. /* Minimum pipe size, as required by POSIX */
  901. if (size < PAGE_SIZE)
  902. return PAGE_SIZE;
  903. return roundup_pow_of_two(size);
  904. }
  905. /*
  906. * Allocate a new array of pipe buffers and copy the info over. Returns the
  907. * pipe size if successful, or return -ERROR on error.
  908. */
  909. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
  910. {
  911. struct pipe_buffer *bufs;
  912. unsigned int size, nr_pages;
  913. unsigned long user_bufs;
  914. long ret = 0;
  915. size = round_pipe_size(arg);
  916. nr_pages = size >> PAGE_SHIFT;
  917. if (!nr_pages)
  918. return -EINVAL;
  919. /*
  920. * If trying to increase the pipe capacity, check that an
  921. * unprivileged user is not trying to exceed various limits
  922. * (soft limit check here, hard limit check just below).
  923. * Decreasing the pipe capacity is always permitted, even
  924. * if the user is currently over a limit.
  925. */
  926. if (nr_pages > pipe->buffers &&
  927. size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
  928. return -EPERM;
  929. user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
  930. if (nr_pages > pipe->buffers &&
  931. (too_many_pipe_buffers_hard(user_bufs) ||
  932. too_many_pipe_buffers_soft(user_bufs)) &&
  933. is_unprivileged_user()) {
  934. ret = -EPERM;
  935. goto out_revert_acct;
  936. }
  937. /*
  938. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  939. * expect a lot of shrink+grow operations, just free and allocate
  940. * again like we would do for growing. If the pipe currently
  941. * contains more buffers than arg, then return busy.
  942. */
  943. if (nr_pages < pipe->nrbufs) {
  944. ret = -EBUSY;
  945. goto out_revert_acct;
  946. }
  947. bufs = kcalloc(nr_pages, sizeof(*bufs),
  948. GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
  949. if (unlikely(!bufs)) {
  950. ret = -ENOMEM;
  951. goto out_revert_acct;
  952. }
  953. /*
  954. * The pipe array wraps around, so just start the new one at zero
  955. * and adjust the indexes.
  956. */
  957. if (pipe->nrbufs) {
  958. unsigned int tail;
  959. unsigned int head;
  960. tail = pipe->curbuf + pipe->nrbufs;
  961. if (tail < pipe->buffers)
  962. tail = 0;
  963. else
  964. tail &= (pipe->buffers - 1);
  965. head = pipe->nrbufs - tail;
  966. if (head)
  967. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  968. if (tail)
  969. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  970. }
  971. pipe->curbuf = 0;
  972. kfree(pipe->bufs);
  973. pipe->bufs = bufs;
  974. pipe->buffers = nr_pages;
  975. return nr_pages * PAGE_SIZE;
  976. out_revert_acct:
  977. (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
  978. return ret;
  979. }
  980. /*
  981. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  982. * location, so checking ->i_pipe is not enough to verify that this is a
  983. * pipe.
  984. */
  985. struct pipe_inode_info *get_pipe_info(struct file *file)
  986. {
  987. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  988. }
  989. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  990. {
  991. struct pipe_inode_info *pipe;
  992. long ret;
  993. pipe = get_pipe_info(file);
  994. if (!pipe)
  995. return -EBADF;
  996. __pipe_lock(pipe);
  997. switch (cmd) {
  998. case F_SETPIPE_SZ:
  999. ret = pipe_set_size(pipe, arg);
  1000. break;
  1001. case F_GETPIPE_SZ:
  1002. ret = pipe->buffers * PAGE_SIZE;
  1003. break;
  1004. default:
  1005. ret = -EINVAL;
  1006. break;
  1007. }
  1008. __pipe_unlock(pipe);
  1009. return ret;
  1010. }
  1011. static const struct super_operations pipefs_ops = {
  1012. .destroy_inode = free_inode_nonrcu,
  1013. .statfs = simple_statfs,
  1014. };
  1015. /*
  1016. * pipefs should _never_ be mounted by userland - too much of security hassle,
  1017. * no real gain from having the whole whorehouse mounted. So we don't need
  1018. * any operations on the root directory. However, we need a non-trivial
  1019. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1020. */
  1021. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1022. int flags, const char *dev_name, void *data)
  1023. {
  1024. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1025. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1026. }
  1027. static struct file_system_type pipe_fs_type = {
  1028. .name = "pipefs",
  1029. .mount = pipefs_mount,
  1030. .kill_sb = kill_anon_super,
  1031. };
  1032. static int __init init_pipe_fs(void)
  1033. {
  1034. int err = register_filesystem(&pipe_fs_type);
  1035. if (!err) {
  1036. pipe_mnt = kern_mount(&pipe_fs_type);
  1037. if (IS_ERR(pipe_mnt)) {
  1038. err = PTR_ERR(pipe_mnt);
  1039. unregister_filesystem(&pipe_fs_type);
  1040. }
  1041. }
  1042. return err;
  1043. }
  1044. fs_initcall(init_pipe_fs);