pipe.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. int err;
  649. struct inode *inode = get_pipe_inode();
  650. struct file *f;
  651. struct path path;
  652. if (!inode)
  653. return -ENFILE;
  654. err = -ENOMEM;
  655. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &empty_name);
  656. if (!path.dentry)
  657. goto err_inode;
  658. path.mnt = mntget(pipe_mnt);
  659. d_instantiate(path.dentry, inode);
  660. f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
  661. if (IS_ERR(f)) {
  662. err = PTR_ERR(f);
  663. goto err_dentry;
  664. }
  665. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  666. f->private_data = inode->i_pipe;
  667. res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
  668. if (IS_ERR(res[0])) {
  669. err = PTR_ERR(res[0]);
  670. goto err_file;
  671. }
  672. path_get(&path);
  673. res[0]->private_data = inode->i_pipe;
  674. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  675. res[1] = f;
  676. return 0;
  677. err_file:
  678. put_filp(f);
  679. err_dentry:
  680. free_pipe_info(inode->i_pipe);
  681. path_put(&path);
  682. return err;
  683. err_inode:
  684. free_pipe_info(inode->i_pipe);
  685. iput(inode);
  686. return err;
  687. }
  688. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  689. {
  690. int error;
  691. int fdw, fdr;
  692. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  693. return -EINVAL;
  694. error = create_pipe_files(files, flags);
  695. if (error)
  696. return error;
  697. error = get_unused_fd_flags(flags);
  698. if (error < 0)
  699. goto err_read_pipe;
  700. fdr = error;
  701. error = get_unused_fd_flags(flags);
  702. if (error < 0)
  703. goto err_fdr;
  704. fdw = error;
  705. audit_fd_pair(fdr, fdw);
  706. fd[0] = fdr;
  707. fd[1] = fdw;
  708. return 0;
  709. err_fdr:
  710. put_unused_fd(fdr);
  711. err_read_pipe:
  712. fput(files[0]);
  713. fput(files[1]);
  714. return error;
  715. }
  716. int do_pipe_flags(int *fd, int flags)
  717. {
  718. struct file *files[2];
  719. int error = __do_pipe_flags(fd, files, flags);
  720. if (!error) {
  721. fd_install(fd[0], files[0]);
  722. fd_install(fd[1], files[1]);
  723. }
  724. return error;
  725. }
  726. /*
  727. * sys_pipe() is the normal C calling standard for creating
  728. * a pipe. It's not the way Unix traditionally does this, though.
  729. */
  730. static int do_pipe2(int __user *fildes, int flags)
  731. {
  732. struct file *files[2];
  733. int fd[2];
  734. int error;
  735. error = __do_pipe_flags(fd, files, flags);
  736. if (!error) {
  737. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  738. fput(files[0]);
  739. fput(files[1]);
  740. put_unused_fd(fd[0]);
  741. put_unused_fd(fd[1]);
  742. error = -EFAULT;
  743. } else {
  744. fd_install(fd[0], files[0]);
  745. fd_install(fd[1], files[1]);
  746. }
  747. }
  748. return error;
  749. }
  750. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  751. {
  752. return do_pipe2(fildes, flags);
  753. }
  754. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  755. {
  756. return do_pipe2(fildes, 0);
  757. }
  758. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  759. {
  760. int cur = *cnt;
  761. while (cur == *cnt) {
  762. pipe_wait(pipe);
  763. if (signal_pending(current))
  764. break;
  765. }
  766. return cur == *cnt ? -ERESTARTSYS : 0;
  767. }
  768. static void wake_up_partner(struct pipe_inode_info *pipe)
  769. {
  770. wake_up_interruptible(&pipe->wait);
  771. }
  772. static int fifo_open(struct inode *inode, struct file *filp)
  773. {
  774. struct pipe_inode_info *pipe;
  775. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  776. int ret;
  777. filp->f_version = 0;
  778. spin_lock(&inode->i_lock);
  779. if (inode->i_pipe) {
  780. pipe = inode->i_pipe;
  781. pipe->files++;
  782. spin_unlock(&inode->i_lock);
  783. } else {
  784. spin_unlock(&inode->i_lock);
  785. pipe = alloc_pipe_info();
  786. if (!pipe)
  787. return -ENOMEM;
  788. pipe->files = 1;
  789. spin_lock(&inode->i_lock);
  790. if (unlikely(inode->i_pipe)) {
  791. inode->i_pipe->files++;
  792. spin_unlock(&inode->i_lock);
  793. free_pipe_info(pipe);
  794. pipe = inode->i_pipe;
  795. } else {
  796. inode->i_pipe = pipe;
  797. spin_unlock(&inode->i_lock);
  798. }
  799. }
  800. filp->private_data = pipe;
  801. /* OK, we have a pipe and it's pinned down */
  802. __pipe_lock(pipe);
  803. /* We can only do regular read/write on fifos */
  804. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  805. switch (filp->f_mode) {
  806. case FMODE_READ:
  807. /*
  808. * O_RDONLY
  809. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  810. * opened, even when there is no process writing the FIFO.
  811. */
  812. pipe->r_counter++;
  813. if (pipe->readers++ == 0)
  814. wake_up_partner(pipe);
  815. if (!is_pipe && !pipe->writers) {
  816. if ((filp->f_flags & O_NONBLOCK)) {
  817. /* suppress EPOLLHUP until we have
  818. * seen a writer */
  819. filp->f_version = pipe->w_counter;
  820. } else {
  821. if (wait_for_partner(pipe, &pipe->w_counter))
  822. goto err_rd;
  823. }
  824. }
  825. break;
  826. case FMODE_WRITE:
  827. /*
  828. * O_WRONLY
  829. * POSIX.1 says that O_NONBLOCK means return -1 with
  830. * errno=ENXIO when there is no process reading the FIFO.
  831. */
  832. ret = -ENXIO;
  833. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  834. goto err;
  835. pipe->w_counter++;
  836. if (!pipe->writers++)
  837. wake_up_partner(pipe);
  838. if (!is_pipe && !pipe->readers) {
  839. if (wait_for_partner(pipe, &pipe->r_counter))
  840. goto err_wr;
  841. }
  842. break;
  843. case FMODE_READ | FMODE_WRITE:
  844. /*
  845. * O_RDWR
  846. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  847. * This implementation will NEVER block on a O_RDWR open, since
  848. * the process can at least talk to itself.
  849. */
  850. pipe->readers++;
  851. pipe->writers++;
  852. pipe->r_counter++;
  853. pipe->w_counter++;
  854. if (pipe->readers == 1 || pipe->writers == 1)
  855. wake_up_partner(pipe);
  856. break;
  857. default:
  858. ret = -EINVAL;
  859. goto err;
  860. }
  861. /* Ok! */
  862. __pipe_unlock(pipe);
  863. return 0;
  864. err_rd:
  865. if (!--pipe->readers)
  866. wake_up_interruptible(&pipe->wait);
  867. ret = -ERESTARTSYS;
  868. goto err;
  869. err_wr:
  870. if (!--pipe->writers)
  871. wake_up_interruptible(&pipe->wait);
  872. ret = -ERESTARTSYS;
  873. goto err;
  874. err:
  875. __pipe_unlock(pipe);
  876. put_pipe_info(inode, pipe);
  877. return ret;
  878. }
  879. const struct file_operations pipefifo_fops = {
  880. .open = fifo_open,
  881. .llseek = no_llseek,
  882. .read_iter = pipe_read,
  883. .write_iter = pipe_write,
  884. .poll = pipe_poll,
  885. .unlocked_ioctl = pipe_ioctl,
  886. .release = pipe_release,
  887. .fasync = pipe_fasync,
  888. };
  889. /*
  890. * Currently we rely on the pipe array holding a power-of-2 number
  891. * of pages. Returns 0 on error.
  892. */
  893. unsigned int round_pipe_size(unsigned long size)
  894. {
  895. if (size > (1U << 31))
  896. return 0;
  897. /* Minimum pipe size, as required by POSIX */
  898. if (size < PAGE_SIZE)
  899. return PAGE_SIZE;
  900. return roundup_pow_of_two(size);
  901. }
  902. /*
  903. * Allocate a new array of pipe buffers and copy the info over. Returns the
  904. * pipe size if successful, or return -ERROR on error.
  905. */
  906. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
  907. {
  908. struct pipe_buffer *bufs;
  909. unsigned int size, nr_pages;
  910. unsigned long user_bufs;
  911. long ret = 0;
  912. size = round_pipe_size(arg);
  913. nr_pages = size >> PAGE_SHIFT;
  914. if (!nr_pages)
  915. return -EINVAL;
  916. /*
  917. * If trying to increase the pipe capacity, check that an
  918. * unprivileged user is not trying to exceed various limits
  919. * (soft limit check here, hard limit check just below).
  920. * Decreasing the pipe capacity is always permitted, even
  921. * if the user is currently over a limit.
  922. */
  923. if (nr_pages > pipe->buffers &&
  924. size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
  925. return -EPERM;
  926. user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
  927. if (nr_pages > pipe->buffers &&
  928. (too_many_pipe_buffers_hard(user_bufs) ||
  929. too_many_pipe_buffers_soft(user_bufs)) &&
  930. is_unprivileged_user()) {
  931. ret = -EPERM;
  932. goto out_revert_acct;
  933. }
  934. /*
  935. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  936. * expect a lot of shrink+grow operations, just free and allocate
  937. * again like we would do for growing. If the pipe currently
  938. * contains more buffers than arg, then return busy.
  939. */
  940. if (nr_pages < pipe->nrbufs) {
  941. ret = -EBUSY;
  942. goto out_revert_acct;
  943. }
  944. bufs = kcalloc(nr_pages, sizeof(*bufs),
  945. GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
  946. if (unlikely(!bufs)) {
  947. ret = -ENOMEM;
  948. goto out_revert_acct;
  949. }
  950. /*
  951. * The pipe array wraps around, so just start the new one at zero
  952. * and adjust the indexes.
  953. */
  954. if (pipe->nrbufs) {
  955. unsigned int tail;
  956. unsigned int head;
  957. tail = pipe->curbuf + pipe->nrbufs;
  958. if (tail < pipe->buffers)
  959. tail = 0;
  960. else
  961. tail &= (pipe->buffers - 1);
  962. head = pipe->nrbufs - tail;
  963. if (head)
  964. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  965. if (tail)
  966. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  967. }
  968. pipe->curbuf = 0;
  969. kfree(pipe->bufs);
  970. pipe->bufs = bufs;
  971. pipe->buffers = nr_pages;
  972. return nr_pages * PAGE_SIZE;
  973. out_revert_acct:
  974. (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
  975. return ret;
  976. }
  977. /*
  978. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  979. * location, so checking ->i_pipe is not enough to verify that this is a
  980. * pipe.
  981. */
  982. struct pipe_inode_info *get_pipe_info(struct file *file)
  983. {
  984. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  985. }
  986. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  987. {
  988. struct pipe_inode_info *pipe;
  989. long ret;
  990. pipe = get_pipe_info(file);
  991. if (!pipe)
  992. return -EBADF;
  993. __pipe_lock(pipe);
  994. switch (cmd) {
  995. case F_SETPIPE_SZ:
  996. ret = pipe_set_size(pipe, arg);
  997. break;
  998. case F_GETPIPE_SZ:
  999. ret = pipe->buffers * PAGE_SIZE;
  1000. break;
  1001. default:
  1002. ret = -EINVAL;
  1003. break;
  1004. }
  1005. __pipe_unlock(pipe);
  1006. return ret;
  1007. }
  1008. static const struct super_operations pipefs_ops = {
  1009. .destroy_inode = free_inode_nonrcu,
  1010. .statfs = simple_statfs,
  1011. };
  1012. /*
  1013. * pipefs should _never_ be mounted by userland - too much of security hassle,
  1014. * no real gain from having the whole whorehouse mounted. So we don't need
  1015. * any operations on the root directory. However, we need a non-trivial
  1016. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1017. */
  1018. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1019. int flags, const char *dev_name, void *data)
  1020. {
  1021. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1022. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1023. }
  1024. static struct file_system_type pipe_fs_type = {
  1025. .name = "pipefs",
  1026. .mount = pipefs_mount,
  1027. .kill_sb = kill_anon_super,
  1028. };
  1029. static int __init init_pipe_fs(void)
  1030. {
  1031. int err = register_filesystem(&pipe_fs_type);
  1032. if (!err) {
  1033. pipe_mnt = kern_mount(&pipe_fs_type);
  1034. if (IS_ERR(pipe_mnt)) {
  1035. err = PTR_ERR(pipe_mnt);
  1036. unregister_filesystem(&pipe_fs_type);
  1037. }
  1038. }
  1039. return err;
  1040. }
  1041. fs_initcall(init_pipe_fs);