pipe.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. /* No kernel lock held - fine */
  460. static __poll_t
  461. pipe_poll(struct file *filp, poll_table *wait)
  462. {
  463. __poll_t mask;
  464. struct pipe_inode_info *pipe = filp->private_data;
  465. int nrbufs;
  466. poll_wait(filp, &pipe->wait, wait);
  467. /* Reading only -- no need for acquiring the semaphore. */
  468. nrbufs = pipe->nrbufs;
  469. mask = 0;
  470. if (filp->f_mode & FMODE_READ) {
  471. mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0;
  472. if (!pipe->writers && filp->f_version != pipe->w_counter)
  473. mask |= EPOLLHUP;
  474. }
  475. if (filp->f_mode & FMODE_WRITE) {
  476. mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
  477. /*
  478. * Most Unices do not set EPOLLERR for FIFOs but on Linux they
  479. * behave exactly like pipes for poll().
  480. */
  481. if (!pipe->readers)
  482. mask |= EPOLLERR;
  483. }
  484. return mask;
  485. }
  486. static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
  487. {
  488. int kill = 0;
  489. spin_lock(&inode->i_lock);
  490. if (!--pipe->files) {
  491. inode->i_pipe = NULL;
  492. kill = 1;
  493. }
  494. spin_unlock(&inode->i_lock);
  495. if (kill)
  496. free_pipe_info(pipe);
  497. }
  498. static int
  499. pipe_release(struct inode *inode, struct file *file)
  500. {
  501. struct pipe_inode_info *pipe = file->private_data;
  502. __pipe_lock(pipe);
  503. if (file->f_mode & FMODE_READ)
  504. pipe->readers--;
  505. if (file->f_mode & FMODE_WRITE)
  506. pipe->writers--;
  507. if (pipe->readers || pipe->writers) {
  508. wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
  509. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  510. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  511. }
  512. __pipe_unlock(pipe);
  513. put_pipe_info(inode, pipe);
  514. return 0;
  515. }
  516. static int
  517. pipe_fasync(int fd, struct file *filp, int on)
  518. {
  519. struct pipe_inode_info *pipe = filp->private_data;
  520. int retval = 0;
  521. __pipe_lock(pipe);
  522. if (filp->f_mode & FMODE_READ)
  523. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  524. if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
  525. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  526. if (retval < 0 && (filp->f_mode & FMODE_READ))
  527. /* this can happen only if on == T */
  528. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  529. }
  530. __pipe_unlock(pipe);
  531. return retval;
  532. }
  533. static unsigned long account_pipe_buffers(struct user_struct *user,
  534. unsigned long old, unsigned long new)
  535. {
  536. return atomic_long_add_return(new - old, &user->pipe_bufs);
  537. }
  538. static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
  539. {
  540. unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
  541. return soft_limit && user_bufs > soft_limit;
  542. }
  543. static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
  544. {
  545. unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
  546. return hard_limit && user_bufs > hard_limit;
  547. }
  548. static bool is_unprivileged_user(void)
  549. {
  550. return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
  551. }
  552. struct pipe_inode_info *alloc_pipe_info(void)
  553. {
  554. struct pipe_inode_info *pipe;
  555. unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
  556. struct user_struct *user = get_current_user();
  557. unsigned long user_bufs;
  558. unsigned int max_size = READ_ONCE(pipe_max_size);
  559. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
  560. if (pipe == NULL)
  561. goto out_free_uid;
  562. if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
  563. pipe_bufs = max_size >> PAGE_SHIFT;
  564. user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
  565. if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
  566. user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
  567. pipe_bufs = 1;
  568. }
  569. if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
  570. goto out_revert_acct;
  571. pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
  572. GFP_KERNEL_ACCOUNT);
  573. if (pipe->bufs) {
  574. init_waitqueue_head(&pipe->wait);
  575. pipe->r_counter = pipe->w_counter = 1;
  576. pipe->buffers = pipe_bufs;
  577. pipe->user = user;
  578. mutex_init(&pipe->mutex);
  579. return pipe;
  580. }
  581. out_revert_acct:
  582. (void) account_pipe_buffers(user, pipe_bufs, 0);
  583. kfree(pipe);
  584. out_free_uid:
  585. free_uid(user);
  586. return NULL;
  587. }
  588. void free_pipe_info(struct pipe_inode_info *pipe)
  589. {
  590. int i;
  591. (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
  592. free_uid(pipe->user);
  593. for (i = 0; i < pipe->buffers; i++) {
  594. struct pipe_buffer *buf = pipe->bufs + i;
  595. if (buf->ops)
  596. pipe_buf_release(pipe, buf);
  597. }
  598. if (pipe->tmp_page)
  599. __free_page(pipe->tmp_page);
  600. kfree(pipe->bufs);
  601. kfree(pipe);
  602. }
  603. static struct vfsmount *pipe_mnt __read_mostly;
  604. /*
  605. * pipefs_dname() is called from d_path().
  606. */
  607. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  608. {
  609. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  610. d_inode(dentry)->i_ino);
  611. }
  612. static const struct dentry_operations pipefs_dentry_operations = {
  613. .d_dname = pipefs_dname,
  614. };
  615. static struct inode * get_pipe_inode(void)
  616. {
  617. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  618. struct pipe_inode_info *pipe;
  619. if (!inode)
  620. goto fail_inode;
  621. inode->i_ino = get_next_ino();
  622. pipe = alloc_pipe_info();
  623. if (!pipe)
  624. goto fail_iput;
  625. inode->i_pipe = pipe;
  626. pipe->files = 2;
  627. pipe->readers = pipe->writers = 1;
  628. inode->i_fop = &pipefifo_fops;
  629. /*
  630. * Mark the inode dirty from the very beginning,
  631. * that way it will never be moved to the dirty
  632. * list because "mark_inode_dirty()" will think
  633. * that it already _is_ on the dirty list.
  634. */
  635. inode->i_state = I_DIRTY;
  636. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  637. inode->i_uid = current_fsuid();
  638. inode->i_gid = current_fsgid();
  639. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  640. return inode;
  641. fail_iput:
  642. iput(inode);
  643. fail_inode:
  644. return NULL;
  645. }
  646. int create_pipe_files(struct file **res, int flags)
  647. {
  648. struct inode *inode = get_pipe_inode();
  649. struct file *f;
  650. if (!inode)
  651. return -ENFILE;
  652. f = alloc_file_pseudo(inode, pipe_mnt, "",
  653. O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
  654. &pipefifo_fops);
  655. if (IS_ERR(f)) {
  656. free_pipe_info(inode->i_pipe);
  657. iput(inode);
  658. return PTR_ERR(f);
  659. }
  660. f->private_data = inode->i_pipe;
  661. res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
  662. &pipefifo_fops);
  663. if (IS_ERR(res[0])) {
  664. put_pipe_info(inode, inode->i_pipe);
  665. fput(f);
  666. return PTR_ERR(res[0]);
  667. }
  668. res[0]->private_data = inode->i_pipe;
  669. res[1] = f;
  670. return 0;
  671. }
  672. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  673. {
  674. int error;
  675. int fdw, fdr;
  676. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  677. return -EINVAL;
  678. error = create_pipe_files(files, flags);
  679. if (error)
  680. return error;
  681. error = get_unused_fd_flags(flags);
  682. if (error < 0)
  683. goto err_read_pipe;
  684. fdr = error;
  685. error = get_unused_fd_flags(flags);
  686. if (error < 0)
  687. goto err_fdr;
  688. fdw = error;
  689. audit_fd_pair(fdr, fdw);
  690. fd[0] = fdr;
  691. fd[1] = fdw;
  692. return 0;
  693. err_fdr:
  694. put_unused_fd(fdr);
  695. err_read_pipe:
  696. fput(files[0]);
  697. fput(files[1]);
  698. return error;
  699. }
  700. int do_pipe_flags(int *fd, int flags)
  701. {
  702. struct file *files[2];
  703. int error = __do_pipe_flags(fd, files, flags);
  704. if (!error) {
  705. fd_install(fd[0], files[0]);
  706. fd_install(fd[1], files[1]);
  707. }
  708. return error;
  709. }
  710. /*
  711. * sys_pipe() is the normal C calling standard for creating
  712. * a pipe. It's not the way Unix traditionally does this, though.
  713. */
  714. static int do_pipe2(int __user *fildes, int flags)
  715. {
  716. struct file *files[2];
  717. int fd[2];
  718. int error;
  719. error = __do_pipe_flags(fd, files, flags);
  720. if (!error) {
  721. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  722. fput(files[0]);
  723. fput(files[1]);
  724. put_unused_fd(fd[0]);
  725. put_unused_fd(fd[1]);
  726. error = -EFAULT;
  727. } else {
  728. fd_install(fd[0], files[0]);
  729. fd_install(fd[1], files[1]);
  730. }
  731. }
  732. return error;
  733. }
  734. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  735. {
  736. return do_pipe2(fildes, flags);
  737. }
  738. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  739. {
  740. return do_pipe2(fildes, 0);
  741. }
  742. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  743. {
  744. int cur = *cnt;
  745. while (cur == *cnt) {
  746. pipe_wait(pipe);
  747. if (signal_pending(current))
  748. break;
  749. }
  750. return cur == *cnt ? -ERESTARTSYS : 0;
  751. }
  752. static void wake_up_partner(struct pipe_inode_info *pipe)
  753. {
  754. wake_up_interruptible(&pipe->wait);
  755. }
  756. static int fifo_open(struct inode *inode, struct file *filp)
  757. {
  758. struct pipe_inode_info *pipe;
  759. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  760. int ret;
  761. filp->f_version = 0;
  762. spin_lock(&inode->i_lock);
  763. if (inode->i_pipe) {
  764. pipe = inode->i_pipe;
  765. pipe->files++;
  766. spin_unlock(&inode->i_lock);
  767. } else {
  768. spin_unlock(&inode->i_lock);
  769. pipe = alloc_pipe_info();
  770. if (!pipe)
  771. return -ENOMEM;
  772. pipe->files = 1;
  773. spin_lock(&inode->i_lock);
  774. if (unlikely(inode->i_pipe)) {
  775. inode->i_pipe->files++;
  776. spin_unlock(&inode->i_lock);
  777. free_pipe_info(pipe);
  778. pipe = inode->i_pipe;
  779. } else {
  780. inode->i_pipe = pipe;
  781. spin_unlock(&inode->i_lock);
  782. }
  783. }
  784. filp->private_data = pipe;
  785. /* OK, we have a pipe and it's pinned down */
  786. __pipe_lock(pipe);
  787. /* We can only do regular read/write on fifos */
  788. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  789. switch (filp->f_mode) {
  790. case FMODE_READ:
  791. /*
  792. * O_RDONLY
  793. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  794. * opened, even when there is no process writing the FIFO.
  795. */
  796. pipe->r_counter++;
  797. if (pipe->readers++ == 0)
  798. wake_up_partner(pipe);
  799. if (!is_pipe && !pipe->writers) {
  800. if ((filp->f_flags & O_NONBLOCK)) {
  801. /* suppress EPOLLHUP until we have
  802. * seen a writer */
  803. filp->f_version = pipe->w_counter;
  804. } else {
  805. if (wait_for_partner(pipe, &pipe->w_counter))
  806. goto err_rd;
  807. }
  808. }
  809. break;
  810. case FMODE_WRITE:
  811. /*
  812. * O_WRONLY
  813. * POSIX.1 says that O_NONBLOCK means return -1 with
  814. * errno=ENXIO when there is no process reading the FIFO.
  815. */
  816. ret = -ENXIO;
  817. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  818. goto err;
  819. pipe->w_counter++;
  820. if (!pipe->writers++)
  821. wake_up_partner(pipe);
  822. if (!is_pipe && !pipe->readers) {
  823. if (wait_for_partner(pipe, &pipe->r_counter))
  824. goto err_wr;
  825. }
  826. break;
  827. case FMODE_READ | FMODE_WRITE:
  828. /*
  829. * O_RDWR
  830. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  831. * This implementation will NEVER block on a O_RDWR open, since
  832. * the process can at least talk to itself.
  833. */
  834. pipe->readers++;
  835. pipe->writers++;
  836. pipe->r_counter++;
  837. pipe->w_counter++;
  838. if (pipe->readers == 1 || pipe->writers == 1)
  839. wake_up_partner(pipe);
  840. break;
  841. default:
  842. ret = -EINVAL;
  843. goto err;
  844. }
  845. /* Ok! */
  846. __pipe_unlock(pipe);
  847. return 0;
  848. err_rd:
  849. if (!--pipe->readers)
  850. wake_up_interruptible(&pipe->wait);
  851. ret = -ERESTARTSYS;
  852. goto err;
  853. err_wr:
  854. if (!--pipe->writers)
  855. wake_up_interruptible(&pipe->wait);
  856. ret = -ERESTARTSYS;
  857. goto err;
  858. err:
  859. __pipe_unlock(pipe);
  860. put_pipe_info(inode, pipe);
  861. return ret;
  862. }
  863. const struct file_operations pipefifo_fops = {
  864. .open = fifo_open,
  865. .llseek = no_llseek,
  866. .read_iter = pipe_read,
  867. .write_iter = pipe_write,
  868. .poll = pipe_poll,
  869. .unlocked_ioctl = pipe_ioctl,
  870. .release = pipe_release,
  871. .fasync = pipe_fasync,
  872. };
  873. /*
  874. * Currently we rely on the pipe array holding a power-of-2 number
  875. * of pages. Returns 0 on error.
  876. */
  877. unsigned int round_pipe_size(unsigned long size)
  878. {
  879. if (size > (1U << 31))
  880. return 0;
  881. /* Minimum pipe size, as required by POSIX */
  882. if (size < PAGE_SIZE)
  883. return PAGE_SIZE;
  884. return roundup_pow_of_two(size);
  885. }
  886. /*
  887. * Allocate a new array of pipe buffers and copy the info over. Returns the
  888. * pipe size if successful, or return -ERROR on error.
  889. */
  890. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
  891. {
  892. struct pipe_buffer *bufs;
  893. unsigned int size, nr_pages;
  894. unsigned long user_bufs;
  895. long ret = 0;
  896. size = round_pipe_size(arg);
  897. nr_pages = size >> PAGE_SHIFT;
  898. if (!nr_pages)
  899. return -EINVAL;
  900. /*
  901. * If trying to increase the pipe capacity, check that an
  902. * unprivileged user is not trying to exceed various limits
  903. * (soft limit check here, hard limit check just below).
  904. * Decreasing the pipe capacity is always permitted, even
  905. * if the user is currently over a limit.
  906. */
  907. if (nr_pages > pipe->buffers &&
  908. size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
  909. return -EPERM;
  910. user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
  911. if (nr_pages > pipe->buffers &&
  912. (too_many_pipe_buffers_hard(user_bufs) ||
  913. too_many_pipe_buffers_soft(user_bufs)) &&
  914. is_unprivileged_user()) {
  915. ret = -EPERM;
  916. goto out_revert_acct;
  917. }
  918. /*
  919. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  920. * expect a lot of shrink+grow operations, just free and allocate
  921. * again like we would do for growing. If the pipe currently
  922. * contains more buffers than arg, then return busy.
  923. */
  924. if (nr_pages < pipe->nrbufs) {
  925. ret = -EBUSY;
  926. goto out_revert_acct;
  927. }
  928. bufs = kcalloc(nr_pages, sizeof(*bufs),
  929. GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
  930. if (unlikely(!bufs)) {
  931. ret = -ENOMEM;
  932. goto out_revert_acct;
  933. }
  934. /*
  935. * The pipe array wraps around, so just start the new one at zero
  936. * and adjust the indexes.
  937. */
  938. if (pipe->nrbufs) {
  939. unsigned int tail;
  940. unsigned int head;
  941. tail = pipe->curbuf + pipe->nrbufs;
  942. if (tail < pipe->buffers)
  943. tail = 0;
  944. else
  945. tail &= (pipe->buffers - 1);
  946. head = pipe->nrbufs - tail;
  947. if (head)
  948. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  949. if (tail)
  950. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  951. }
  952. pipe->curbuf = 0;
  953. kfree(pipe->bufs);
  954. pipe->bufs = bufs;
  955. pipe->buffers = nr_pages;
  956. return nr_pages * PAGE_SIZE;
  957. out_revert_acct:
  958. (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
  959. return ret;
  960. }
  961. /*
  962. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  963. * location, so checking ->i_pipe is not enough to verify that this is a
  964. * pipe.
  965. */
  966. struct pipe_inode_info *get_pipe_info(struct file *file)
  967. {
  968. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  969. }
  970. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  971. {
  972. struct pipe_inode_info *pipe;
  973. long ret;
  974. pipe = get_pipe_info(file);
  975. if (!pipe)
  976. return -EBADF;
  977. __pipe_lock(pipe);
  978. switch (cmd) {
  979. case F_SETPIPE_SZ:
  980. ret = pipe_set_size(pipe, arg);
  981. break;
  982. case F_GETPIPE_SZ:
  983. ret = pipe->buffers * PAGE_SIZE;
  984. break;
  985. default:
  986. ret = -EINVAL;
  987. break;
  988. }
  989. __pipe_unlock(pipe);
  990. return ret;
  991. }
  992. static const struct super_operations pipefs_ops = {
  993. .destroy_inode = free_inode_nonrcu,
  994. .statfs = simple_statfs,
  995. };
  996. /*
  997. * pipefs should _never_ be mounted by userland - too much of security hassle,
  998. * no real gain from having the whole whorehouse mounted. So we don't need
  999. * any operations on the root directory. However, we need a non-trivial
  1000. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1001. */
  1002. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1003. int flags, const char *dev_name, void *data)
  1004. {
  1005. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1006. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1007. }
  1008. static struct file_system_type pipe_fs_type = {
  1009. .name = "pipefs",
  1010. .mount = pipefs_mount,
  1011. .kill_sb = kill_anon_super,
  1012. };
  1013. static int __init init_pipe_fs(void)
  1014. {
  1015. int err = register_filesystem(&pipe_fs_type);
  1016. if (!err) {
  1017. pipe_mnt = kern_mount(&pipe_fs_type);
  1018. if (IS_ERR(pipe_mnt)) {
  1019. err = PTR_ERR(pipe_mnt);
  1020. unregister_filesystem(&pipe_fs_type);
  1021. }
  1022. }
  1023. return err;
  1024. }
  1025. fs_initcall(init_pipe_fs);