af_unix.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NET4: Implementation of BSD Unix domain sockets.
  4. *
  5. * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk>
  6. *
  7. * Fixes:
  8. * Linus Torvalds : Assorted bug cures.
  9. * Niibe Yutaka : async I/O support.
  10. * Carsten Paeth : PF_UNIX check, address fixes.
  11. * Alan Cox : Limit size of allocated blocks.
  12. * Alan Cox : Fixed the stupid socketpair bug.
  13. * Alan Cox : BSD compatibility fine tuning.
  14. * Alan Cox : Fixed a bug in connect when interrupted.
  15. * Alan Cox : Sorted out a proper draft version of
  16. * file descriptor passing hacked up from
  17. * Mike Shaver's work.
  18. * Marty Leisner : Fixes to fd passing
  19. * Nick Nevin : recvmsg bugfix.
  20. * Alan Cox : Started proper garbage collector
  21. * Heiko EiBfeldt : Missing verify_area check
  22. * Alan Cox : Started POSIXisms
  23. * Andreas Schwab : Replace inode by dentry for proper
  24. * reference counting
  25. * Kirk Petersen : Made this a module
  26. * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
  27. * Lots of bug fixes.
  28. * Alexey Kuznetosv : Repaired (I hope) bugs introduces
  29. * by above two patches.
  30. * Andrea Arcangeli : If possible we block in connect(2)
  31. * if the max backlog of the listen socket
  32. * is been reached. This won't break
  33. * old apps and it will avoid huge amount
  34. * of socks hashed (this for unix_gc()
  35. * performances reasons).
  36. * Security fix that limits the max
  37. * number of socks to 2*max_files and
  38. * the number of skb queueable in the
  39. * dgram receiver.
  40. * Artur Skawina : Hash function optimizations
  41. * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8)
  42. * Malcolm Beattie : Set peercred for socketpair
  43. * Michal Ostrowski : Module initialization cleanup.
  44. * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
  45. * the core infrastructure is doing that
  46. * for all net proto families now (2.5.69+)
  47. *
  48. * Known differences from reference BSD that was tested:
  49. *
  50. * [TO FIX]
  51. * ECONNREFUSED is not returned from one end of a connected() socket to the
  52. * other the moment one end closes.
  53. * fstat() doesn't return st_dev=0, and give the blksize as high water mark
  54. * and a fake inode identifier (nor the BSD first socket fstat twice bug).
  55. * [NOT TO FIX]
  56. * accept() returns a path name even if the connecting socket has closed
  57. * in the meantime (BSD loses the path and gives up).
  58. * accept() returns 0 length path for an unbound connector. BSD returns 16
  59. * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
  60. * socketpair(...SOCK_RAW..) doesn't panic the kernel.
  61. * BSD af_unix apparently has connect forgetting to block properly.
  62. * (need to check this with the POSIX spec in detail)
  63. *
  64. * Differences from 2.0.0-11-... (ANK)
  65. * Bug fixes and improvements.
  66. * - client shutdown killed server socket.
  67. * - removed all useless cli/sti pairs.
  68. *
  69. * Semantic changes/extensions.
  70. * - generic control message passing.
  71. * - SCM_CREDENTIALS control message.
  72. * - "Abstract" (not FS based) socket bindings.
  73. * Abstract names are sequences of bytes (not zero terminated)
  74. * started by 0, so that this name space does not intersect
  75. * with BSD names.
  76. */
  77. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  78. #include <linux/module.h>
  79. #include <linux/kernel.h>
  80. #include <linux/signal.h>
  81. #include <linux/sched/signal.h>
  82. #include <linux/errno.h>
  83. #include <linux/string.h>
  84. #include <linux/stat.h>
  85. #include <linux/dcache.h>
  86. #include <linux/namei.h>
  87. #include <linux/socket.h>
  88. #include <linux/un.h>
  89. #include <linux/fcntl.h>
  90. #include <linux/termios.h>
  91. #include <linux/sockios.h>
  92. #include <linux/net.h>
  93. #include <linux/in.h>
  94. #include <linux/fs.h>
  95. #include <linux/slab.h>
  96. #include <linux/uaccess.h>
  97. #include <linux/skbuff.h>
  98. #include <linux/netdevice.h>
  99. #include <net/net_namespace.h>
  100. #include <net/sock.h>
  101. #include <net/tcp_states.h>
  102. #include <net/af_unix.h>
  103. #include <linux/proc_fs.h>
  104. #include <linux/seq_file.h>
  105. #include <net/scm.h>
  106. #include <linux/init.h>
  107. #include <linux/poll.h>
  108. #include <linux/rtnetlink.h>
  109. #include <linux/mount.h>
  110. #include <net/checksum.h>
  111. #include <linux/security.h>
  112. #include <linux/freezer.h>
  113. #include <linux/file.h>
  114. #include "scm.h"
  115. struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
  116. EXPORT_SYMBOL_GPL(unix_socket_table);
  117. DEFINE_SPINLOCK(unix_table_lock);
  118. EXPORT_SYMBOL_GPL(unix_table_lock);
  119. static atomic_long_t unix_nr_socks;
  120. static struct hlist_head *unix_sockets_unbound(void *addr)
  121. {
  122. unsigned long hash = (unsigned long)addr;
  123. hash ^= hash >> 16;
  124. hash ^= hash >> 8;
  125. hash %= UNIX_HASH_SIZE;
  126. return &unix_socket_table[UNIX_HASH_SIZE + hash];
  127. }
  128. #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash < UNIX_HASH_SIZE)
  129. #ifdef CONFIG_SECURITY_NETWORK
  130. static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  131. {
  132. UNIXCB(skb).secid = scm->secid;
  133. }
  134. static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  135. {
  136. scm->secid = UNIXCB(skb).secid;
  137. }
  138. static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
  139. {
  140. return (scm->secid == UNIXCB(skb).secid);
  141. }
  142. #else
  143. static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  144. { }
  145. static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  146. { }
  147. static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
  148. {
  149. return true;
  150. }
  151. #endif /* CONFIG_SECURITY_NETWORK */
  152. /*
  153. * SMP locking strategy:
  154. * hash table is protected with spinlock unix_table_lock
  155. * each socket state is protected by separate spin lock.
  156. */
  157. static inline unsigned int unix_hash_fold(__wsum n)
  158. {
  159. unsigned int hash = (__force unsigned int)csum_fold(n);
  160. hash ^= hash>>8;
  161. return hash&(UNIX_HASH_SIZE-1);
  162. }
  163. #define unix_peer(sk) (unix_sk(sk)->peer)
  164. static inline int unix_our_peer(struct sock *sk, struct sock *osk)
  165. {
  166. return unix_peer(osk) == sk;
  167. }
  168. static inline int unix_may_send(struct sock *sk, struct sock *osk)
  169. {
  170. return unix_peer(osk) == NULL || unix_our_peer(sk, osk);
  171. }
  172. static inline int unix_recvq_full(const struct sock *sk)
  173. {
  174. return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
  175. }
  176. static inline int unix_recvq_full_lockless(const struct sock *sk)
  177. {
  178. return skb_queue_len_lockless(&sk->sk_receive_queue) >
  179. READ_ONCE(sk->sk_max_ack_backlog);
  180. }
  181. struct sock *unix_peer_get(struct sock *s)
  182. {
  183. struct sock *peer;
  184. unix_state_lock(s);
  185. peer = unix_peer(s);
  186. if (peer)
  187. sock_hold(peer);
  188. unix_state_unlock(s);
  189. return peer;
  190. }
  191. EXPORT_SYMBOL_GPL(unix_peer_get);
  192. static inline void unix_release_addr(struct unix_address *addr)
  193. {
  194. if (refcount_dec_and_test(&addr->refcnt))
  195. kfree(addr);
  196. }
  197. /*
  198. * Check unix socket name:
  199. * - should be not zero length.
  200. * - if started by not zero, should be NULL terminated (FS object)
  201. * - if started by zero, it is abstract name.
  202. */
  203. static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp)
  204. {
  205. *hashp = 0;
  206. if (len <= sizeof(short) || len > sizeof(*sunaddr))
  207. return -EINVAL;
  208. if (!sunaddr || sunaddr->sun_family != AF_UNIX)
  209. return -EINVAL;
  210. if (sunaddr->sun_path[0]) {
  211. /*
  212. * This may look like an off by one error but it is a bit more
  213. * subtle. 108 is the longest valid AF_UNIX path for a binding.
  214. * sun_path[108] doesn't as such exist. However in kernel space
  215. * we are guaranteed that it is a valid memory location in our
  216. * kernel address buffer.
  217. */
  218. ((char *)sunaddr)[len] = 0;
  219. len = strlen(sunaddr->sun_path)+1+sizeof(short);
  220. return len;
  221. }
  222. *hashp = unix_hash_fold(csum_partial(sunaddr, len, 0));
  223. return len;
  224. }
  225. static void __unix_remove_socket(struct sock *sk)
  226. {
  227. sk_del_node_init(sk);
  228. }
  229. static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
  230. {
  231. WARN_ON(!sk_unhashed(sk));
  232. sk_add_node(sk, list);
  233. }
  234. static inline void unix_remove_socket(struct sock *sk)
  235. {
  236. spin_lock(&unix_table_lock);
  237. __unix_remove_socket(sk);
  238. spin_unlock(&unix_table_lock);
  239. }
  240. static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
  241. {
  242. spin_lock(&unix_table_lock);
  243. __unix_insert_socket(list, sk);
  244. spin_unlock(&unix_table_lock);
  245. }
  246. static struct sock *__unix_find_socket_byname(struct net *net,
  247. struct sockaddr_un *sunname,
  248. int len, int type, unsigned int hash)
  249. {
  250. struct sock *s;
  251. sk_for_each(s, &unix_socket_table[hash ^ type]) {
  252. struct unix_sock *u = unix_sk(s);
  253. if (!net_eq(sock_net(s), net))
  254. continue;
  255. if (u->addr->len == len &&
  256. !memcmp(u->addr->name, sunname, len))
  257. return s;
  258. }
  259. return NULL;
  260. }
  261. static inline struct sock *unix_find_socket_byname(struct net *net,
  262. struct sockaddr_un *sunname,
  263. int len, int type,
  264. unsigned int hash)
  265. {
  266. struct sock *s;
  267. spin_lock(&unix_table_lock);
  268. s = __unix_find_socket_byname(net, sunname, len, type, hash);
  269. if (s)
  270. sock_hold(s);
  271. spin_unlock(&unix_table_lock);
  272. return s;
  273. }
  274. static struct sock *unix_find_socket_byinode(struct inode *i)
  275. {
  276. struct sock *s;
  277. spin_lock(&unix_table_lock);
  278. sk_for_each(s,
  279. &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
  280. struct dentry *dentry = unix_sk(s)->path.dentry;
  281. if (dentry && d_backing_inode(dentry) == i) {
  282. sock_hold(s);
  283. goto found;
  284. }
  285. }
  286. s = NULL;
  287. found:
  288. spin_unlock(&unix_table_lock);
  289. return s;
  290. }
  291. /* Support code for asymmetrically connected dgram sockets
  292. *
  293. * If a datagram socket is connected to a socket not itself connected
  294. * to the first socket (eg, /dev/log), clients may only enqueue more
  295. * messages if the present receive queue of the server socket is not
  296. * "too large". This means there's a second writeability condition
  297. * poll and sendmsg need to test. The dgram recv code will do a wake
  298. * up on the peer_wait wait queue of a socket upon reception of a
  299. * datagram which needs to be propagated to sleeping would-be writers
  300. * since these might not have sent anything so far. This can't be
  301. * accomplished via poll_wait because the lifetime of the server
  302. * socket might be less than that of its clients if these break their
  303. * association with it or if the server socket is closed while clients
  304. * are still connected to it and there's no way to inform "a polling
  305. * implementation" that it should let go of a certain wait queue
  306. *
  307. * In order to propagate a wake up, a wait_queue_entry_t of the client
  308. * socket is enqueued on the peer_wait queue of the server socket
  309. * whose wake function does a wake_up on the ordinary client socket
  310. * wait queue. This connection is established whenever a write (or
  311. * poll for write) hit the flow control condition and broken when the
  312. * association to the server socket is dissolved or after a wake up
  313. * was relayed.
  314. */
  315. static int unix_dgram_peer_wake_relay(wait_queue_entry_t *q, unsigned mode, int flags,
  316. void *key)
  317. {
  318. struct unix_sock *u;
  319. wait_queue_head_t *u_sleep;
  320. u = container_of(q, struct unix_sock, peer_wake);
  321. __remove_wait_queue(&unix_sk(u->peer_wake.private)->peer_wait,
  322. q);
  323. u->peer_wake.private = NULL;
  324. /* relaying can only happen while the wq still exists */
  325. u_sleep = sk_sleep(&u->sk);
  326. if (u_sleep)
  327. wake_up_interruptible_poll(u_sleep, key_to_poll(key));
  328. return 0;
  329. }
  330. static int unix_dgram_peer_wake_connect(struct sock *sk, struct sock *other)
  331. {
  332. struct unix_sock *u, *u_other;
  333. int rc;
  334. u = unix_sk(sk);
  335. u_other = unix_sk(other);
  336. rc = 0;
  337. spin_lock(&u_other->peer_wait.lock);
  338. if (!u->peer_wake.private) {
  339. u->peer_wake.private = other;
  340. __add_wait_queue(&u_other->peer_wait, &u->peer_wake);
  341. rc = 1;
  342. }
  343. spin_unlock(&u_other->peer_wait.lock);
  344. return rc;
  345. }
  346. static void unix_dgram_peer_wake_disconnect(struct sock *sk,
  347. struct sock *other)
  348. {
  349. struct unix_sock *u, *u_other;
  350. u = unix_sk(sk);
  351. u_other = unix_sk(other);
  352. spin_lock(&u_other->peer_wait.lock);
  353. if (u->peer_wake.private == other) {
  354. __remove_wait_queue(&u_other->peer_wait, &u->peer_wake);
  355. u->peer_wake.private = NULL;
  356. }
  357. spin_unlock(&u_other->peer_wait.lock);
  358. }
  359. static void unix_dgram_peer_wake_disconnect_wakeup(struct sock *sk,
  360. struct sock *other)
  361. {
  362. unix_dgram_peer_wake_disconnect(sk, other);
  363. wake_up_interruptible_poll(sk_sleep(sk),
  364. EPOLLOUT |
  365. EPOLLWRNORM |
  366. EPOLLWRBAND);
  367. }
  368. /* preconditions:
  369. * - unix_peer(sk) == other
  370. * - association is stable
  371. */
  372. static int unix_dgram_peer_wake_me(struct sock *sk, struct sock *other)
  373. {
  374. int connected;
  375. connected = unix_dgram_peer_wake_connect(sk, other);
  376. /* If other is SOCK_DEAD, we want to make sure we signal
  377. * POLLOUT, such that a subsequent write() can get a
  378. * -ECONNREFUSED. Otherwise, if we haven't queued any skbs
  379. * to other and its full, we will hang waiting for POLLOUT.
  380. */
  381. if (unix_recvq_full(other) && !sock_flag(other, SOCK_DEAD))
  382. return 1;
  383. if (connected)
  384. unix_dgram_peer_wake_disconnect(sk, other);
  385. return 0;
  386. }
  387. static int unix_writable(const struct sock *sk)
  388. {
  389. return sk->sk_state != TCP_LISTEN &&
  390. (refcount_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
  391. }
  392. static void unix_write_space(struct sock *sk)
  393. {
  394. struct socket_wq *wq;
  395. rcu_read_lock();
  396. if (unix_writable(sk)) {
  397. wq = rcu_dereference(sk->sk_wq);
  398. if (skwq_has_sleeper(wq))
  399. wake_up_interruptible_sync_poll(&wq->wait,
  400. EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
  401. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  402. }
  403. rcu_read_unlock();
  404. }
  405. /* When dgram socket disconnects (or changes its peer), we clear its receive
  406. * queue of packets arrived from previous peer. First, it allows to do
  407. * flow control based only on wmem_alloc; second, sk connected to peer
  408. * may receive messages only from that peer. */
  409. static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
  410. {
  411. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  412. skb_queue_purge(&sk->sk_receive_queue);
  413. wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
  414. /* If one link of bidirectional dgram pipe is disconnected,
  415. * we signal error. Messages are lost. Do not make this,
  416. * when peer was not connected to us.
  417. */
  418. if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
  419. other->sk_err = ECONNRESET;
  420. other->sk_error_report(other);
  421. }
  422. }
  423. }
  424. static void unix_sock_destructor(struct sock *sk)
  425. {
  426. struct unix_sock *u = unix_sk(sk);
  427. skb_queue_purge(&sk->sk_receive_queue);
  428. WARN_ON(refcount_read(&sk->sk_wmem_alloc));
  429. WARN_ON(!sk_unhashed(sk));
  430. WARN_ON(sk->sk_socket);
  431. if (!sock_flag(sk, SOCK_DEAD)) {
  432. pr_info("Attempt to release alive unix socket: %p\n", sk);
  433. return;
  434. }
  435. if (u->addr)
  436. unix_release_addr(u->addr);
  437. atomic_long_dec(&unix_nr_socks);
  438. local_bh_disable();
  439. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  440. local_bh_enable();
  441. #ifdef UNIX_REFCNT_DEBUG
  442. pr_debug("UNIX %p is destroyed, %ld are still alive.\n", sk,
  443. atomic_long_read(&unix_nr_socks));
  444. #endif
  445. }
  446. static void unix_release_sock(struct sock *sk, int embrion)
  447. {
  448. struct unix_sock *u = unix_sk(sk);
  449. struct path path;
  450. struct sock *skpair;
  451. struct sk_buff *skb;
  452. int state;
  453. unix_remove_socket(sk);
  454. /* Clear state */
  455. unix_state_lock(sk);
  456. sock_orphan(sk);
  457. sk->sk_shutdown = SHUTDOWN_MASK;
  458. path = u->path;
  459. u->path.dentry = NULL;
  460. u->path.mnt = NULL;
  461. state = sk->sk_state;
  462. sk->sk_state = TCP_CLOSE;
  463. skpair = unix_peer(sk);
  464. unix_peer(sk) = NULL;
  465. unix_state_unlock(sk);
  466. wake_up_interruptible_all(&u->peer_wait);
  467. if (skpair != NULL) {
  468. if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
  469. unix_state_lock(skpair);
  470. /* No more writes */
  471. skpair->sk_shutdown = SHUTDOWN_MASK;
  472. if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
  473. skpair->sk_err = ECONNRESET;
  474. unix_state_unlock(skpair);
  475. skpair->sk_state_change(skpair);
  476. sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP);
  477. }
  478. unix_dgram_peer_wake_disconnect(sk, skpair);
  479. sock_put(skpair); /* It may now die */
  480. }
  481. /* Try to flush out this socket. Throw out buffers at least */
  482. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  483. if (state == TCP_LISTEN)
  484. unix_release_sock(skb->sk, 1);
  485. /* passed fds are erased in the kfree_skb hook */
  486. UNIXCB(skb).consumed = skb->len;
  487. kfree_skb(skb);
  488. }
  489. if (path.dentry)
  490. path_put(&path);
  491. sock_put(sk);
  492. /* ---- Socket is dead now and most probably destroyed ---- */
  493. /*
  494. * Fixme: BSD difference: In BSD all sockets connected to us get
  495. * ECONNRESET and we die on the spot. In Linux we behave
  496. * like files and pipes do and wait for the last
  497. * dereference.
  498. *
  499. * Can't we simply set sock->err?
  500. *
  501. * What the above comment does talk about? --ANK(980817)
  502. */
  503. if (unix_tot_inflight)
  504. unix_gc(); /* Garbage collect fds */
  505. }
  506. static void init_peercred(struct sock *sk)
  507. {
  508. const struct cred *old_cred;
  509. struct pid *old_pid;
  510. spin_lock(&sk->sk_peer_lock);
  511. old_pid = sk->sk_peer_pid;
  512. old_cred = sk->sk_peer_cred;
  513. sk->sk_peer_pid = get_pid(task_tgid(current));
  514. sk->sk_peer_cred = get_current_cred();
  515. spin_unlock(&sk->sk_peer_lock);
  516. put_pid(old_pid);
  517. put_cred(old_cred);
  518. }
  519. static void copy_peercred(struct sock *sk, struct sock *peersk)
  520. {
  521. const struct cred *old_cred;
  522. struct pid *old_pid;
  523. if (sk < peersk) {
  524. spin_lock(&sk->sk_peer_lock);
  525. spin_lock_nested(&peersk->sk_peer_lock, SINGLE_DEPTH_NESTING);
  526. } else {
  527. spin_lock(&peersk->sk_peer_lock);
  528. spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING);
  529. }
  530. old_pid = sk->sk_peer_pid;
  531. old_cred = sk->sk_peer_cred;
  532. sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
  533. sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
  534. spin_unlock(&sk->sk_peer_lock);
  535. spin_unlock(&peersk->sk_peer_lock);
  536. put_pid(old_pid);
  537. put_cred(old_cred);
  538. }
  539. static int unix_listen(struct socket *sock, int backlog)
  540. {
  541. int err;
  542. struct sock *sk = sock->sk;
  543. struct unix_sock *u = unix_sk(sk);
  544. err = -EOPNOTSUPP;
  545. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  546. goto out; /* Only stream/seqpacket sockets accept */
  547. err = -EINVAL;
  548. if (!u->addr)
  549. goto out; /* No listens on an unbound socket */
  550. unix_state_lock(sk);
  551. if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
  552. goto out_unlock;
  553. if (backlog > sk->sk_max_ack_backlog)
  554. wake_up_interruptible_all(&u->peer_wait);
  555. sk->sk_max_ack_backlog = backlog;
  556. sk->sk_state = TCP_LISTEN;
  557. /* set credentials so connect can copy them */
  558. init_peercred(sk);
  559. err = 0;
  560. out_unlock:
  561. unix_state_unlock(sk);
  562. out:
  563. return err;
  564. }
  565. static int unix_release(struct socket *);
  566. static int unix_bind(struct socket *, struct sockaddr *, int);
  567. static int unix_stream_connect(struct socket *, struct sockaddr *,
  568. int addr_len, int flags);
  569. static int unix_socketpair(struct socket *, struct socket *);
  570. static int unix_accept(struct socket *, struct socket *, int, bool);
  571. static int unix_getname(struct socket *, struct sockaddr *, int);
  572. static __poll_t unix_poll(struct file *, struct socket *, poll_table *);
  573. static __poll_t unix_dgram_poll(struct file *, struct socket *,
  574. poll_table *);
  575. static int unix_ioctl(struct socket *, unsigned int, unsigned long);
  576. #ifdef CONFIG_COMPAT
  577. static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
  578. #endif
  579. static int unix_shutdown(struct socket *, int);
  580. static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t);
  581. static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int);
  582. static ssize_t unix_stream_sendpage(struct socket *, struct page *, int offset,
  583. size_t size, int flags);
  584. static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos,
  585. struct pipe_inode_info *, size_t size,
  586. unsigned int flags);
  587. static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
  588. static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
  589. static int unix_dgram_connect(struct socket *, struct sockaddr *,
  590. int, int);
  591. static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
  592. static int unix_seqpacket_recvmsg(struct socket *, struct msghdr *, size_t,
  593. int);
  594. static int unix_set_peek_off(struct sock *sk, int val)
  595. {
  596. struct unix_sock *u = unix_sk(sk);
  597. if (mutex_lock_interruptible(&u->iolock))
  598. return -EINTR;
  599. sk->sk_peek_off = val;
  600. mutex_unlock(&u->iolock);
  601. return 0;
  602. }
  603. #ifdef CONFIG_PROC_FS
  604. static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
  605. {
  606. struct sock *sk = sock->sk;
  607. struct unix_sock *u;
  608. if (sk) {
  609. u = unix_sk(sock->sk);
  610. seq_printf(m, "scm_fds: %u\n",
  611. atomic_read(&u->scm_stat.nr_fds));
  612. }
  613. }
  614. #else
  615. #define unix_show_fdinfo NULL
  616. #endif
  617. static const struct proto_ops unix_stream_ops = {
  618. .family = PF_UNIX,
  619. .owner = THIS_MODULE,
  620. .release = unix_release,
  621. .bind = unix_bind,
  622. .connect = unix_stream_connect,
  623. .socketpair = unix_socketpair,
  624. .accept = unix_accept,
  625. .getname = unix_getname,
  626. .poll = unix_poll,
  627. .ioctl = unix_ioctl,
  628. #ifdef CONFIG_COMPAT
  629. .compat_ioctl = unix_compat_ioctl,
  630. #endif
  631. .listen = unix_listen,
  632. .shutdown = unix_shutdown,
  633. .sendmsg = unix_stream_sendmsg,
  634. .recvmsg = unix_stream_recvmsg,
  635. .mmap = sock_no_mmap,
  636. .sendpage = unix_stream_sendpage,
  637. .splice_read = unix_stream_splice_read,
  638. .set_peek_off = unix_set_peek_off,
  639. .show_fdinfo = unix_show_fdinfo,
  640. };
  641. static const struct proto_ops unix_dgram_ops = {
  642. .family = PF_UNIX,
  643. .owner = THIS_MODULE,
  644. .release = unix_release,
  645. .bind = unix_bind,
  646. .connect = unix_dgram_connect,
  647. .socketpair = unix_socketpair,
  648. .accept = sock_no_accept,
  649. .getname = unix_getname,
  650. .poll = unix_dgram_poll,
  651. .ioctl = unix_ioctl,
  652. #ifdef CONFIG_COMPAT
  653. .compat_ioctl = unix_compat_ioctl,
  654. #endif
  655. .listen = sock_no_listen,
  656. .shutdown = unix_shutdown,
  657. .sendmsg = unix_dgram_sendmsg,
  658. .recvmsg = unix_dgram_recvmsg,
  659. .mmap = sock_no_mmap,
  660. .sendpage = sock_no_sendpage,
  661. .set_peek_off = unix_set_peek_off,
  662. .show_fdinfo = unix_show_fdinfo,
  663. };
  664. static const struct proto_ops unix_seqpacket_ops = {
  665. .family = PF_UNIX,
  666. .owner = THIS_MODULE,
  667. .release = unix_release,
  668. .bind = unix_bind,
  669. .connect = unix_stream_connect,
  670. .socketpair = unix_socketpair,
  671. .accept = unix_accept,
  672. .getname = unix_getname,
  673. .poll = unix_dgram_poll,
  674. .ioctl = unix_ioctl,
  675. #ifdef CONFIG_COMPAT
  676. .compat_ioctl = unix_compat_ioctl,
  677. #endif
  678. .listen = unix_listen,
  679. .shutdown = unix_shutdown,
  680. .sendmsg = unix_seqpacket_sendmsg,
  681. .recvmsg = unix_seqpacket_recvmsg,
  682. .mmap = sock_no_mmap,
  683. .sendpage = sock_no_sendpage,
  684. .set_peek_off = unix_set_peek_off,
  685. .show_fdinfo = unix_show_fdinfo,
  686. };
  687. static struct proto unix_proto = {
  688. .name = "UNIX",
  689. .owner = THIS_MODULE,
  690. .obj_size = sizeof(struct unix_sock),
  691. };
  692. static struct sock *unix_create1(struct net *net, struct socket *sock, int kern)
  693. {
  694. struct sock *sk = NULL;
  695. struct unix_sock *u;
  696. atomic_long_inc(&unix_nr_socks);
  697. if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
  698. goto out;
  699. sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto, kern);
  700. if (!sk)
  701. goto out;
  702. sock_init_data(sock, sk);
  703. sk->sk_allocation = GFP_KERNEL_ACCOUNT;
  704. sk->sk_write_space = unix_write_space;
  705. sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
  706. sk->sk_destruct = unix_sock_destructor;
  707. u = unix_sk(sk);
  708. u->path.dentry = NULL;
  709. u->path.mnt = NULL;
  710. spin_lock_init(&u->lock);
  711. atomic_long_set(&u->inflight, 0);
  712. INIT_LIST_HEAD(&u->link);
  713. mutex_init(&u->iolock); /* single task reading lock */
  714. mutex_init(&u->bindlock); /* single task binding lock */
  715. init_waitqueue_head(&u->peer_wait);
  716. init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
  717. memset(&u->scm_stat, 0, sizeof(struct scm_stat));
  718. unix_insert_socket(unix_sockets_unbound(sk), sk);
  719. out:
  720. if (sk == NULL)
  721. atomic_long_dec(&unix_nr_socks);
  722. else {
  723. local_bh_disable();
  724. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  725. local_bh_enable();
  726. }
  727. return sk;
  728. }
  729. static int unix_create(struct net *net, struct socket *sock, int protocol,
  730. int kern)
  731. {
  732. if (protocol && protocol != PF_UNIX)
  733. return -EPROTONOSUPPORT;
  734. sock->state = SS_UNCONNECTED;
  735. switch (sock->type) {
  736. case SOCK_STREAM:
  737. sock->ops = &unix_stream_ops;
  738. break;
  739. /*
  740. * Believe it or not BSD has AF_UNIX, SOCK_RAW though
  741. * nothing uses it.
  742. */
  743. case SOCK_RAW:
  744. sock->type = SOCK_DGRAM;
  745. fallthrough;
  746. case SOCK_DGRAM:
  747. sock->ops = &unix_dgram_ops;
  748. break;
  749. case SOCK_SEQPACKET:
  750. sock->ops = &unix_seqpacket_ops;
  751. break;
  752. default:
  753. return -ESOCKTNOSUPPORT;
  754. }
  755. return unix_create1(net, sock, kern) ? 0 : -ENOMEM;
  756. }
  757. static int unix_release(struct socket *sock)
  758. {
  759. struct sock *sk = sock->sk;
  760. if (!sk)
  761. return 0;
  762. unix_release_sock(sk, 0);
  763. sock->sk = NULL;
  764. return 0;
  765. }
  766. static int unix_autobind(struct socket *sock)
  767. {
  768. struct sock *sk = sock->sk;
  769. struct net *net = sock_net(sk);
  770. struct unix_sock *u = unix_sk(sk);
  771. static u32 ordernum = 1;
  772. struct unix_address *addr;
  773. int err;
  774. unsigned int retries = 0;
  775. err = mutex_lock_interruptible(&u->bindlock);
  776. if (err)
  777. return err;
  778. if (u->addr)
  779. goto out;
  780. err = -ENOMEM;
  781. addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
  782. if (!addr)
  783. goto out;
  784. addr->name->sun_family = AF_UNIX;
  785. refcount_set(&addr->refcnt, 1);
  786. retry:
  787. addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
  788. addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0));
  789. spin_lock(&unix_table_lock);
  790. ordernum = (ordernum+1)&0xFFFFF;
  791. if (__unix_find_socket_byname(net, addr->name, addr->len, sock->type,
  792. addr->hash)) {
  793. spin_unlock(&unix_table_lock);
  794. /*
  795. * __unix_find_socket_byname() may take long time if many names
  796. * are already in use.
  797. */
  798. cond_resched();
  799. /* Give up if all names seems to be in use. */
  800. if (retries++ == 0xFFFFF) {
  801. err = -ENOSPC;
  802. kfree(addr);
  803. goto out;
  804. }
  805. goto retry;
  806. }
  807. addr->hash ^= sk->sk_type;
  808. __unix_remove_socket(sk);
  809. smp_store_release(&u->addr, addr);
  810. __unix_insert_socket(&unix_socket_table[addr->hash], sk);
  811. spin_unlock(&unix_table_lock);
  812. err = 0;
  813. out: mutex_unlock(&u->bindlock);
  814. return err;
  815. }
  816. static struct sock *unix_find_other(struct net *net,
  817. struct sockaddr_un *sunname, int len,
  818. int type, unsigned int hash, int *error)
  819. {
  820. struct sock *u;
  821. struct path path;
  822. int err = 0;
  823. if (sunname->sun_path[0]) {
  824. struct inode *inode;
  825. err = kern_path(sunname->sun_path, LOOKUP_FOLLOW, &path);
  826. if (err)
  827. goto fail;
  828. inode = d_backing_inode(path.dentry);
  829. err = inode_permission(inode, MAY_WRITE);
  830. if (err)
  831. goto put_fail;
  832. err = -ECONNREFUSED;
  833. if (!S_ISSOCK(inode->i_mode))
  834. goto put_fail;
  835. u = unix_find_socket_byinode(inode);
  836. if (!u)
  837. goto put_fail;
  838. if (u->sk_type == type)
  839. touch_atime(&path);
  840. path_put(&path);
  841. err = -EPROTOTYPE;
  842. if (u->sk_type != type) {
  843. sock_put(u);
  844. goto fail;
  845. }
  846. } else {
  847. err = -ECONNREFUSED;
  848. u = unix_find_socket_byname(net, sunname, len, type, hash);
  849. if (u) {
  850. struct dentry *dentry;
  851. dentry = unix_sk(u)->path.dentry;
  852. if (dentry)
  853. touch_atime(&unix_sk(u)->path);
  854. } else
  855. goto fail;
  856. }
  857. return u;
  858. put_fail:
  859. path_put(&path);
  860. fail:
  861. *error = err;
  862. return NULL;
  863. }
  864. static int unix_mknod(const char *sun_path, umode_t mode, struct path *res)
  865. {
  866. struct dentry *dentry;
  867. struct path path;
  868. int err = 0;
  869. /*
  870. * Get the parent directory, calculate the hash for last
  871. * component.
  872. */
  873. dentry = kern_path_create(AT_FDCWD, sun_path, &path, 0);
  874. err = PTR_ERR(dentry);
  875. if (IS_ERR(dentry))
  876. return err;
  877. /*
  878. * All right, let's create it.
  879. */
  880. err = security_path_mknod(&path, dentry, mode, 0);
  881. if (!err) {
  882. err = vfs_mknod(d_inode(path.dentry), dentry, mode, 0);
  883. if (!err) {
  884. res->mnt = mntget(path.mnt);
  885. res->dentry = dget(dentry);
  886. }
  887. }
  888. done_path_create(&path, dentry);
  889. return err;
  890. }
  891. static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  892. {
  893. struct sock *sk = sock->sk;
  894. struct net *net = sock_net(sk);
  895. struct unix_sock *u = unix_sk(sk);
  896. struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
  897. char *sun_path = sunaddr->sun_path;
  898. int err;
  899. unsigned int hash;
  900. struct unix_address *addr;
  901. struct hlist_head *list;
  902. struct path path = { };
  903. err = -EINVAL;
  904. if (addr_len < offsetofend(struct sockaddr_un, sun_family) ||
  905. sunaddr->sun_family != AF_UNIX)
  906. goto out;
  907. if (addr_len == sizeof(short)) {
  908. err = unix_autobind(sock);
  909. goto out;
  910. }
  911. err = unix_mkname(sunaddr, addr_len, &hash);
  912. if (err < 0)
  913. goto out;
  914. addr_len = err;
  915. if (sun_path[0]) {
  916. umode_t mode = S_IFSOCK |
  917. (SOCK_INODE(sock)->i_mode & ~current_umask());
  918. err = unix_mknod(sun_path, mode, &path);
  919. if (err) {
  920. if (err == -EEXIST)
  921. err = -EADDRINUSE;
  922. goto out;
  923. }
  924. }
  925. err = mutex_lock_interruptible(&u->bindlock);
  926. if (err)
  927. goto out_put;
  928. err = -EINVAL;
  929. if (u->addr)
  930. goto out_up;
  931. err = -ENOMEM;
  932. addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
  933. if (!addr)
  934. goto out_up;
  935. memcpy(addr->name, sunaddr, addr_len);
  936. addr->len = addr_len;
  937. addr->hash = hash ^ sk->sk_type;
  938. refcount_set(&addr->refcnt, 1);
  939. if (sun_path[0]) {
  940. addr->hash = UNIX_HASH_SIZE;
  941. hash = d_backing_inode(path.dentry)->i_ino & (UNIX_HASH_SIZE - 1);
  942. spin_lock(&unix_table_lock);
  943. u->path = path;
  944. list = &unix_socket_table[hash];
  945. } else {
  946. spin_lock(&unix_table_lock);
  947. err = -EADDRINUSE;
  948. if (__unix_find_socket_byname(net, sunaddr, addr_len,
  949. sk->sk_type, hash)) {
  950. unix_release_addr(addr);
  951. goto out_unlock;
  952. }
  953. list = &unix_socket_table[addr->hash];
  954. }
  955. err = 0;
  956. __unix_remove_socket(sk);
  957. smp_store_release(&u->addr, addr);
  958. __unix_insert_socket(list, sk);
  959. out_unlock:
  960. spin_unlock(&unix_table_lock);
  961. out_up:
  962. mutex_unlock(&u->bindlock);
  963. out_put:
  964. if (err)
  965. path_put(&path);
  966. out:
  967. return err;
  968. }
  969. static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
  970. {
  971. if (unlikely(sk1 == sk2) || !sk2) {
  972. unix_state_lock(sk1);
  973. return;
  974. }
  975. if (sk1 < sk2) {
  976. unix_state_lock(sk1);
  977. unix_state_lock_nested(sk2);
  978. } else {
  979. unix_state_lock(sk2);
  980. unix_state_lock_nested(sk1);
  981. }
  982. }
  983. static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
  984. {
  985. if (unlikely(sk1 == sk2) || !sk2) {
  986. unix_state_unlock(sk1);
  987. return;
  988. }
  989. unix_state_unlock(sk1);
  990. unix_state_unlock(sk2);
  991. }
  992. static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
  993. int alen, int flags)
  994. {
  995. struct sock *sk = sock->sk;
  996. struct net *net = sock_net(sk);
  997. struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
  998. struct sock *other;
  999. unsigned int hash;
  1000. int err;
  1001. err = -EINVAL;
  1002. if (alen < offsetofend(struct sockaddr, sa_family))
  1003. goto out;
  1004. if (addr->sa_family != AF_UNSPEC) {
  1005. err = unix_mkname(sunaddr, alen, &hash);
  1006. if (err < 0)
  1007. goto out;
  1008. alen = err;
  1009. if (test_bit(SOCK_PASSCRED, &sock->flags) &&
  1010. !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
  1011. goto out;
  1012. restart:
  1013. other = unix_find_other(net, sunaddr, alen, sock->type, hash, &err);
  1014. if (!other)
  1015. goto out;
  1016. unix_state_double_lock(sk, other);
  1017. /* Apparently VFS overslept socket death. Retry. */
  1018. if (sock_flag(other, SOCK_DEAD)) {
  1019. unix_state_double_unlock(sk, other);
  1020. sock_put(other);
  1021. goto restart;
  1022. }
  1023. err = -EPERM;
  1024. if (!unix_may_send(sk, other))
  1025. goto out_unlock;
  1026. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  1027. if (err)
  1028. goto out_unlock;
  1029. } else {
  1030. /*
  1031. * 1003.1g breaking connected state with AF_UNSPEC
  1032. */
  1033. other = NULL;
  1034. unix_state_double_lock(sk, other);
  1035. }
  1036. /*
  1037. * If it was connected, reconnect.
  1038. */
  1039. if (unix_peer(sk)) {
  1040. struct sock *old_peer = unix_peer(sk);
  1041. unix_peer(sk) = other;
  1042. unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer);
  1043. unix_state_double_unlock(sk, other);
  1044. if (other != old_peer)
  1045. unix_dgram_disconnected(sk, old_peer);
  1046. sock_put(old_peer);
  1047. } else {
  1048. unix_peer(sk) = other;
  1049. unix_state_double_unlock(sk, other);
  1050. }
  1051. return 0;
  1052. out_unlock:
  1053. unix_state_double_unlock(sk, other);
  1054. sock_put(other);
  1055. out:
  1056. return err;
  1057. }
  1058. static long unix_wait_for_peer(struct sock *other, long timeo)
  1059. __releases(&unix_sk(other)->lock)
  1060. {
  1061. struct unix_sock *u = unix_sk(other);
  1062. int sched;
  1063. DEFINE_WAIT(wait);
  1064. prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
  1065. sched = !sock_flag(other, SOCK_DEAD) &&
  1066. !(other->sk_shutdown & RCV_SHUTDOWN) &&
  1067. unix_recvq_full(other);
  1068. unix_state_unlock(other);
  1069. if (sched)
  1070. timeo = schedule_timeout(timeo);
  1071. finish_wait(&u->peer_wait, &wait);
  1072. return timeo;
  1073. }
  1074. static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  1075. int addr_len, int flags)
  1076. {
  1077. struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
  1078. struct sock *sk = sock->sk;
  1079. struct net *net = sock_net(sk);
  1080. struct unix_sock *u = unix_sk(sk), *newu, *otheru;
  1081. struct sock *newsk = NULL;
  1082. struct sock *other = NULL;
  1083. struct sk_buff *skb = NULL;
  1084. unsigned int hash;
  1085. int st;
  1086. int err;
  1087. long timeo;
  1088. err = unix_mkname(sunaddr, addr_len, &hash);
  1089. if (err < 0)
  1090. goto out;
  1091. addr_len = err;
  1092. if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr &&
  1093. (err = unix_autobind(sock)) != 0)
  1094. goto out;
  1095. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  1096. /* First of all allocate resources.
  1097. If we will make it after state is locked,
  1098. we will have to recheck all again in any case.
  1099. */
  1100. err = -ENOMEM;
  1101. /* create new sock for complete connection */
  1102. newsk = unix_create1(sock_net(sk), NULL, 0);
  1103. if (newsk == NULL)
  1104. goto out;
  1105. /* Allocate skb for sending to listening sock */
  1106. skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
  1107. if (skb == NULL)
  1108. goto out;
  1109. restart:
  1110. /* Find listening sock. */
  1111. other = unix_find_other(net, sunaddr, addr_len, sk->sk_type, hash, &err);
  1112. if (!other)
  1113. goto out;
  1114. /* Latch state of peer */
  1115. unix_state_lock(other);
  1116. /* Apparently VFS overslept socket death. Retry. */
  1117. if (sock_flag(other, SOCK_DEAD)) {
  1118. unix_state_unlock(other);
  1119. sock_put(other);
  1120. goto restart;
  1121. }
  1122. err = -ECONNREFUSED;
  1123. if (other->sk_state != TCP_LISTEN)
  1124. goto out_unlock;
  1125. if (other->sk_shutdown & RCV_SHUTDOWN)
  1126. goto out_unlock;
  1127. if (unix_recvq_full(other)) {
  1128. err = -EAGAIN;
  1129. if (!timeo)
  1130. goto out_unlock;
  1131. timeo = unix_wait_for_peer(other, timeo);
  1132. err = sock_intr_errno(timeo);
  1133. if (signal_pending(current))
  1134. goto out;
  1135. sock_put(other);
  1136. goto restart;
  1137. }
  1138. /* Latch our state.
  1139. It is tricky place. We need to grab our state lock and cannot
  1140. drop lock on peer. It is dangerous because deadlock is
  1141. possible. Connect to self case and simultaneous
  1142. attempt to connect are eliminated by checking socket
  1143. state. other is TCP_LISTEN, if sk is TCP_LISTEN we
  1144. check this before attempt to grab lock.
  1145. Well, and we have to recheck the state after socket locked.
  1146. */
  1147. st = sk->sk_state;
  1148. switch (st) {
  1149. case TCP_CLOSE:
  1150. /* This is ok... continue with connect */
  1151. break;
  1152. case TCP_ESTABLISHED:
  1153. /* Socket is already connected */
  1154. err = -EISCONN;
  1155. goto out_unlock;
  1156. default:
  1157. err = -EINVAL;
  1158. goto out_unlock;
  1159. }
  1160. unix_state_lock_nested(sk);
  1161. if (sk->sk_state != st) {
  1162. unix_state_unlock(sk);
  1163. unix_state_unlock(other);
  1164. sock_put(other);
  1165. goto restart;
  1166. }
  1167. err = security_unix_stream_connect(sk, other, newsk);
  1168. if (err) {
  1169. unix_state_unlock(sk);
  1170. goto out_unlock;
  1171. }
  1172. /* The way is open! Fastly set all the necessary fields... */
  1173. sock_hold(sk);
  1174. unix_peer(newsk) = sk;
  1175. newsk->sk_state = TCP_ESTABLISHED;
  1176. newsk->sk_type = sk->sk_type;
  1177. init_peercred(newsk);
  1178. newu = unix_sk(newsk);
  1179. RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
  1180. otheru = unix_sk(other);
  1181. /* copy address information from listening to new sock
  1182. *
  1183. * The contents of *(otheru->addr) and otheru->path
  1184. * are seen fully set up here, since we have found
  1185. * otheru in hash under unix_table_lock. Insertion
  1186. * into the hash chain we'd found it in had been done
  1187. * in an earlier critical area protected by unix_table_lock,
  1188. * the same one where we'd set *(otheru->addr) contents,
  1189. * as well as otheru->path and otheru->addr itself.
  1190. *
  1191. * Using smp_store_release() here to set newu->addr
  1192. * is enough to make those stores, as well as stores
  1193. * to newu->path visible to anyone who gets newu->addr
  1194. * by smp_load_acquire(). IOW, the same warranties
  1195. * as for unix_sock instances bound in unix_bind() or
  1196. * in unix_autobind().
  1197. */
  1198. if (otheru->path.dentry) {
  1199. path_get(&otheru->path);
  1200. newu->path = otheru->path;
  1201. }
  1202. refcount_inc(&otheru->addr->refcnt);
  1203. smp_store_release(&newu->addr, otheru->addr);
  1204. /* Set credentials */
  1205. copy_peercred(sk, other);
  1206. sock->state = SS_CONNECTED;
  1207. sk->sk_state = TCP_ESTABLISHED;
  1208. sock_hold(newsk);
  1209. smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
  1210. unix_peer(sk) = newsk;
  1211. unix_state_unlock(sk);
  1212. /* take ten and and send info to listening sock */
  1213. spin_lock(&other->sk_receive_queue.lock);
  1214. __skb_queue_tail(&other->sk_receive_queue, skb);
  1215. spin_unlock(&other->sk_receive_queue.lock);
  1216. unix_state_unlock(other);
  1217. other->sk_data_ready(other);
  1218. sock_put(other);
  1219. return 0;
  1220. out_unlock:
  1221. if (other)
  1222. unix_state_unlock(other);
  1223. out:
  1224. kfree_skb(skb);
  1225. if (newsk)
  1226. unix_release_sock(newsk, 0);
  1227. if (other)
  1228. sock_put(other);
  1229. return err;
  1230. }
  1231. static int unix_socketpair(struct socket *socka, struct socket *sockb)
  1232. {
  1233. struct sock *ska = socka->sk, *skb = sockb->sk;
  1234. /* Join our sockets back to back */
  1235. sock_hold(ska);
  1236. sock_hold(skb);
  1237. unix_peer(ska) = skb;
  1238. unix_peer(skb) = ska;
  1239. init_peercred(ska);
  1240. init_peercred(skb);
  1241. if (ska->sk_type != SOCK_DGRAM) {
  1242. ska->sk_state = TCP_ESTABLISHED;
  1243. skb->sk_state = TCP_ESTABLISHED;
  1244. socka->state = SS_CONNECTED;
  1245. sockb->state = SS_CONNECTED;
  1246. }
  1247. return 0;
  1248. }
  1249. static void unix_sock_inherit_flags(const struct socket *old,
  1250. struct socket *new)
  1251. {
  1252. if (test_bit(SOCK_PASSCRED, &old->flags))
  1253. set_bit(SOCK_PASSCRED, &new->flags);
  1254. if (test_bit(SOCK_PASSSEC, &old->flags))
  1255. set_bit(SOCK_PASSSEC, &new->flags);
  1256. }
  1257. static int unix_accept(struct socket *sock, struct socket *newsock, int flags,
  1258. bool kern)
  1259. {
  1260. struct sock *sk = sock->sk;
  1261. struct sock *tsk;
  1262. struct sk_buff *skb;
  1263. int err;
  1264. err = -EOPNOTSUPP;
  1265. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  1266. goto out;
  1267. err = -EINVAL;
  1268. if (sk->sk_state != TCP_LISTEN)
  1269. goto out;
  1270. /* If socket state is TCP_LISTEN it cannot change (for now...),
  1271. * so that no locks are necessary.
  1272. */
  1273. skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
  1274. if (!skb) {
  1275. /* This means receive shutdown. */
  1276. if (err == 0)
  1277. err = -EINVAL;
  1278. goto out;
  1279. }
  1280. tsk = skb->sk;
  1281. skb_free_datagram(sk, skb);
  1282. wake_up_interruptible(&unix_sk(sk)->peer_wait);
  1283. /* attach accepted sock to socket */
  1284. unix_state_lock(tsk);
  1285. newsock->state = SS_CONNECTED;
  1286. unix_sock_inherit_flags(sock, newsock);
  1287. sock_graft(tsk, newsock);
  1288. unix_state_unlock(tsk);
  1289. return 0;
  1290. out:
  1291. return err;
  1292. }
  1293. static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
  1294. {
  1295. struct sock *sk = sock->sk;
  1296. struct unix_address *addr;
  1297. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr);
  1298. int err = 0;
  1299. if (peer) {
  1300. sk = unix_peer_get(sk);
  1301. err = -ENOTCONN;
  1302. if (!sk)
  1303. goto out;
  1304. err = 0;
  1305. } else {
  1306. sock_hold(sk);
  1307. }
  1308. addr = smp_load_acquire(&unix_sk(sk)->addr);
  1309. if (!addr) {
  1310. sunaddr->sun_family = AF_UNIX;
  1311. sunaddr->sun_path[0] = 0;
  1312. err = sizeof(short);
  1313. } else {
  1314. err = addr->len;
  1315. memcpy(sunaddr, addr->name, addr->len);
  1316. }
  1317. sock_put(sk);
  1318. out:
  1319. return err;
  1320. }
  1321. static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1322. {
  1323. scm->fp = scm_fp_dup(UNIXCB(skb).fp);
  1324. /*
  1325. * Garbage collection of unix sockets starts by selecting a set of
  1326. * candidate sockets which have reference only from being in flight
  1327. * (total_refs == inflight_refs). This condition is checked once during
  1328. * the candidate collection phase, and candidates are marked as such, so
  1329. * that non-candidates can later be ignored. While inflight_refs is
  1330. * protected by unix_gc_lock, total_refs (file count) is not, hence this
  1331. * is an instantaneous decision.
  1332. *
  1333. * Once a candidate, however, the socket must not be reinstalled into a
  1334. * file descriptor while the garbage collection is in progress.
  1335. *
  1336. * If the above conditions are met, then the directed graph of
  1337. * candidates (*) does not change while unix_gc_lock is held.
  1338. *
  1339. * Any operations that changes the file count through file descriptors
  1340. * (dup, close, sendmsg) does not change the graph since candidates are
  1341. * not installed in fds.
  1342. *
  1343. * Dequeing a candidate via recvmsg would install it into an fd, but
  1344. * that takes unix_gc_lock to decrement the inflight count, so it's
  1345. * serialized with garbage collection.
  1346. *
  1347. * MSG_PEEK is special in that it does not change the inflight count,
  1348. * yet does install the socket into an fd. The following lock/unlock
  1349. * pair is to ensure serialization with garbage collection. It must be
  1350. * done between incrementing the file count and installing the file into
  1351. * an fd.
  1352. *
  1353. * If garbage collection starts after the barrier provided by the
  1354. * lock/unlock, then it will see the elevated refcount and not mark this
  1355. * as a candidate. If a garbage collection is already in progress
  1356. * before the file count was incremented, then the lock/unlock pair will
  1357. * ensure that garbage collection is finished before progressing to
  1358. * installing the fd.
  1359. *
  1360. * (*) A -> B where B is on the queue of A or B is on the queue of C
  1361. * which is on the queue of listening socket A.
  1362. */
  1363. spin_lock(&unix_gc_lock);
  1364. spin_unlock(&unix_gc_lock);
  1365. }
  1366. static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
  1367. {
  1368. int err = 0;
  1369. UNIXCB(skb).pid = get_pid(scm->pid);
  1370. UNIXCB(skb).uid = scm->creds.uid;
  1371. UNIXCB(skb).gid = scm->creds.gid;
  1372. UNIXCB(skb).fp = NULL;
  1373. unix_get_secdata(scm, skb);
  1374. if (scm->fp && send_fds)
  1375. err = unix_attach_fds(scm, skb);
  1376. skb->destructor = unix_destruct_scm;
  1377. return err;
  1378. }
  1379. static bool unix_passcred_enabled(const struct socket *sock,
  1380. const struct sock *other)
  1381. {
  1382. return test_bit(SOCK_PASSCRED, &sock->flags) ||
  1383. !other->sk_socket ||
  1384. test_bit(SOCK_PASSCRED, &other->sk_socket->flags);
  1385. }
  1386. /*
  1387. * Some apps rely on write() giving SCM_CREDENTIALS
  1388. * We include credentials if source or destination socket
  1389. * asserted SOCK_PASSCRED.
  1390. */
  1391. static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
  1392. const struct sock *other)
  1393. {
  1394. if (UNIXCB(skb).pid)
  1395. return;
  1396. if (unix_passcred_enabled(sock, other)) {
  1397. UNIXCB(skb).pid = get_pid(task_tgid(current));
  1398. current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
  1399. }
  1400. }
  1401. static int maybe_init_creds(struct scm_cookie *scm,
  1402. struct socket *socket,
  1403. const struct sock *other)
  1404. {
  1405. int err;
  1406. struct msghdr msg = { .msg_controllen = 0 };
  1407. err = scm_send(socket, &msg, scm, false);
  1408. if (err)
  1409. return err;
  1410. if (unix_passcred_enabled(socket, other)) {
  1411. scm->pid = get_pid(task_tgid(current));
  1412. current_uid_gid(&scm->creds.uid, &scm->creds.gid);
  1413. }
  1414. return err;
  1415. }
  1416. static bool unix_skb_scm_eq(struct sk_buff *skb,
  1417. struct scm_cookie *scm)
  1418. {
  1419. const struct unix_skb_parms *u = &UNIXCB(skb);
  1420. return u->pid == scm->pid &&
  1421. uid_eq(u->uid, scm->creds.uid) &&
  1422. gid_eq(u->gid, scm->creds.gid) &&
  1423. unix_secdata_eq(scm, skb);
  1424. }
  1425. static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
  1426. {
  1427. struct scm_fp_list *fp = UNIXCB(skb).fp;
  1428. struct unix_sock *u = unix_sk(sk);
  1429. if (unlikely(fp && fp->count))
  1430. atomic_add(fp->count, &u->scm_stat.nr_fds);
  1431. }
  1432. static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
  1433. {
  1434. struct scm_fp_list *fp = UNIXCB(skb).fp;
  1435. struct unix_sock *u = unix_sk(sk);
  1436. if (unlikely(fp && fp->count))
  1437. atomic_sub(fp->count, &u->scm_stat.nr_fds);
  1438. }
  1439. /*
  1440. * Send AF_UNIX data.
  1441. */
  1442. static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
  1443. size_t len)
  1444. {
  1445. struct sock *sk = sock->sk;
  1446. struct net *net = sock_net(sk);
  1447. struct unix_sock *u = unix_sk(sk);
  1448. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
  1449. struct sock *other = NULL;
  1450. int namelen = 0; /* fake GCC */
  1451. int err;
  1452. unsigned int hash;
  1453. struct sk_buff *skb;
  1454. long timeo;
  1455. struct scm_cookie scm;
  1456. int data_len = 0;
  1457. int sk_locked;
  1458. wait_for_unix_gc();
  1459. err = scm_send(sock, msg, &scm, false);
  1460. if (err < 0)
  1461. return err;
  1462. err = -EOPNOTSUPP;
  1463. if (msg->msg_flags&MSG_OOB)
  1464. goto out;
  1465. if (msg->msg_namelen) {
  1466. err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
  1467. if (err < 0)
  1468. goto out;
  1469. namelen = err;
  1470. } else {
  1471. sunaddr = NULL;
  1472. err = -ENOTCONN;
  1473. other = unix_peer_get(sk);
  1474. if (!other)
  1475. goto out;
  1476. }
  1477. if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr
  1478. && (err = unix_autobind(sock)) != 0)
  1479. goto out;
  1480. err = -EMSGSIZE;
  1481. if (len > sk->sk_sndbuf - 32)
  1482. goto out;
  1483. if (len > SKB_MAX_ALLOC) {
  1484. data_len = min_t(size_t,
  1485. len - SKB_MAX_ALLOC,
  1486. MAX_SKB_FRAGS * PAGE_SIZE);
  1487. data_len = PAGE_ALIGN(data_len);
  1488. BUILD_BUG_ON(SKB_MAX_ALLOC < PAGE_SIZE);
  1489. }
  1490. skb = sock_alloc_send_pskb(sk, len - data_len, data_len,
  1491. msg->msg_flags & MSG_DONTWAIT, &err,
  1492. PAGE_ALLOC_COSTLY_ORDER);
  1493. if (skb == NULL)
  1494. goto out;
  1495. err = unix_scm_to_skb(&scm, skb, true);
  1496. if (err < 0)
  1497. goto out_free;
  1498. skb_put(skb, len - data_len);
  1499. skb->data_len = data_len;
  1500. skb->len = len;
  1501. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
  1502. if (err)
  1503. goto out_free;
  1504. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1505. restart:
  1506. if (!other) {
  1507. err = -ECONNRESET;
  1508. if (sunaddr == NULL)
  1509. goto out_free;
  1510. other = unix_find_other(net, sunaddr, namelen, sk->sk_type,
  1511. hash, &err);
  1512. if (other == NULL)
  1513. goto out_free;
  1514. }
  1515. if (sk_filter(other, skb) < 0) {
  1516. /* Toss the packet but do not return any error to the sender */
  1517. err = len;
  1518. goto out_free;
  1519. }
  1520. sk_locked = 0;
  1521. unix_state_lock(other);
  1522. restart_locked:
  1523. err = -EPERM;
  1524. if (!unix_may_send(sk, other))
  1525. goto out_unlock;
  1526. if (unlikely(sock_flag(other, SOCK_DEAD))) {
  1527. /*
  1528. * Check with 1003.1g - what should
  1529. * datagram error
  1530. */
  1531. unix_state_unlock(other);
  1532. sock_put(other);
  1533. if (!sk_locked)
  1534. unix_state_lock(sk);
  1535. err = 0;
  1536. if (unix_peer(sk) == other) {
  1537. unix_peer(sk) = NULL;
  1538. unix_dgram_peer_wake_disconnect_wakeup(sk, other);
  1539. unix_state_unlock(sk);
  1540. unix_dgram_disconnected(sk, other);
  1541. sock_put(other);
  1542. err = -ECONNREFUSED;
  1543. } else {
  1544. unix_state_unlock(sk);
  1545. }
  1546. other = NULL;
  1547. if (err)
  1548. goto out_free;
  1549. goto restart;
  1550. }
  1551. err = -EPIPE;
  1552. if (other->sk_shutdown & RCV_SHUTDOWN)
  1553. goto out_unlock;
  1554. if (sk->sk_type != SOCK_SEQPACKET) {
  1555. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  1556. if (err)
  1557. goto out_unlock;
  1558. }
  1559. /* other == sk && unix_peer(other) != sk if
  1560. * - unix_peer(sk) == NULL, destination address bound to sk
  1561. * - unix_peer(sk) == sk by time of get but disconnected before lock
  1562. */
  1563. if (other != sk &&
  1564. unlikely(unix_peer(other) != sk &&
  1565. unix_recvq_full_lockless(other))) {
  1566. if (timeo) {
  1567. timeo = unix_wait_for_peer(other, timeo);
  1568. err = sock_intr_errno(timeo);
  1569. if (signal_pending(current))
  1570. goto out_free;
  1571. goto restart;
  1572. }
  1573. if (!sk_locked) {
  1574. unix_state_unlock(other);
  1575. unix_state_double_lock(sk, other);
  1576. }
  1577. if (unix_peer(sk) != other ||
  1578. unix_dgram_peer_wake_me(sk, other)) {
  1579. err = -EAGAIN;
  1580. sk_locked = 1;
  1581. goto out_unlock;
  1582. }
  1583. if (!sk_locked) {
  1584. sk_locked = 1;
  1585. goto restart_locked;
  1586. }
  1587. }
  1588. if (unlikely(sk_locked))
  1589. unix_state_unlock(sk);
  1590. if (sock_flag(other, SOCK_RCVTSTAMP))
  1591. __net_timestamp(skb);
  1592. maybe_add_creds(skb, sock, other);
  1593. scm_stat_add(other, skb);
  1594. skb_queue_tail(&other->sk_receive_queue, skb);
  1595. unix_state_unlock(other);
  1596. other->sk_data_ready(other);
  1597. sock_put(other);
  1598. scm_destroy(&scm);
  1599. return len;
  1600. out_unlock:
  1601. if (sk_locked)
  1602. unix_state_unlock(sk);
  1603. unix_state_unlock(other);
  1604. out_free:
  1605. kfree_skb(skb);
  1606. out:
  1607. if (other)
  1608. sock_put(other);
  1609. scm_destroy(&scm);
  1610. return err;
  1611. }
  1612. /* We use paged skbs for stream sockets, and limit occupancy to 32768
  1613. * bytes, and a minimum of a full page.
  1614. */
  1615. #define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768))
  1616. static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
  1617. size_t len)
  1618. {
  1619. struct sock *sk = sock->sk;
  1620. struct sock *other = NULL;
  1621. int err, size;
  1622. struct sk_buff *skb;
  1623. int sent = 0;
  1624. struct scm_cookie scm;
  1625. bool fds_sent = false;
  1626. int data_len;
  1627. wait_for_unix_gc();
  1628. err = scm_send(sock, msg, &scm, false);
  1629. if (err < 0)
  1630. return err;
  1631. err = -EOPNOTSUPP;
  1632. if (msg->msg_flags&MSG_OOB)
  1633. goto out_err;
  1634. if (msg->msg_namelen) {
  1635. err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
  1636. goto out_err;
  1637. } else {
  1638. err = -ENOTCONN;
  1639. other = unix_peer(sk);
  1640. if (!other)
  1641. goto out_err;
  1642. }
  1643. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1644. goto pipe_err;
  1645. while (sent < len) {
  1646. size = len - sent;
  1647. /* Keep two messages in the pipe so it schedules better */
  1648. size = min_t(int, size, (sk->sk_sndbuf >> 1) - 64);
  1649. /* allow fallback to order-0 allocations */
  1650. size = min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ);
  1651. data_len = max_t(int, 0, size - SKB_MAX_HEAD(0));
  1652. data_len = min_t(size_t, size, PAGE_ALIGN(data_len));
  1653. skb = sock_alloc_send_pskb(sk, size - data_len, data_len,
  1654. msg->msg_flags & MSG_DONTWAIT, &err,
  1655. get_order(UNIX_SKB_FRAGS_SZ));
  1656. if (!skb)
  1657. goto out_err;
  1658. /* Only send the fds in the first buffer */
  1659. err = unix_scm_to_skb(&scm, skb, !fds_sent);
  1660. if (err < 0) {
  1661. kfree_skb(skb);
  1662. goto out_err;
  1663. }
  1664. fds_sent = true;
  1665. skb_put(skb, size - data_len);
  1666. skb->data_len = data_len;
  1667. skb->len = size;
  1668. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
  1669. if (err) {
  1670. kfree_skb(skb);
  1671. goto out_err;
  1672. }
  1673. unix_state_lock(other);
  1674. if (sock_flag(other, SOCK_DEAD) ||
  1675. (other->sk_shutdown & RCV_SHUTDOWN))
  1676. goto pipe_err_free;
  1677. maybe_add_creds(skb, sock, other);
  1678. scm_stat_add(other, skb);
  1679. skb_queue_tail(&other->sk_receive_queue, skb);
  1680. unix_state_unlock(other);
  1681. other->sk_data_ready(other);
  1682. sent += size;
  1683. }
  1684. scm_destroy(&scm);
  1685. return sent;
  1686. pipe_err_free:
  1687. unix_state_unlock(other);
  1688. kfree_skb(skb);
  1689. pipe_err:
  1690. if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
  1691. send_sig(SIGPIPE, current, 0);
  1692. err = -EPIPE;
  1693. out_err:
  1694. scm_destroy(&scm);
  1695. return sent ? : err;
  1696. }
  1697. static ssize_t unix_stream_sendpage(struct socket *socket, struct page *page,
  1698. int offset, size_t size, int flags)
  1699. {
  1700. int err;
  1701. bool send_sigpipe = false;
  1702. bool init_scm = true;
  1703. struct scm_cookie scm;
  1704. struct sock *other, *sk = socket->sk;
  1705. struct sk_buff *skb, *newskb = NULL, *tail = NULL;
  1706. if (flags & MSG_OOB)
  1707. return -EOPNOTSUPP;
  1708. other = unix_peer(sk);
  1709. if (!other || sk->sk_state != TCP_ESTABLISHED)
  1710. return -ENOTCONN;
  1711. if (false) {
  1712. alloc_skb:
  1713. unix_state_unlock(other);
  1714. mutex_unlock(&unix_sk(other)->iolock);
  1715. newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
  1716. &err, 0);
  1717. if (!newskb)
  1718. goto err;
  1719. }
  1720. /* we must acquire iolock as we modify already present
  1721. * skbs in the sk_receive_queue and mess with skb->len
  1722. */
  1723. err = mutex_lock_interruptible(&unix_sk(other)->iolock);
  1724. if (err) {
  1725. err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS;
  1726. goto err;
  1727. }
  1728. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  1729. err = -EPIPE;
  1730. send_sigpipe = true;
  1731. goto err_unlock;
  1732. }
  1733. unix_state_lock(other);
  1734. if (sock_flag(other, SOCK_DEAD) ||
  1735. other->sk_shutdown & RCV_SHUTDOWN) {
  1736. err = -EPIPE;
  1737. send_sigpipe = true;
  1738. goto err_state_unlock;
  1739. }
  1740. if (init_scm) {
  1741. err = maybe_init_creds(&scm, socket, other);
  1742. if (err)
  1743. goto err_state_unlock;
  1744. init_scm = false;
  1745. }
  1746. skb = skb_peek_tail(&other->sk_receive_queue);
  1747. if (tail && tail == skb) {
  1748. skb = newskb;
  1749. } else if (!skb || !unix_skb_scm_eq(skb, &scm)) {
  1750. if (newskb) {
  1751. skb = newskb;
  1752. } else {
  1753. tail = skb;
  1754. goto alloc_skb;
  1755. }
  1756. } else if (newskb) {
  1757. /* this is fast path, we don't necessarily need to
  1758. * call to kfree_skb even though with newskb == NULL
  1759. * this - does no harm
  1760. */
  1761. consume_skb(newskb);
  1762. newskb = NULL;
  1763. }
  1764. if (skb_append_pagefrags(skb, page, offset, size)) {
  1765. tail = skb;
  1766. goto alloc_skb;
  1767. }
  1768. skb->len += size;
  1769. skb->data_len += size;
  1770. skb->truesize += size;
  1771. refcount_add(size, &sk->sk_wmem_alloc);
  1772. if (newskb) {
  1773. err = unix_scm_to_skb(&scm, skb, false);
  1774. if (err)
  1775. goto err_state_unlock;
  1776. spin_lock(&other->sk_receive_queue.lock);
  1777. __skb_queue_tail(&other->sk_receive_queue, newskb);
  1778. spin_unlock(&other->sk_receive_queue.lock);
  1779. }
  1780. unix_state_unlock(other);
  1781. mutex_unlock(&unix_sk(other)->iolock);
  1782. other->sk_data_ready(other);
  1783. scm_destroy(&scm);
  1784. return size;
  1785. err_state_unlock:
  1786. unix_state_unlock(other);
  1787. err_unlock:
  1788. mutex_unlock(&unix_sk(other)->iolock);
  1789. err:
  1790. kfree_skb(newskb);
  1791. if (send_sigpipe && !(flags & MSG_NOSIGNAL))
  1792. send_sig(SIGPIPE, current, 0);
  1793. if (!init_scm)
  1794. scm_destroy(&scm);
  1795. return err;
  1796. }
  1797. static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
  1798. size_t len)
  1799. {
  1800. int err;
  1801. struct sock *sk = sock->sk;
  1802. err = sock_error(sk);
  1803. if (err)
  1804. return err;
  1805. if (sk->sk_state != TCP_ESTABLISHED)
  1806. return -ENOTCONN;
  1807. if (msg->msg_namelen)
  1808. msg->msg_namelen = 0;
  1809. return unix_dgram_sendmsg(sock, msg, len);
  1810. }
  1811. static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
  1812. size_t size, int flags)
  1813. {
  1814. struct sock *sk = sock->sk;
  1815. if (sk->sk_state != TCP_ESTABLISHED)
  1816. return -ENOTCONN;
  1817. return unix_dgram_recvmsg(sock, msg, size, flags);
  1818. }
  1819. static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
  1820. {
  1821. struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr);
  1822. if (addr) {
  1823. msg->msg_namelen = addr->len;
  1824. memcpy(msg->msg_name, addr->name, addr->len);
  1825. }
  1826. }
  1827. static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  1828. size_t size, int flags)
  1829. {
  1830. struct scm_cookie scm;
  1831. struct sock *sk = sock->sk;
  1832. struct unix_sock *u = unix_sk(sk);
  1833. struct sk_buff *skb, *last;
  1834. long timeo;
  1835. int skip;
  1836. int err;
  1837. err = -EOPNOTSUPP;
  1838. if (flags&MSG_OOB)
  1839. goto out;
  1840. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1841. do {
  1842. mutex_lock(&u->iolock);
  1843. skip = sk_peek_offset(sk, flags);
  1844. skb = __skb_try_recv_datagram(sk, &sk->sk_receive_queue, flags,
  1845. &skip, &err, &last);
  1846. if (skb) {
  1847. if (!(flags & MSG_PEEK))
  1848. scm_stat_del(sk, skb);
  1849. break;
  1850. }
  1851. mutex_unlock(&u->iolock);
  1852. if (err != -EAGAIN)
  1853. break;
  1854. } while (timeo &&
  1855. !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue,
  1856. &err, &timeo, last));
  1857. if (!skb) { /* implies iolock unlocked */
  1858. unix_state_lock(sk);
  1859. /* Signal EOF on disconnected non-blocking SEQPACKET socket. */
  1860. if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
  1861. (sk->sk_shutdown & RCV_SHUTDOWN))
  1862. err = 0;
  1863. unix_state_unlock(sk);
  1864. goto out;
  1865. }
  1866. if (wq_has_sleeper(&u->peer_wait))
  1867. wake_up_interruptible_sync_poll(&u->peer_wait,
  1868. EPOLLOUT | EPOLLWRNORM |
  1869. EPOLLWRBAND);
  1870. if (msg->msg_name)
  1871. unix_copy_addr(msg, skb->sk);
  1872. if (size > skb->len - skip)
  1873. size = skb->len - skip;
  1874. else if (size < skb->len - skip)
  1875. msg->msg_flags |= MSG_TRUNC;
  1876. err = skb_copy_datagram_msg(skb, skip, msg, size);
  1877. if (err)
  1878. goto out_free;
  1879. if (sock_flag(sk, SOCK_RCVTSTAMP))
  1880. __sock_recv_timestamp(msg, sk, skb);
  1881. memset(&scm, 0, sizeof(scm));
  1882. scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
  1883. unix_set_secdata(&scm, skb);
  1884. if (!(flags & MSG_PEEK)) {
  1885. if (UNIXCB(skb).fp)
  1886. unix_detach_fds(&scm, skb);
  1887. sk_peek_offset_bwd(sk, skb->len);
  1888. } else {
  1889. /* It is questionable: on PEEK we could:
  1890. - do not return fds - good, but too simple 8)
  1891. - return fds, and do not return them on read (old strategy,
  1892. apparently wrong)
  1893. - clone fds (I chose it for now, it is the most universal
  1894. solution)
  1895. POSIX 1003.1g does not actually define this clearly
  1896. at all. POSIX 1003.1g doesn't define a lot of things
  1897. clearly however!
  1898. */
  1899. sk_peek_offset_fwd(sk, size);
  1900. if (UNIXCB(skb).fp)
  1901. unix_peek_fds(&scm, skb);
  1902. }
  1903. err = (flags & MSG_TRUNC) ? skb->len - skip : size;
  1904. scm_recv(sock, msg, &scm, flags);
  1905. out_free:
  1906. skb_free_datagram(sk, skb);
  1907. mutex_unlock(&u->iolock);
  1908. out:
  1909. return err;
  1910. }
  1911. /*
  1912. * Sleep until more data has arrived. But check for races..
  1913. */
  1914. static long unix_stream_data_wait(struct sock *sk, long timeo,
  1915. struct sk_buff *last, unsigned int last_len,
  1916. bool freezable)
  1917. {
  1918. struct sk_buff *tail;
  1919. DEFINE_WAIT(wait);
  1920. unix_state_lock(sk);
  1921. for (;;) {
  1922. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1923. tail = skb_peek_tail(&sk->sk_receive_queue);
  1924. if (tail != last ||
  1925. (tail && tail->len != last_len) ||
  1926. sk->sk_err ||
  1927. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1928. signal_pending(current) ||
  1929. !timeo)
  1930. break;
  1931. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1932. unix_state_unlock(sk);
  1933. if (freezable)
  1934. timeo = freezable_schedule_timeout(timeo);
  1935. else
  1936. timeo = schedule_timeout(timeo);
  1937. unix_state_lock(sk);
  1938. if (sock_flag(sk, SOCK_DEAD))
  1939. break;
  1940. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1941. }
  1942. finish_wait(sk_sleep(sk), &wait);
  1943. unix_state_unlock(sk);
  1944. return timeo;
  1945. }
  1946. static unsigned int unix_skb_len(const struct sk_buff *skb)
  1947. {
  1948. return skb->len - UNIXCB(skb).consumed;
  1949. }
  1950. struct unix_stream_read_state {
  1951. int (*recv_actor)(struct sk_buff *, int, int,
  1952. struct unix_stream_read_state *);
  1953. struct socket *socket;
  1954. struct msghdr *msg;
  1955. struct pipe_inode_info *pipe;
  1956. size_t size;
  1957. int flags;
  1958. unsigned int splice_flags;
  1959. };
  1960. static int unix_stream_read_generic(struct unix_stream_read_state *state,
  1961. bool freezable)
  1962. {
  1963. struct scm_cookie scm;
  1964. struct socket *sock = state->socket;
  1965. struct sock *sk = sock->sk;
  1966. struct unix_sock *u = unix_sk(sk);
  1967. int copied = 0;
  1968. int flags = state->flags;
  1969. int noblock = flags & MSG_DONTWAIT;
  1970. bool check_creds = false;
  1971. int target;
  1972. int err = 0;
  1973. long timeo;
  1974. int skip;
  1975. size_t size = state->size;
  1976. unsigned int last_len;
  1977. if (unlikely(sk->sk_state != TCP_ESTABLISHED)) {
  1978. err = -EINVAL;
  1979. goto out;
  1980. }
  1981. if (unlikely(flags & MSG_OOB)) {
  1982. err = -EOPNOTSUPP;
  1983. goto out;
  1984. }
  1985. target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
  1986. timeo = sock_rcvtimeo(sk, noblock);
  1987. memset(&scm, 0, sizeof(scm));
  1988. /* Lock the socket to prevent queue disordering
  1989. * while sleeps in memcpy_tomsg
  1990. */
  1991. mutex_lock(&u->iolock);
  1992. skip = max(sk_peek_offset(sk, flags), 0);
  1993. do {
  1994. int chunk;
  1995. bool drop_skb;
  1996. struct sk_buff *skb, *last;
  1997. redo:
  1998. unix_state_lock(sk);
  1999. if (sock_flag(sk, SOCK_DEAD)) {
  2000. err = -ECONNRESET;
  2001. goto unlock;
  2002. }
  2003. last = skb = skb_peek(&sk->sk_receive_queue);
  2004. last_len = last ? last->len : 0;
  2005. again:
  2006. if (skb == NULL) {
  2007. if (copied >= target)
  2008. goto unlock;
  2009. /*
  2010. * POSIX 1003.1g mandates this order.
  2011. */
  2012. err = sock_error(sk);
  2013. if (err)
  2014. goto unlock;
  2015. if (sk->sk_shutdown & RCV_SHUTDOWN)
  2016. goto unlock;
  2017. unix_state_unlock(sk);
  2018. if (!timeo) {
  2019. err = -EAGAIN;
  2020. break;
  2021. }
  2022. mutex_unlock(&u->iolock);
  2023. timeo = unix_stream_data_wait(sk, timeo, last,
  2024. last_len, freezable);
  2025. if (signal_pending(current)) {
  2026. err = sock_intr_errno(timeo);
  2027. scm_destroy(&scm);
  2028. goto out;
  2029. }
  2030. mutex_lock(&u->iolock);
  2031. goto redo;
  2032. unlock:
  2033. unix_state_unlock(sk);
  2034. break;
  2035. }
  2036. while (skip >= unix_skb_len(skb)) {
  2037. skip -= unix_skb_len(skb);
  2038. last = skb;
  2039. last_len = skb->len;
  2040. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2041. if (!skb)
  2042. goto again;
  2043. }
  2044. unix_state_unlock(sk);
  2045. if (check_creds) {
  2046. /* Never glue messages from different writers */
  2047. if (!unix_skb_scm_eq(skb, &scm))
  2048. break;
  2049. } else if (test_bit(SOCK_PASSCRED, &sock->flags)) {
  2050. /* Copy credentials */
  2051. scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
  2052. unix_set_secdata(&scm, skb);
  2053. check_creds = true;
  2054. }
  2055. /* Copy address just once */
  2056. if (state->msg && state->msg->msg_name) {
  2057. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr,
  2058. state->msg->msg_name);
  2059. unix_copy_addr(state->msg, skb->sk);
  2060. sunaddr = NULL;
  2061. }
  2062. chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size);
  2063. skb_get(skb);
  2064. chunk = state->recv_actor(skb, skip, chunk, state);
  2065. drop_skb = !unix_skb_len(skb);
  2066. /* skb is only safe to use if !drop_skb */
  2067. consume_skb(skb);
  2068. if (chunk < 0) {
  2069. if (copied == 0)
  2070. copied = -EFAULT;
  2071. break;
  2072. }
  2073. copied += chunk;
  2074. size -= chunk;
  2075. if (drop_skb) {
  2076. /* the skb was touched by a concurrent reader;
  2077. * we should not expect anything from this skb
  2078. * anymore and assume it invalid - we can be
  2079. * sure it was dropped from the socket queue
  2080. *
  2081. * let's report a short read
  2082. */
  2083. err = 0;
  2084. break;
  2085. }
  2086. /* Mark read part of skb as used */
  2087. if (!(flags & MSG_PEEK)) {
  2088. UNIXCB(skb).consumed += chunk;
  2089. sk_peek_offset_bwd(sk, chunk);
  2090. if (UNIXCB(skb).fp) {
  2091. scm_stat_del(sk, skb);
  2092. unix_detach_fds(&scm, skb);
  2093. }
  2094. if (unix_skb_len(skb))
  2095. break;
  2096. skb_unlink(skb, &sk->sk_receive_queue);
  2097. consume_skb(skb);
  2098. if (scm.fp)
  2099. break;
  2100. } else {
  2101. /* It is questionable, see note in unix_dgram_recvmsg.
  2102. */
  2103. if (UNIXCB(skb).fp)
  2104. unix_peek_fds(&scm, skb);
  2105. sk_peek_offset_fwd(sk, chunk);
  2106. if (UNIXCB(skb).fp)
  2107. break;
  2108. skip = 0;
  2109. last = skb;
  2110. last_len = skb->len;
  2111. unix_state_lock(sk);
  2112. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2113. if (skb)
  2114. goto again;
  2115. unix_state_unlock(sk);
  2116. break;
  2117. }
  2118. } while (size);
  2119. mutex_unlock(&u->iolock);
  2120. if (state->msg)
  2121. scm_recv(sock, state->msg, &scm, flags);
  2122. else
  2123. scm_destroy(&scm);
  2124. out:
  2125. return copied ? : err;
  2126. }
  2127. static int unix_stream_read_actor(struct sk_buff *skb,
  2128. int skip, int chunk,
  2129. struct unix_stream_read_state *state)
  2130. {
  2131. int ret;
  2132. ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,
  2133. state->msg, chunk);
  2134. return ret ?: chunk;
  2135. }
  2136. static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
  2137. size_t size, int flags)
  2138. {
  2139. struct unix_stream_read_state state = {
  2140. .recv_actor = unix_stream_read_actor,
  2141. .socket = sock,
  2142. .msg = msg,
  2143. .size = size,
  2144. .flags = flags
  2145. };
  2146. return unix_stream_read_generic(&state, true);
  2147. }
  2148. static int unix_stream_splice_actor(struct sk_buff *skb,
  2149. int skip, int chunk,
  2150. struct unix_stream_read_state *state)
  2151. {
  2152. return skb_splice_bits(skb, state->socket->sk,
  2153. UNIXCB(skb).consumed + skip,
  2154. state->pipe, chunk, state->splice_flags);
  2155. }
  2156. static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos,
  2157. struct pipe_inode_info *pipe,
  2158. size_t size, unsigned int flags)
  2159. {
  2160. struct unix_stream_read_state state = {
  2161. .recv_actor = unix_stream_splice_actor,
  2162. .socket = sock,
  2163. .pipe = pipe,
  2164. .size = size,
  2165. .splice_flags = flags,
  2166. };
  2167. if (unlikely(*ppos))
  2168. return -ESPIPE;
  2169. if (sock->file->f_flags & O_NONBLOCK ||
  2170. flags & SPLICE_F_NONBLOCK)
  2171. state.flags = MSG_DONTWAIT;
  2172. return unix_stream_read_generic(&state, false);
  2173. }
  2174. static int unix_shutdown(struct socket *sock, int mode)
  2175. {
  2176. struct sock *sk = sock->sk;
  2177. struct sock *other;
  2178. if (mode < SHUT_RD || mode > SHUT_RDWR)
  2179. return -EINVAL;
  2180. /* This maps:
  2181. * SHUT_RD (0) -> RCV_SHUTDOWN (1)
  2182. * SHUT_WR (1) -> SEND_SHUTDOWN (2)
  2183. * SHUT_RDWR (2) -> SHUTDOWN_MASK (3)
  2184. */
  2185. ++mode;
  2186. unix_state_lock(sk);
  2187. sk->sk_shutdown |= mode;
  2188. other = unix_peer(sk);
  2189. if (other)
  2190. sock_hold(other);
  2191. unix_state_unlock(sk);
  2192. sk->sk_state_change(sk);
  2193. if (other &&
  2194. (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
  2195. int peer_mode = 0;
  2196. if (mode&RCV_SHUTDOWN)
  2197. peer_mode |= SEND_SHUTDOWN;
  2198. if (mode&SEND_SHUTDOWN)
  2199. peer_mode |= RCV_SHUTDOWN;
  2200. unix_state_lock(other);
  2201. other->sk_shutdown |= peer_mode;
  2202. unix_state_unlock(other);
  2203. other->sk_state_change(other);
  2204. if (peer_mode == SHUTDOWN_MASK)
  2205. sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP);
  2206. else if (peer_mode & RCV_SHUTDOWN)
  2207. sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN);
  2208. }
  2209. if (other)
  2210. sock_put(other);
  2211. return 0;
  2212. }
  2213. long unix_inq_len(struct sock *sk)
  2214. {
  2215. struct sk_buff *skb;
  2216. long amount = 0;
  2217. if (sk->sk_state == TCP_LISTEN)
  2218. return -EINVAL;
  2219. spin_lock(&sk->sk_receive_queue.lock);
  2220. if (sk->sk_type == SOCK_STREAM ||
  2221. sk->sk_type == SOCK_SEQPACKET) {
  2222. skb_queue_walk(&sk->sk_receive_queue, skb)
  2223. amount += unix_skb_len(skb);
  2224. } else {
  2225. skb = skb_peek(&sk->sk_receive_queue);
  2226. if (skb)
  2227. amount = skb->len;
  2228. }
  2229. spin_unlock(&sk->sk_receive_queue.lock);
  2230. return amount;
  2231. }
  2232. EXPORT_SYMBOL_GPL(unix_inq_len);
  2233. long unix_outq_len(struct sock *sk)
  2234. {
  2235. return sk_wmem_alloc_get(sk);
  2236. }
  2237. EXPORT_SYMBOL_GPL(unix_outq_len);
  2238. static int unix_open_file(struct sock *sk)
  2239. {
  2240. struct path path;
  2241. struct file *f;
  2242. int fd;
  2243. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  2244. return -EPERM;
  2245. if (!smp_load_acquire(&unix_sk(sk)->addr))
  2246. return -ENOENT;
  2247. path = unix_sk(sk)->path;
  2248. if (!path.dentry)
  2249. return -ENOENT;
  2250. path_get(&path);
  2251. fd = get_unused_fd_flags(O_CLOEXEC);
  2252. if (fd < 0)
  2253. goto out;
  2254. f = dentry_open(&path, O_PATH, current_cred());
  2255. if (IS_ERR(f)) {
  2256. put_unused_fd(fd);
  2257. fd = PTR_ERR(f);
  2258. goto out;
  2259. }
  2260. fd_install(fd, f);
  2261. out:
  2262. path_put(&path);
  2263. return fd;
  2264. }
  2265. static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2266. {
  2267. struct sock *sk = sock->sk;
  2268. long amount = 0;
  2269. int err;
  2270. switch (cmd) {
  2271. case SIOCOUTQ:
  2272. amount = unix_outq_len(sk);
  2273. err = put_user(amount, (int __user *)arg);
  2274. break;
  2275. case SIOCINQ:
  2276. amount = unix_inq_len(sk);
  2277. if (amount < 0)
  2278. err = amount;
  2279. else
  2280. err = put_user(amount, (int __user *)arg);
  2281. break;
  2282. case SIOCUNIXFILE:
  2283. err = unix_open_file(sk);
  2284. break;
  2285. default:
  2286. err = -ENOIOCTLCMD;
  2287. break;
  2288. }
  2289. return err;
  2290. }
  2291. #ifdef CONFIG_COMPAT
  2292. static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2293. {
  2294. return unix_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
  2295. }
  2296. #endif
  2297. static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wait)
  2298. {
  2299. struct sock *sk = sock->sk;
  2300. __poll_t mask;
  2301. sock_poll_wait(file, sock, wait);
  2302. mask = 0;
  2303. /* exceptional events? */
  2304. if (sk->sk_err)
  2305. mask |= EPOLLERR;
  2306. if (sk->sk_shutdown == SHUTDOWN_MASK)
  2307. mask |= EPOLLHUP;
  2308. if (sk->sk_shutdown & RCV_SHUTDOWN)
  2309. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  2310. /* readable? */
  2311. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  2312. mask |= EPOLLIN | EPOLLRDNORM;
  2313. /* Connection-based need to check for termination and startup */
  2314. if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
  2315. sk->sk_state == TCP_CLOSE)
  2316. mask |= EPOLLHUP;
  2317. /*
  2318. * we set writable also when the other side has shut down the
  2319. * connection. This prevents stuck sockets.
  2320. */
  2321. if (unix_writable(sk))
  2322. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  2323. return mask;
  2324. }
  2325. static __poll_t unix_dgram_poll(struct file *file, struct socket *sock,
  2326. poll_table *wait)
  2327. {
  2328. struct sock *sk = sock->sk, *other;
  2329. unsigned int writable;
  2330. __poll_t mask;
  2331. sock_poll_wait(file, sock, wait);
  2332. mask = 0;
  2333. /* exceptional events? */
  2334. if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
  2335. mask |= EPOLLERR |
  2336. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
  2337. if (sk->sk_shutdown & RCV_SHUTDOWN)
  2338. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  2339. if (sk->sk_shutdown == SHUTDOWN_MASK)
  2340. mask |= EPOLLHUP;
  2341. /* readable? */
  2342. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  2343. mask |= EPOLLIN | EPOLLRDNORM;
  2344. /* Connection-based need to check for termination and startup */
  2345. if (sk->sk_type == SOCK_SEQPACKET) {
  2346. if (sk->sk_state == TCP_CLOSE)
  2347. mask |= EPOLLHUP;
  2348. /* connection hasn't started yet? */
  2349. if (sk->sk_state == TCP_SYN_SENT)
  2350. return mask;
  2351. }
  2352. /* No write status requested, avoid expensive OUT tests. */
  2353. if (!(poll_requested_events(wait) & (EPOLLWRBAND|EPOLLWRNORM|EPOLLOUT)))
  2354. return mask;
  2355. writable = unix_writable(sk);
  2356. if (writable) {
  2357. unix_state_lock(sk);
  2358. other = unix_peer(sk);
  2359. if (other && unix_peer(other) != sk &&
  2360. unix_recvq_full_lockless(other) &&
  2361. unix_dgram_peer_wake_me(sk, other))
  2362. writable = 0;
  2363. unix_state_unlock(sk);
  2364. }
  2365. if (writable)
  2366. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  2367. else
  2368. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  2369. return mask;
  2370. }
  2371. #ifdef CONFIG_PROC_FS
  2372. #define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
  2373. #define get_bucket(x) ((x) >> BUCKET_SPACE)
  2374. #define get_offset(x) ((x) & ((1L << BUCKET_SPACE) - 1))
  2375. #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
  2376. static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos)
  2377. {
  2378. unsigned long offset = get_offset(*pos);
  2379. unsigned long bucket = get_bucket(*pos);
  2380. struct sock *sk;
  2381. unsigned long count = 0;
  2382. for (sk = sk_head(&unix_socket_table[bucket]); sk; sk = sk_next(sk)) {
  2383. if (sock_net(sk) != seq_file_net(seq))
  2384. continue;
  2385. if (++count == offset)
  2386. break;
  2387. }
  2388. return sk;
  2389. }
  2390. static struct sock *unix_next_socket(struct seq_file *seq,
  2391. struct sock *sk,
  2392. loff_t *pos)
  2393. {
  2394. unsigned long bucket;
  2395. while (sk > (struct sock *)SEQ_START_TOKEN) {
  2396. sk = sk_next(sk);
  2397. if (!sk)
  2398. goto next_bucket;
  2399. if (sock_net(sk) == seq_file_net(seq))
  2400. return sk;
  2401. }
  2402. do {
  2403. sk = unix_from_bucket(seq, pos);
  2404. if (sk)
  2405. return sk;
  2406. next_bucket:
  2407. bucket = get_bucket(*pos) + 1;
  2408. *pos = set_bucket_offset(bucket, 1);
  2409. } while (bucket < ARRAY_SIZE(unix_socket_table));
  2410. return NULL;
  2411. }
  2412. static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
  2413. __acquires(unix_table_lock)
  2414. {
  2415. spin_lock(&unix_table_lock);
  2416. if (!*pos)
  2417. return SEQ_START_TOKEN;
  2418. if (get_bucket(*pos) >= ARRAY_SIZE(unix_socket_table))
  2419. return NULL;
  2420. return unix_next_socket(seq, NULL, pos);
  2421. }
  2422. static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2423. {
  2424. ++*pos;
  2425. return unix_next_socket(seq, v, pos);
  2426. }
  2427. static void unix_seq_stop(struct seq_file *seq, void *v)
  2428. __releases(unix_table_lock)
  2429. {
  2430. spin_unlock(&unix_table_lock);
  2431. }
  2432. static int unix_seq_show(struct seq_file *seq, void *v)
  2433. {
  2434. if (v == SEQ_START_TOKEN)
  2435. seq_puts(seq, "Num RefCount Protocol Flags Type St "
  2436. "Inode Path\n");
  2437. else {
  2438. struct sock *s = v;
  2439. struct unix_sock *u = unix_sk(s);
  2440. unix_state_lock(s);
  2441. seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu",
  2442. s,
  2443. refcount_read(&s->sk_refcnt),
  2444. 0,
  2445. s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
  2446. s->sk_type,
  2447. s->sk_socket ?
  2448. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
  2449. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
  2450. sock_i_ino(s));
  2451. if (u->addr) { // under unix_table_lock here
  2452. int i, len;
  2453. seq_putc(seq, ' ');
  2454. i = 0;
  2455. len = u->addr->len - sizeof(short);
  2456. if (!UNIX_ABSTRACT(s))
  2457. len--;
  2458. else {
  2459. seq_putc(seq, '@');
  2460. i++;
  2461. }
  2462. for ( ; i < len; i++)
  2463. seq_putc(seq, u->addr->name->sun_path[i] ?:
  2464. '@');
  2465. }
  2466. unix_state_unlock(s);
  2467. seq_putc(seq, '\n');
  2468. }
  2469. return 0;
  2470. }
  2471. static const struct seq_operations unix_seq_ops = {
  2472. .start = unix_seq_start,
  2473. .next = unix_seq_next,
  2474. .stop = unix_seq_stop,
  2475. .show = unix_seq_show,
  2476. };
  2477. #endif
  2478. static const struct net_proto_family unix_family_ops = {
  2479. .family = PF_UNIX,
  2480. .create = unix_create,
  2481. .owner = THIS_MODULE,
  2482. };
  2483. static int __net_init unix_net_init(struct net *net)
  2484. {
  2485. int error = -ENOMEM;
  2486. net->unx.sysctl_max_dgram_qlen = 10;
  2487. if (unix_sysctl_register(net))
  2488. goto out;
  2489. #ifdef CONFIG_PROC_FS
  2490. if (!proc_create_net("unix", 0, net->proc_net, &unix_seq_ops,
  2491. sizeof(struct seq_net_private))) {
  2492. unix_sysctl_unregister(net);
  2493. goto out;
  2494. }
  2495. #endif
  2496. error = 0;
  2497. out:
  2498. return error;
  2499. }
  2500. static void __net_exit unix_net_exit(struct net *net)
  2501. {
  2502. unix_sysctl_unregister(net);
  2503. remove_proc_entry("unix", net->proc_net);
  2504. }
  2505. static struct pernet_operations unix_net_ops = {
  2506. .init = unix_net_init,
  2507. .exit = unix_net_exit,
  2508. };
  2509. static int __init af_unix_init(void)
  2510. {
  2511. int rc = -1;
  2512. BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb));
  2513. rc = proto_register(&unix_proto, 1);
  2514. if (rc != 0) {
  2515. pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
  2516. goto out;
  2517. }
  2518. sock_register(&unix_family_ops);
  2519. register_pernet_subsys(&unix_net_ops);
  2520. out:
  2521. return rc;
  2522. }
  2523. static void __exit af_unix_exit(void)
  2524. {
  2525. sock_unregister(PF_UNIX);
  2526. proto_unregister(&unix_proto);
  2527. unregister_pernet_subsys(&unix_net_ops);
  2528. }
  2529. /* Earlier than device_initcall() so that other drivers invoking
  2530. request_module() don't end up in a loop when modprobe tries
  2531. to use a UNIX socket. But later than subsys_initcall() because
  2532. we depend on stuff initialised there */
  2533. fs_initcall(af_unix_init);
  2534. module_exit(af_unix_exit);
  2535. MODULE_LICENSE("GPL");
  2536. MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
  2537. MODULE_ALIAS_NETPROTO(PF_UNIX);