af_unix.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843
  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/filter.h>
  91. #include <linux/termios.h>
  92. #include <linux/sockios.h>
  93. #include <linux/net.h>
  94. #include <linux/in.h>
  95. #include <linux/fs.h>
  96. #include <linux/slab.h>
  97. #include <linux/uaccess.h>
  98. #include <linux/skbuff.h>
  99. #include <linux/netdevice.h>
  100. #include <net/net_namespace.h>
  101. #include <net/sock.h>
  102. #include <net/tcp_states.h>
  103. #include <net/af_unix.h>
  104. #include <linux/proc_fs.h>
  105. #include <linux/seq_file.h>
  106. #include <net/scm.h>
  107. #include <linux/init.h>
  108. #include <linux/poll.h>
  109. #include <linux/rtnetlink.h>
  110. #include <linux/mount.h>
  111. #include <net/checksum.h>
  112. #include <linux/security.h>
  113. #include <linux/splice.h>
  114. #include <linux/freezer.h>
  115. #include <linux/file.h>
  116. #include <linux/btf_ids.h>
  117. #include <linux/bpf-cgroup.h>
  118. static atomic_long_t unix_nr_socks;
  119. static struct hlist_head bsd_socket_buckets[UNIX_HASH_SIZE / 2];
  120. static spinlock_t bsd_socket_locks[UNIX_HASH_SIZE / 2];
  121. /* SMP locking strategy:
  122. * hash table is protected with spinlock.
  123. * each socket state is protected by separate spinlock.
  124. */
  125. #ifdef CONFIG_PROVE_LOCKING
  126. #define cmp_ptr(l, r) (((l) > (r)) - ((l) < (r)))
  127. static int unix_table_lock_cmp_fn(const struct lockdep_map *a,
  128. const struct lockdep_map *b)
  129. {
  130. return cmp_ptr(a, b);
  131. }
  132. static int unix_state_lock_cmp_fn(const struct lockdep_map *_a,
  133. const struct lockdep_map *_b)
  134. {
  135. const struct unix_sock *a, *b;
  136. a = container_of(_a, struct unix_sock, lock.dep_map);
  137. b = container_of(_b, struct unix_sock, lock.dep_map);
  138. if (a->sk.sk_state == TCP_LISTEN) {
  139. /* unix_stream_connect(): Before the 2nd unix_state_lock(),
  140. *
  141. * 1. a is TCP_LISTEN.
  142. * 2. b is not a.
  143. * 3. concurrent connect(b -> a) must fail.
  144. *
  145. * Except for 2. & 3., the b's state can be any possible
  146. * value due to concurrent connect() or listen().
  147. *
  148. * 2. is detected in debug_spin_lock_before(), and 3. cannot
  149. * be expressed as lock_cmp_fn.
  150. */
  151. switch (b->sk.sk_state) {
  152. case TCP_CLOSE:
  153. case TCP_ESTABLISHED:
  154. case TCP_LISTEN:
  155. return -1;
  156. default:
  157. /* Invalid case. */
  158. return 0;
  159. }
  160. }
  161. /* Should never happen. Just to be symmetric. */
  162. if (b->sk.sk_state == TCP_LISTEN) {
  163. switch (b->sk.sk_state) {
  164. case TCP_CLOSE:
  165. case TCP_ESTABLISHED:
  166. return 1;
  167. default:
  168. return 0;
  169. }
  170. }
  171. /* unix_state_double_lock(): ascending address order. */
  172. return cmp_ptr(a, b);
  173. }
  174. static int unix_recvq_lock_cmp_fn(const struct lockdep_map *_a,
  175. const struct lockdep_map *_b)
  176. {
  177. const struct sock *a, *b;
  178. a = container_of(_a, struct sock, sk_receive_queue.lock.dep_map);
  179. b = container_of(_b, struct sock, sk_receive_queue.lock.dep_map);
  180. /* unix_collect_skb(): listener -> embryo order. */
  181. if (a->sk_state == TCP_LISTEN && unix_sk(b)->listener == a)
  182. return -1;
  183. /* Should never happen. Just to be symmetric. */
  184. if (b->sk_state == TCP_LISTEN && unix_sk(a)->listener == b)
  185. return 1;
  186. return 0;
  187. }
  188. #endif
  189. static unsigned int unix_unbound_hash(struct sock *sk)
  190. {
  191. unsigned long hash = (unsigned long)sk;
  192. hash ^= hash >> 16;
  193. hash ^= hash >> 8;
  194. hash ^= sk->sk_type;
  195. return hash & UNIX_HASH_MOD;
  196. }
  197. static unsigned int unix_bsd_hash(struct inode *i)
  198. {
  199. return i->i_ino & UNIX_HASH_MOD;
  200. }
  201. static unsigned int unix_abstract_hash(struct sockaddr_un *sunaddr,
  202. int addr_len, int type)
  203. {
  204. __wsum csum = csum_partial(sunaddr, addr_len, 0);
  205. unsigned int hash;
  206. hash = (__force unsigned int)csum_fold(csum);
  207. hash ^= hash >> 8;
  208. hash ^= type;
  209. return UNIX_HASH_MOD + 1 + (hash & UNIX_HASH_MOD);
  210. }
  211. static void unix_table_double_lock(struct net *net,
  212. unsigned int hash1, unsigned int hash2)
  213. {
  214. if (hash1 == hash2) {
  215. spin_lock(&net->unx.table.locks[hash1]);
  216. return;
  217. }
  218. if (hash1 > hash2)
  219. swap(hash1, hash2);
  220. spin_lock(&net->unx.table.locks[hash1]);
  221. spin_lock(&net->unx.table.locks[hash2]);
  222. }
  223. static void unix_table_double_unlock(struct net *net,
  224. unsigned int hash1, unsigned int hash2)
  225. {
  226. if (hash1 == hash2) {
  227. spin_unlock(&net->unx.table.locks[hash1]);
  228. return;
  229. }
  230. spin_unlock(&net->unx.table.locks[hash1]);
  231. spin_unlock(&net->unx.table.locks[hash2]);
  232. }
  233. #ifdef CONFIG_SECURITY_NETWORK
  234. static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  235. {
  236. UNIXCB(skb).secid = scm->secid;
  237. }
  238. static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  239. {
  240. scm->secid = UNIXCB(skb).secid;
  241. }
  242. static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
  243. {
  244. return (scm->secid == UNIXCB(skb).secid);
  245. }
  246. #else
  247. static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  248. { }
  249. static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
  250. { }
  251. static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
  252. {
  253. return true;
  254. }
  255. #endif /* CONFIG_SECURITY_NETWORK */
  256. static inline int unix_our_peer(struct sock *sk, struct sock *osk)
  257. {
  258. return unix_peer(osk) == sk;
  259. }
  260. static inline int unix_may_send(struct sock *sk, struct sock *osk)
  261. {
  262. return unix_peer(osk) == NULL || unix_our_peer(sk, osk);
  263. }
  264. static inline int unix_recvq_full_lockless(const struct sock *sk)
  265. {
  266. return skb_queue_len_lockless(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
  267. }
  268. struct sock *unix_peer_get(struct sock *s)
  269. {
  270. struct sock *peer;
  271. unix_state_lock(s);
  272. peer = unix_peer(s);
  273. if (peer)
  274. sock_hold(peer);
  275. unix_state_unlock(s);
  276. return peer;
  277. }
  278. EXPORT_SYMBOL_GPL(unix_peer_get);
  279. static struct unix_address *unix_create_addr(struct sockaddr_un *sunaddr,
  280. int addr_len)
  281. {
  282. struct unix_address *addr;
  283. addr = kmalloc(sizeof(*addr) + addr_len, GFP_KERNEL);
  284. if (!addr)
  285. return NULL;
  286. refcount_set(&addr->refcnt, 1);
  287. addr->len = addr_len;
  288. memcpy(addr->name, sunaddr, addr_len);
  289. return addr;
  290. }
  291. static inline void unix_release_addr(struct unix_address *addr)
  292. {
  293. if (refcount_dec_and_test(&addr->refcnt))
  294. kfree(addr);
  295. }
  296. /*
  297. * Check unix socket name:
  298. * - should be not zero length.
  299. * - if started by not zero, should be NULL terminated (FS object)
  300. * - if started by zero, it is abstract name.
  301. */
  302. static int unix_validate_addr(struct sockaddr_un *sunaddr, int addr_len)
  303. {
  304. if (addr_len <= offsetof(struct sockaddr_un, sun_path) ||
  305. addr_len > sizeof(*sunaddr))
  306. return -EINVAL;
  307. if (sunaddr->sun_family != AF_UNIX)
  308. return -EINVAL;
  309. return 0;
  310. }
  311. static int unix_mkname_bsd(struct sockaddr_un *sunaddr, int addr_len)
  312. {
  313. struct sockaddr_storage *addr = (struct sockaddr_storage *)sunaddr;
  314. short offset = offsetof(struct sockaddr_storage, __data);
  315. BUILD_BUG_ON(offset != offsetof(struct sockaddr_un, sun_path));
  316. /* This may look like an off by one error but it is a bit more
  317. * subtle. 108 is the longest valid AF_UNIX path for a binding.
  318. * sun_path[108] doesn't as such exist. However in kernel space
  319. * we are guaranteed that it is a valid memory location in our
  320. * kernel address buffer because syscall functions always pass
  321. * a pointer of struct sockaddr_storage which has a bigger buffer
  322. * than 108. Also, we must terminate sun_path for strlen() in
  323. * getname_kernel().
  324. */
  325. addr->__data[addr_len - offset] = 0;
  326. /* Don't pass sunaddr->sun_path to strlen(). Otherwise, 108 will
  327. * cause panic if CONFIG_FORTIFY_SOURCE=y. Let __fortify_strlen()
  328. * know the actual buffer.
  329. */
  330. return strlen(addr->__data) + offset + 1;
  331. }
  332. static void __unix_remove_socket(struct sock *sk)
  333. {
  334. sk_del_node_init(sk);
  335. }
  336. static void __unix_insert_socket(struct net *net, struct sock *sk)
  337. {
  338. DEBUG_NET_WARN_ON_ONCE(!sk_unhashed(sk));
  339. sk_add_node(sk, &net->unx.table.buckets[sk->sk_hash]);
  340. }
  341. static void __unix_set_addr_hash(struct net *net, struct sock *sk,
  342. struct unix_address *addr, unsigned int hash)
  343. {
  344. __unix_remove_socket(sk);
  345. smp_store_release(&unix_sk(sk)->addr, addr);
  346. sk->sk_hash = hash;
  347. __unix_insert_socket(net, sk);
  348. }
  349. static void unix_remove_socket(struct net *net, struct sock *sk)
  350. {
  351. spin_lock(&net->unx.table.locks[sk->sk_hash]);
  352. __unix_remove_socket(sk);
  353. spin_unlock(&net->unx.table.locks[sk->sk_hash]);
  354. }
  355. static void unix_insert_unbound_socket(struct net *net, struct sock *sk)
  356. {
  357. spin_lock(&net->unx.table.locks[sk->sk_hash]);
  358. __unix_insert_socket(net, sk);
  359. spin_unlock(&net->unx.table.locks[sk->sk_hash]);
  360. }
  361. static void unix_insert_bsd_socket(struct sock *sk)
  362. {
  363. spin_lock(&bsd_socket_locks[sk->sk_hash]);
  364. sk_add_bind_node(sk, &bsd_socket_buckets[sk->sk_hash]);
  365. spin_unlock(&bsd_socket_locks[sk->sk_hash]);
  366. }
  367. static void unix_remove_bsd_socket(struct sock *sk)
  368. {
  369. if (!hlist_unhashed(&sk->sk_bind_node)) {
  370. spin_lock(&bsd_socket_locks[sk->sk_hash]);
  371. __sk_del_bind_node(sk);
  372. spin_unlock(&bsd_socket_locks[sk->sk_hash]);
  373. sk_node_init(&sk->sk_bind_node);
  374. }
  375. }
  376. static struct sock *__unix_find_socket_byname(struct net *net,
  377. struct sockaddr_un *sunname,
  378. int len, unsigned int hash)
  379. {
  380. struct sock *s;
  381. sk_for_each(s, &net->unx.table.buckets[hash]) {
  382. struct unix_sock *u = unix_sk(s);
  383. if (u->addr->len == len &&
  384. !memcmp(u->addr->name, sunname, len))
  385. return s;
  386. }
  387. return NULL;
  388. }
  389. static inline struct sock *unix_find_socket_byname(struct net *net,
  390. struct sockaddr_un *sunname,
  391. int len, unsigned int hash)
  392. {
  393. struct sock *s;
  394. spin_lock(&net->unx.table.locks[hash]);
  395. s = __unix_find_socket_byname(net, sunname, len, hash);
  396. if (s)
  397. sock_hold(s);
  398. spin_unlock(&net->unx.table.locks[hash]);
  399. return s;
  400. }
  401. static struct sock *unix_find_socket_byinode(struct inode *i)
  402. {
  403. unsigned int hash = unix_bsd_hash(i);
  404. struct sock *s;
  405. spin_lock(&bsd_socket_locks[hash]);
  406. sk_for_each_bound(s, &bsd_socket_buckets[hash]) {
  407. struct dentry *dentry = unix_sk(s)->path.dentry;
  408. if (dentry && d_backing_inode(dentry) == i) {
  409. sock_hold(s);
  410. spin_unlock(&bsd_socket_locks[hash]);
  411. return s;
  412. }
  413. }
  414. spin_unlock(&bsd_socket_locks[hash]);
  415. return NULL;
  416. }
  417. /* Support code for asymmetrically connected dgram sockets
  418. *
  419. * If a datagram socket is connected to a socket not itself connected
  420. * to the first socket (eg, /dev/log), clients may only enqueue more
  421. * messages if the present receive queue of the server socket is not
  422. * "too large". This means there's a second writeability condition
  423. * poll and sendmsg need to test. The dgram recv code will do a wake
  424. * up on the peer_wait wait queue of a socket upon reception of a
  425. * datagram which needs to be propagated to sleeping would-be writers
  426. * since these might not have sent anything so far. This can't be
  427. * accomplished via poll_wait because the lifetime of the server
  428. * socket might be less than that of its clients if these break their
  429. * association with it or if the server socket is closed while clients
  430. * are still connected to it and there's no way to inform "a polling
  431. * implementation" that it should let go of a certain wait queue
  432. *
  433. * In order to propagate a wake up, a wait_queue_entry_t of the client
  434. * socket is enqueued on the peer_wait queue of the server socket
  435. * whose wake function does a wake_up on the ordinary client socket
  436. * wait queue. This connection is established whenever a write (or
  437. * poll for write) hit the flow control condition and broken when the
  438. * association to the server socket is dissolved or after a wake up
  439. * was relayed.
  440. */
  441. static int unix_dgram_peer_wake_relay(wait_queue_entry_t *q, unsigned mode, int flags,
  442. void *key)
  443. {
  444. struct unix_sock *u;
  445. wait_queue_head_t *u_sleep;
  446. u = container_of(q, struct unix_sock, peer_wake);
  447. __remove_wait_queue(&unix_sk(u->peer_wake.private)->peer_wait,
  448. q);
  449. u->peer_wake.private = NULL;
  450. /* relaying can only happen while the wq still exists */
  451. u_sleep = sk_sleep(&u->sk);
  452. if (u_sleep)
  453. wake_up_interruptible_poll(u_sleep, key_to_poll(key));
  454. return 0;
  455. }
  456. static int unix_dgram_peer_wake_connect(struct sock *sk, struct sock *other)
  457. {
  458. struct unix_sock *u, *u_other;
  459. int rc;
  460. u = unix_sk(sk);
  461. u_other = unix_sk(other);
  462. rc = 0;
  463. spin_lock(&u_other->peer_wait.lock);
  464. if (!u->peer_wake.private) {
  465. u->peer_wake.private = other;
  466. __add_wait_queue(&u_other->peer_wait, &u->peer_wake);
  467. rc = 1;
  468. }
  469. spin_unlock(&u_other->peer_wait.lock);
  470. return rc;
  471. }
  472. static void unix_dgram_peer_wake_disconnect(struct sock *sk,
  473. struct sock *other)
  474. {
  475. struct unix_sock *u, *u_other;
  476. u = unix_sk(sk);
  477. u_other = unix_sk(other);
  478. spin_lock(&u_other->peer_wait.lock);
  479. if (u->peer_wake.private == other) {
  480. __remove_wait_queue(&u_other->peer_wait, &u->peer_wake);
  481. u->peer_wake.private = NULL;
  482. }
  483. spin_unlock(&u_other->peer_wait.lock);
  484. }
  485. static void unix_dgram_peer_wake_disconnect_wakeup(struct sock *sk,
  486. struct sock *other)
  487. {
  488. unix_dgram_peer_wake_disconnect(sk, other);
  489. wake_up_interruptible_poll(sk_sleep(sk),
  490. EPOLLOUT |
  491. EPOLLWRNORM |
  492. EPOLLWRBAND);
  493. }
  494. /* preconditions:
  495. * - unix_peer(sk) == other
  496. * - association is stable
  497. */
  498. static int unix_dgram_peer_wake_me(struct sock *sk, struct sock *other)
  499. {
  500. int connected;
  501. connected = unix_dgram_peer_wake_connect(sk, other);
  502. /* If other is SOCK_DEAD, we want to make sure we signal
  503. * POLLOUT, such that a subsequent write() can get a
  504. * -ECONNREFUSED. Otherwise, if we haven't queued any skbs
  505. * to other and its full, we will hang waiting for POLLOUT.
  506. */
  507. if (unix_recvq_full_lockless(other) && !sock_flag(other, SOCK_DEAD))
  508. return 1;
  509. if (connected)
  510. unix_dgram_peer_wake_disconnect(sk, other);
  511. return 0;
  512. }
  513. static int unix_writable(const struct sock *sk, unsigned char state)
  514. {
  515. return state != TCP_LISTEN &&
  516. (refcount_read(&sk->sk_wmem_alloc) << 2) <= READ_ONCE(sk->sk_sndbuf);
  517. }
  518. static void unix_write_space(struct sock *sk)
  519. {
  520. struct socket_wq *wq;
  521. rcu_read_lock();
  522. if (unix_writable(sk, READ_ONCE(sk->sk_state))) {
  523. wq = rcu_dereference(sk->sk_wq);
  524. if (skwq_has_sleeper(wq))
  525. wake_up_interruptible_sync_poll(&wq->wait,
  526. EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
  527. sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT);
  528. }
  529. rcu_read_unlock();
  530. }
  531. /* When dgram socket disconnects (or changes its peer), we clear its receive
  532. * queue of packets arrived from previous peer. First, it allows to do
  533. * flow control based only on wmem_alloc; second, sk connected to peer
  534. * may receive messages only from that peer. */
  535. static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
  536. {
  537. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  538. skb_queue_purge(&sk->sk_receive_queue);
  539. wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
  540. /* If one link of bidirectional dgram pipe is disconnected,
  541. * we signal error. Messages are lost. Do not make this,
  542. * when peer was not connected to us.
  543. */
  544. if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
  545. WRITE_ONCE(other->sk_err, ECONNRESET);
  546. sk_error_report(other);
  547. }
  548. }
  549. }
  550. static void unix_sock_destructor(struct sock *sk)
  551. {
  552. struct unix_sock *u = unix_sk(sk);
  553. skb_queue_purge(&sk->sk_receive_queue);
  554. DEBUG_NET_WARN_ON_ONCE(refcount_read(&sk->sk_wmem_alloc));
  555. DEBUG_NET_WARN_ON_ONCE(!sk_unhashed(sk));
  556. DEBUG_NET_WARN_ON_ONCE(sk->sk_socket);
  557. if (!sock_flag(sk, SOCK_DEAD)) {
  558. pr_info("Attempt to release alive unix socket: %p\n", sk);
  559. return;
  560. }
  561. if (u->addr)
  562. unix_release_addr(u->addr);
  563. atomic_long_dec(&unix_nr_socks);
  564. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  565. #ifdef UNIX_REFCNT_DEBUG
  566. pr_debug("UNIX %p is destroyed, %ld are still alive.\n", sk,
  567. atomic_long_read(&unix_nr_socks));
  568. #endif
  569. }
  570. static unsigned int unix_skb_len(const struct sk_buff *skb)
  571. {
  572. return skb->len - UNIXCB(skb).consumed;
  573. }
  574. static void unix_release_sock(struct sock *sk, int embrion)
  575. {
  576. struct unix_sock *u = unix_sk(sk);
  577. struct sock *skpair;
  578. struct sk_buff *skb;
  579. struct path path;
  580. int state;
  581. unix_remove_socket(sock_net(sk), sk);
  582. unix_remove_bsd_socket(sk);
  583. /* Clear state */
  584. unix_state_lock(sk);
  585. sock_orphan(sk);
  586. WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
  587. path = u->path;
  588. u->path.dentry = NULL;
  589. u->path.mnt = NULL;
  590. state = sk->sk_state;
  591. WRITE_ONCE(sk->sk_state, TCP_CLOSE);
  592. skpair = unix_peer(sk);
  593. unix_peer(sk) = NULL;
  594. unix_state_unlock(sk);
  595. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  596. u->oob_skb = NULL;
  597. #endif
  598. wake_up_interruptible_all(&u->peer_wait);
  599. if (skpair != NULL) {
  600. if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
  601. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  602. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  603. if (skb && !unix_skb_len(skb))
  604. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  605. #endif
  606. unix_state_lock(skpair);
  607. /* No more writes */
  608. WRITE_ONCE(skpair->sk_shutdown, SHUTDOWN_MASK);
  609. if (skb || embrion)
  610. WRITE_ONCE(skpair->sk_err, ECONNRESET);
  611. unix_state_unlock(skpair);
  612. skpair->sk_state_change(skpair);
  613. sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP);
  614. }
  615. unix_dgram_peer_wake_disconnect(sk, skpair);
  616. sock_put(skpair); /* It may now die */
  617. }
  618. /* Try to flush out this socket. Throw out buffers at least */
  619. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  620. if (state == TCP_LISTEN)
  621. unix_release_sock(skb->sk, 1);
  622. /* passed fds are erased in the kfree_skb hook */
  623. kfree_skb(skb);
  624. }
  625. if (path.dentry)
  626. path_put(&path);
  627. sock_put(sk);
  628. /* ---- Socket is dead now and most probably destroyed ---- */
  629. /*
  630. * Fixme: BSD difference: In BSD all sockets connected to us get
  631. * ECONNRESET and we die on the spot. In Linux we behave
  632. * like files and pipes do and wait for the last
  633. * dereference.
  634. *
  635. * Can't we simply set sock->err?
  636. *
  637. * What the above comment does talk about? --ANK(980817)
  638. */
  639. if (READ_ONCE(unix_tot_inflight))
  640. unix_gc(); /* Garbage collect fds */
  641. }
  642. static void init_peercred(struct sock *sk)
  643. {
  644. sk->sk_peer_pid = get_pid(task_tgid(current));
  645. sk->sk_peer_cred = get_current_cred();
  646. }
  647. static void update_peercred(struct sock *sk)
  648. {
  649. const struct cred *old_cred;
  650. struct pid *old_pid;
  651. spin_lock(&sk->sk_peer_lock);
  652. old_pid = sk->sk_peer_pid;
  653. old_cred = sk->sk_peer_cred;
  654. init_peercred(sk);
  655. spin_unlock(&sk->sk_peer_lock);
  656. put_pid(old_pid);
  657. put_cred(old_cred);
  658. }
  659. static void copy_peercred(struct sock *sk, struct sock *peersk)
  660. {
  661. lockdep_assert_held(&unix_sk(peersk)->lock);
  662. spin_lock(&sk->sk_peer_lock);
  663. sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
  664. sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
  665. spin_unlock(&sk->sk_peer_lock);
  666. }
  667. static int unix_listen(struct socket *sock, int backlog)
  668. {
  669. int err;
  670. struct sock *sk = sock->sk;
  671. struct unix_sock *u = unix_sk(sk);
  672. err = -EOPNOTSUPP;
  673. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  674. goto out; /* Only stream/seqpacket sockets accept */
  675. err = -EINVAL;
  676. if (!READ_ONCE(u->addr))
  677. goto out; /* No listens on an unbound socket */
  678. unix_state_lock(sk);
  679. if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
  680. goto out_unlock;
  681. if (backlog > sk->sk_max_ack_backlog)
  682. wake_up_interruptible_all(&u->peer_wait);
  683. sk->sk_max_ack_backlog = backlog;
  684. WRITE_ONCE(sk->sk_state, TCP_LISTEN);
  685. /* set credentials so connect can copy them */
  686. update_peercred(sk);
  687. err = 0;
  688. out_unlock:
  689. unix_state_unlock(sk);
  690. out:
  691. return err;
  692. }
  693. static int unix_release(struct socket *);
  694. static int unix_bind(struct socket *, struct sockaddr *, int);
  695. static int unix_stream_connect(struct socket *, struct sockaddr *,
  696. int addr_len, int flags);
  697. static int unix_socketpair(struct socket *, struct socket *);
  698. static int unix_accept(struct socket *, struct socket *, struct proto_accept_arg *arg);
  699. static int unix_getname(struct socket *, struct sockaddr *, int);
  700. static __poll_t unix_poll(struct file *, struct socket *, poll_table *);
  701. static __poll_t unix_dgram_poll(struct file *, struct socket *,
  702. poll_table *);
  703. static int unix_ioctl(struct socket *, unsigned int, unsigned long);
  704. #ifdef CONFIG_COMPAT
  705. static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
  706. #endif
  707. static int unix_shutdown(struct socket *, int);
  708. static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t);
  709. static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int);
  710. static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos,
  711. struct pipe_inode_info *, size_t size,
  712. unsigned int flags);
  713. static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
  714. static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
  715. static int unix_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
  716. static int unix_stream_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
  717. static int unix_dgram_connect(struct socket *, struct sockaddr *,
  718. int, int);
  719. static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
  720. static int unix_seqpacket_recvmsg(struct socket *, struct msghdr *, size_t,
  721. int);
  722. #ifdef CONFIG_PROC_FS
  723. static int unix_count_nr_fds(struct sock *sk)
  724. {
  725. struct sk_buff *skb;
  726. struct unix_sock *u;
  727. int nr_fds = 0;
  728. spin_lock(&sk->sk_receive_queue.lock);
  729. skb = skb_peek(&sk->sk_receive_queue);
  730. while (skb) {
  731. u = unix_sk(skb->sk);
  732. nr_fds += atomic_read(&u->scm_stat.nr_fds);
  733. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  734. }
  735. spin_unlock(&sk->sk_receive_queue.lock);
  736. return nr_fds;
  737. }
  738. static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
  739. {
  740. struct sock *sk = sock->sk;
  741. unsigned char s_state;
  742. struct unix_sock *u;
  743. int nr_fds = 0;
  744. if (sk) {
  745. s_state = READ_ONCE(sk->sk_state);
  746. u = unix_sk(sk);
  747. /* SOCK_STREAM and SOCK_SEQPACKET sockets never change their
  748. * sk_state after switching to TCP_ESTABLISHED or TCP_LISTEN.
  749. * SOCK_DGRAM is ordinary. So, no lock is needed.
  750. */
  751. if (sock->type == SOCK_DGRAM || s_state == TCP_ESTABLISHED)
  752. nr_fds = atomic_read(&u->scm_stat.nr_fds);
  753. else if (s_state == TCP_LISTEN)
  754. nr_fds = unix_count_nr_fds(sk);
  755. seq_printf(m, "scm_fds: %u\n", nr_fds);
  756. }
  757. }
  758. #else
  759. #define unix_show_fdinfo NULL
  760. #endif
  761. static const struct proto_ops unix_stream_ops = {
  762. .family = PF_UNIX,
  763. .owner = THIS_MODULE,
  764. .release = unix_release,
  765. .bind = unix_bind,
  766. .connect = unix_stream_connect,
  767. .socketpair = unix_socketpair,
  768. .accept = unix_accept,
  769. .getname = unix_getname,
  770. .poll = unix_poll,
  771. .ioctl = unix_ioctl,
  772. #ifdef CONFIG_COMPAT
  773. .compat_ioctl = unix_compat_ioctl,
  774. #endif
  775. .listen = unix_listen,
  776. .shutdown = unix_shutdown,
  777. .sendmsg = unix_stream_sendmsg,
  778. .recvmsg = unix_stream_recvmsg,
  779. .read_skb = unix_stream_read_skb,
  780. .mmap = sock_no_mmap,
  781. .splice_read = unix_stream_splice_read,
  782. .set_peek_off = sk_set_peek_off,
  783. .show_fdinfo = unix_show_fdinfo,
  784. };
  785. static const struct proto_ops unix_dgram_ops = {
  786. .family = PF_UNIX,
  787. .owner = THIS_MODULE,
  788. .release = unix_release,
  789. .bind = unix_bind,
  790. .connect = unix_dgram_connect,
  791. .socketpair = unix_socketpair,
  792. .accept = sock_no_accept,
  793. .getname = unix_getname,
  794. .poll = unix_dgram_poll,
  795. .ioctl = unix_ioctl,
  796. #ifdef CONFIG_COMPAT
  797. .compat_ioctl = unix_compat_ioctl,
  798. #endif
  799. .listen = sock_no_listen,
  800. .shutdown = unix_shutdown,
  801. .sendmsg = unix_dgram_sendmsg,
  802. .read_skb = unix_read_skb,
  803. .recvmsg = unix_dgram_recvmsg,
  804. .mmap = sock_no_mmap,
  805. .set_peek_off = sk_set_peek_off,
  806. .show_fdinfo = unix_show_fdinfo,
  807. };
  808. static const struct proto_ops unix_seqpacket_ops = {
  809. .family = PF_UNIX,
  810. .owner = THIS_MODULE,
  811. .release = unix_release,
  812. .bind = unix_bind,
  813. .connect = unix_stream_connect,
  814. .socketpair = unix_socketpair,
  815. .accept = unix_accept,
  816. .getname = unix_getname,
  817. .poll = unix_dgram_poll,
  818. .ioctl = unix_ioctl,
  819. #ifdef CONFIG_COMPAT
  820. .compat_ioctl = unix_compat_ioctl,
  821. #endif
  822. .listen = unix_listen,
  823. .shutdown = unix_shutdown,
  824. .sendmsg = unix_seqpacket_sendmsg,
  825. .recvmsg = unix_seqpacket_recvmsg,
  826. .mmap = sock_no_mmap,
  827. .set_peek_off = sk_set_peek_off,
  828. .show_fdinfo = unix_show_fdinfo,
  829. };
  830. static void unix_close(struct sock *sk, long timeout)
  831. {
  832. /* Nothing to do here, unix socket does not need a ->close().
  833. * This is merely for sockmap.
  834. */
  835. }
  836. static void unix_unhash(struct sock *sk)
  837. {
  838. /* Nothing to do here, unix socket does not need a ->unhash().
  839. * This is merely for sockmap.
  840. */
  841. }
  842. static bool unix_bpf_bypass_getsockopt(int level, int optname)
  843. {
  844. if (level == SOL_SOCKET) {
  845. switch (optname) {
  846. case SO_PEERPIDFD:
  847. return true;
  848. default:
  849. return false;
  850. }
  851. }
  852. return false;
  853. }
  854. struct proto unix_dgram_proto = {
  855. .name = "UNIX",
  856. .owner = THIS_MODULE,
  857. .obj_size = sizeof(struct unix_sock),
  858. .close = unix_close,
  859. .bpf_bypass_getsockopt = unix_bpf_bypass_getsockopt,
  860. #ifdef CONFIG_BPF_SYSCALL
  861. .psock_update_sk_prot = unix_dgram_bpf_update_proto,
  862. #endif
  863. };
  864. struct proto unix_stream_proto = {
  865. .name = "UNIX-STREAM",
  866. .owner = THIS_MODULE,
  867. .obj_size = sizeof(struct unix_sock),
  868. .close = unix_close,
  869. .unhash = unix_unhash,
  870. .bpf_bypass_getsockopt = unix_bpf_bypass_getsockopt,
  871. #ifdef CONFIG_BPF_SYSCALL
  872. .psock_update_sk_prot = unix_stream_bpf_update_proto,
  873. #endif
  874. };
  875. static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, int type)
  876. {
  877. struct unix_sock *u;
  878. struct sock *sk;
  879. int err;
  880. atomic_long_inc(&unix_nr_socks);
  881. if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files()) {
  882. err = -ENFILE;
  883. goto err;
  884. }
  885. if (type == SOCK_STREAM)
  886. sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_stream_proto, kern);
  887. else /*dgram and seqpacket */
  888. sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_dgram_proto, kern);
  889. if (!sk) {
  890. err = -ENOMEM;
  891. goto err;
  892. }
  893. sock_init_data(sock, sk);
  894. sk->sk_hash = unix_unbound_hash(sk);
  895. sk->sk_allocation = GFP_KERNEL_ACCOUNT;
  896. sk->sk_write_space = unix_write_space;
  897. sk->sk_max_ack_backlog = READ_ONCE(net->unx.sysctl_max_dgram_qlen);
  898. sk->sk_destruct = unix_sock_destructor;
  899. lock_set_cmp_fn(&sk->sk_receive_queue.lock, unix_recvq_lock_cmp_fn, NULL);
  900. u = unix_sk(sk);
  901. u->listener = NULL;
  902. u->vertex = NULL;
  903. u->path.dentry = NULL;
  904. u->path.mnt = NULL;
  905. spin_lock_init(&u->lock);
  906. lock_set_cmp_fn(&u->lock, unix_state_lock_cmp_fn, NULL);
  907. mutex_init(&u->iolock); /* single task reading lock */
  908. mutex_init(&u->bindlock); /* single task binding lock */
  909. init_waitqueue_head(&u->peer_wait);
  910. init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
  911. memset(&u->scm_stat, 0, sizeof(struct scm_stat));
  912. unix_insert_unbound_socket(net, sk);
  913. sock_prot_inuse_add(net, sk->sk_prot, 1);
  914. return sk;
  915. err:
  916. atomic_long_dec(&unix_nr_socks);
  917. return ERR_PTR(err);
  918. }
  919. static int unix_create(struct net *net, struct socket *sock, int protocol,
  920. int kern)
  921. {
  922. struct sock *sk;
  923. if (protocol && protocol != PF_UNIX)
  924. return -EPROTONOSUPPORT;
  925. sock->state = SS_UNCONNECTED;
  926. switch (sock->type) {
  927. case SOCK_STREAM:
  928. sock->ops = &unix_stream_ops;
  929. break;
  930. /*
  931. * Believe it or not BSD has AF_UNIX, SOCK_RAW though
  932. * nothing uses it.
  933. */
  934. case SOCK_RAW:
  935. sock->type = SOCK_DGRAM;
  936. fallthrough;
  937. case SOCK_DGRAM:
  938. sock->ops = &unix_dgram_ops;
  939. break;
  940. case SOCK_SEQPACKET:
  941. sock->ops = &unix_seqpacket_ops;
  942. break;
  943. default:
  944. return -ESOCKTNOSUPPORT;
  945. }
  946. sk = unix_create1(net, sock, kern, sock->type);
  947. if (IS_ERR(sk))
  948. return PTR_ERR(sk);
  949. return 0;
  950. }
  951. static int unix_release(struct socket *sock)
  952. {
  953. struct sock *sk = sock->sk;
  954. if (!sk)
  955. return 0;
  956. sk->sk_prot->close(sk, 0);
  957. unix_release_sock(sk, 0);
  958. sock->sk = NULL;
  959. return 0;
  960. }
  961. static struct sock *unix_find_bsd(struct sockaddr_un *sunaddr, int addr_len,
  962. int type)
  963. {
  964. struct inode *inode;
  965. struct path path;
  966. struct sock *sk;
  967. int err;
  968. unix_mkname_bsd(sunaddr, addr_len);
  969. err = kern_path(sunaddr->sun_path, LOOKUP_FOLLOW, &path);
  970. if (err)
  971. goto fail;
  972. err = path_permission(&path, MAY_WRITE);
  973. if (err)
  974. goto path_put;
  975. err = -ECONNREFUSED;
  976. inode = d_backing_inode(path.dentry);
  977. if (!S_ISSOCK(inode->i_mode))
  978. goto path_put;
  979. sk = unix_find_socket_byinode(inode);
  980. if (!sk)
  981. goto path_put;
  982. err = -EPROTOTYPE;
  983. if (sk->sk_type == type)
  984. touch_atime(&path);
  985. else
  986. goto sock_put;
  987. path_put(&path);
  988. return sk;
  989. sock_put:
  990. sock_put(sk);
  991. path_put:
  992. path_put(&path);
  993. fail:
  994. return ERR_PTR(err);
  995. }
  996. static struct sock *unix_find_abstract(struct net *net,
  997. struct sockaddr_un *sunaddr,
  998. int addr_len, int type)
  999. {
  1000. unsigned int hash = unix_abstract_hash(sunaddr, addr_len, type);
  1001. struct dentry *dentry;
  1002. struct sock *sk;
  1003. sk = unix_find_socket_byname(net, sunaddr, addr_len, hash);
  1004. if (!sk)
  1005. return ERR_PTR(-ECONNREFUSED);
  1006. dentry = unix_sk(sk)->path.dentry;
  1007. if (dentry)
  1008. touch_atime(&unix_sk(sk)->path);
  1009. return sk;
  1010. }
  1011. static struct sock *unix_find_other(struct net *net,
  1012. struct sockaddr_un *sunaddr,
  1013. int addr_len, int type)
  1014. {
  1015. struct sock *sk;
  1016. if (sunaddr->sun_path[0])
  1017. sk = unix_find_bsd(sunaddr, addr_len, type);
  1018. else
  1019. sk = unix_find_abstract(net, sunaddr, addr_len, type);
  1020. return sk;
  1021. }
  1022. static int unix_autobind(struct sock *sk)
  1023. {
  1024. struct unix_sock *u = unix_sk(sk);
  1025. unsigned int new_hash, old_hash;
  1026. struct net *net = sock_net(sk);
  1027. struct unix_address *addr;
  1028. u32 lastnum, ordernum;
  1029. int err;
  1030. err = mutex_lock_interruptible(&u->bindlock);
  1031. if (err)
  1032. return err;
  1033. if (u->addr)
  1034. goto out;
  1035. err = -ENOMEM;
  1036. addr = kzalloc(sizeof(*addr) +
  1037. offsetof(struct sockaddr_un, sun_path) + 16, GFP_KERNEL);
  1038. if (!addr)
  1039. goto out;
  1040. addr->len = offsetof(struct sockaddr_un, sun_path) + 6;
  1041. addr->name->sun_family = AF_UNIX;
  1042. refcount_set(&addr->refcnt, 1);
  1043. old_hash = sk->sk_hash;
  1044. ordernum = get_random_u32();
  1045. lastnum = ordernum & 0xFFFFF;
  1046. retry:
  1047. ordernum = (ordernum + 1) & 0xFFFFF;
  1048. sprintf(addr->name->sun_path + 1, "%05x", ordernum);
  1049. new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
  1050. unix_table_double_lock(net, old_hash, new_hash);
  1051. if (__unix_find_socket_byname(net, addr->name, addr->len, new_hash)) {
  1052. unix_table_double_unlock(net, old_hash, new_hash);
  1053. /* __unix_find_socket_byname() may take long time if many names
  1054. * are already in use.
  1055. */
  1056. cond_resched();
  1057. if (ordernum == lastnum) {
  1058. /* Give up if all names seems to be in use. */
  1059. err = -ENOSPC;
  1060. unix_release_addr(addr);
  1061. goto out;
  1062. }
  1063. goto retry;
  1064. }
  1065. __unix_set_addr_hash(net, sk, addr, new_hash);
  1066. unix_table_double_unlock(net, old_hash, new_hash);
  1067. err = 0;
  1068. out: mutex_unlock(&u->bindlock);
  1069. return err;
  1070. }
  1071. static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr,
  1072. int addr_len)
  1073. {
  1074. umode_t mode = S_IFSOCK |
  1075. (SOCK_INODE(sk->sk_socket)->i_mode & ~current_umask());
  1076. struct unix_sock *u = unix_sk(sk);
  1077. unsigned int new_hash, old_hash;
  1078. struct net *net = sock_net(sk);
  1079. struct mnt_idmap *idmap;
  1080. struct unix_address *addr;
  1081. struct dentry *dentry;
  1082. struct path parent;
  1083. int err;
  1084. addr_len = unix_mkname_bsd(sunaddr, addr_len);
  1085. addr = unix_create_addr(sunaddr, addr_len);
  1086. if (!addr)
  1087. return -ENOMEM;
  1088. /*
  1089. * Get the parent directory, calculate the hash for last
  1090. * component.
  1091. */
  1092. dentry = kern_path_create(AT_FDCWD, addr->name->sun_path, &parent, 0);
  1093. if (IS_ERR(dentry)) {
  1094. err = PTR_ERR(dentry);
  1095. goto out;
  1096. }
  1097. /*
  1098. * All right, let's create it.
  1099. */
  1100. idmap = mnt_idmap(parent.mnt);
  1101. err = security_path_mknod(&parent, dentry, mode, 0);
  1102. if (!err)
  1103. err = vfs_mknod(idmap, d_inode(parent.dentry), dentry, mode, 0);
  1104. if (err)
  1105. goto out_path;
  1106. err = mutex_lock_interruptible(&u->bindlock);
  1107. if (err)
  1108. goto out_unlink;
  1109. if (u->addr)
  1110. goto out_unlock;
  1111. old_hash = sk->sk_hash;
  1112. new_hash = unix_bsd_hash(d_backing_inode(dentry));
  1113. unix_table_double_lock(net, old_hash, new_hash);
  1114. u->path.mnt = mntget(parent.mnt);
  1115. u->path.dentry = dget(dentry);
  1116. __unix_set_addr_hash(net, sk, addr, new_hash);
  1117. unix_table_double_unlock(net, old_hash, new_hash);
  1118. unix_insert_bsd_socket(sk);
  1119. mutex_unlock(&u->bindlock);
  1120. done_path_create(&parent, dentry);
  1121. return 0;
  1122. out_unlock:
  1123. mutex_unlock(&u->bindlock);
  1124. err = -EINVAL;
  1125. out_unlink:
  1126. /* failed after successful mknod? unlink what we'd created... */
  1127. vfs_unlink(idmap, d_inode(parent.dentry), dentry, NULL);
  1128. out_path:
  1129. done_path_create(&parent, dentry);
  1130. out:
  1131. unix_release_addr(addr);
  1132. return err == -EEXIST ? -EADDRINUSE : err;
  1133. }
  1134. static int unix_bind_abstract(struct sock *sk, struct sockaddr_un *sunaddr,
  1135. int addr_len)
  1136. {
  1137. struct unix_sock *u = unix_sk(sk);
  1138. unsigned int new_hash, old_hash;
  1139. struct net *net = sock_net(sk);
  1140. struct unix_address *addr;
  1141. int err;
  1142. addr = unix_create_addr(sunaddr, addr_len);
  1143. if (!addr)
  1144. return -ENOMEM;
  1145. err = mutex_lock_interruptible(&u->bindlock);
  1146. if (err)
  1147. goto out;
  1148. if (u->addr) {
  1149. err = -EINVAL;
  1150. goto out_mutex;
  1151. }
  1152. old_hash = sk->sk_hash;
  1153. new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
  1154. unix_table_double_lock(net, old_hash, new_hash);
  1155. if (__unix_find_socket_byname(net, addr->name, addr->len, new_hash))
  1156. goto out_spin;
  1157. __unix_set_addr_hash(net, sk, addr, new_hash);
  1158. unix_table_double_unlock(net, old_hash, new_hash);
  1159. mutex_unlock(&u->bindlock);
  1160. return 0;
  1161. out_spin:
  1162. unix_table_double_unlock(net, old_hash, new_hash);
  1163. err = -EADDRINUSE;
  1164. out_mutex:
  1165. mutex_unlock(&u->bindlock);
  1166. out:
  1167. unix_release_addr(addr);
  1168. return err;
  1169. }
  1170. static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  1171. {
  1172. struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
  1173. struct sock *sk = sock->sk;
  1174. int err;
  1175. if (addr_len == offsetof(struct sockaddr_un, sun_path) &&
  1176. sunaddr->sun_family == AF_UNIX)
  1177. return unix_autobind(sk);
  1178. err = unix_validate_addr(sunaddr, addr_len);
  1179. if (err)
  1180. return err;
  1181. if (sunaddr->sun_path[0])
  1182. err = unix_bind_bsd(sk, sunaddr, addr_len);
  1183. else
  1184. err = unix_bind_abstract(sk, sunaddr, addr_len);
  1185. return err;
  1186. }
  1187. static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
  1188. {
  1189. if (unlikely(sk1 == sk2) || !sk2) {
  1190. unix_state_lock(sk1);
  1191. return;
  1192. }
  1193. if (sk1 > sk2)
  1194. swap(sk1, sk2);
  1195. unix_state_lock(sk1);
  1196. unix_state_lock(sk2);
  1197. }
  1198. static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
  1199. {
  1200. if (unlikely(sk1 == sk2) || !sk2) {
  1201. unix_state_unlock(sk1);
  1202. return;
  1203. }
  1204. unix_state_unlock(sk1);
  1205. unix_state_unlock(sk2);
  1206. }
  1207. static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
  1208. int alen, int flags)
  1209. {
  1210. struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
  1211. struct sock *sk = sock->sk;
  1212. struct sock *other;
  1213. int err;
  1214. err = -EINVAL;
  1215. if (alen < offsetofend(struct sockaddr, sa_family))
  1216. goto out;
  1217. if (addr->sa_family != AF_UNSPEC) {
  1218. err = unix_validate_addr(sunaddr, alen);
  1219. if (err)
  1220. goto out;
  1221. err = BPF_CGROUP_RUN_PROG_UNIX_CONNECT_LOCK(sk, addr, &alen);
  1222. if (err)
  1223. goto out;
  1224. if ((test_bit(SOCK_PASSCRED, &sock->flags) ||
  1225. test_bit(SOCK_PASSPIDFD, &sock->flags)) &&
  1226. !READ_ONCE(unix_sk(sk)->addr)) {
  1227. err = unix_autobind(sk);
  1228. if (err)
  1229. goto out;
  1230. }
  1231. restart:
  1232. other = unix_find_other(sock_net(sk), sunaddr, alen, sock->type);
  1233. if (IS_ERR(other)) {
  1234. err = PTR_ERR(other);
  1235. goto out;
  1236. }
  1237. unix_state_double_lock(sk, other);
  1238. /* Apparently VFS overslept socket death. Retry. */
  1239. if (sock_flag(other, SOCK_DEAD)) {
  1240. unix_state_double_unlock(sk, other);
  1241. sock_put(other);
  1242. goto restart;
  1243. }
  1244. err = -EPERM;
  1245. if (!unix_may_send(sk, other))
  1246. goto out_unlock;
  1247. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  1248. if (err)
  1249. goto out_unlock;
  1250. WRITE_ONCE(sk->sk_state, TCP_ESTABLISHED);
  1251. WRITE_ONCE(other->sk_state, TCP_ESTABLISHED);
  1252. } else {
  1253. /*
  1254. * 1003.1g breaking connected state with AF_UNSPEC
  1255. */
  1256. other = NULL;
  1257. unix_state_double_lock(sk, other);
  1258. }
  1259. /*
  1260. * If it was connected, reconnect.
  1261. */
  1262. if (unix_peer(sk)) {
  1263. struct sock *old_peer = unix_peer(sk);
  1264. unix_peer(sk) = other;
  1265. if (!other)
  1266. WRITE_ONCE(sk->sk_state, TCP_CLOSE);
  1267. unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer);
  1268. unix_state_double_unlock(sk, other);
  1269. if (other != old_peer) {
  1270. unix_dgram_disconnected(sk, old_peer);
  1271. unix_state_lock(old_peer);
  1272. if (!unix_peer(old_peer))
  1273. WRITE_ONCE(old_peer->sk_state, TCP_CLOSE);
  1274. unix_state_unlock(old_peer);
  1275. }
  1276. sock_put(old_peer);
  1277. } else {
  1278. unix_peer(sk) = other;
  1279. unix_state_double_unlock(sk, other);
  1280. }
  1281. return 0;
  1282. out_unlock:
  1283. unix_state_double_unlock(sk, other);
  1284. sock_put(other);
  1285. out:
  1286. return err;
  1287. }
  1288. static long unix_wait_for_peer(struct sock *other, long timeo)
  1289. __releases(&unix_sk(other)->lock)
  1290. {
  1291. struct unix_sock *u = unix_sk(other);
  1292. int sched;
  1293. DEFINE_WAIT(wait);
  1294. prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
  1295. sched = !sock_flag(other, SOCK_DEAD) &&
  1296. !(other->sk_shutdown & RCV_SHUTDOWN) &&
  1297. unix_recvq_full_lockless(other);
  1298. unix_state_unlock(other);
  1299. if (sched)
  1300. timeo = schedule_timeout(timeo);
  1301. finish_wait(&u->peer_wait, &wait);
  1302. return timeo;
  1303. }
  1304. static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  1305. int addr_len, int flags)
  1306. {
  1307. struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
  1308. struct sock *sk = sock->sk, *newsk = NULL, *other = NULL;
  1309. struct unix_sock *u = unix_sk(sk), *newu, *otheru;
  1310. struct net *net = sock_net(sk);
  1311. struct sk_buff *skb = NULL;
  1312. unsigned char state;
  1313. long timeo;
  1314. int err;
  1315. err = unix_validate_addr(sunaddr, addr_len);
  1316. if (err)
  1317. goto out;
  1318. err = BPF_CGROUP_RUN_PROG_UNIX_CONNECT_LOCK(sk, uaddr, &addr_len);
  1319. if (err)
  1320. goto out;
  1321. if ((test_bit(SOCK_PASSCRED, &sock->flags) ||
  1322. test_bit(SOCK_PASSPIDFD, &sock->flags)) &&
  1323. !READ_ONCE(u->addr)) {
  1324. err = unix_autobind(sk);
  1325. if (err)
  1326. goto out;
  1327. }
  1328. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  1329. /* First of all allocate resources.
  1330. If we will make it after state is locked,
  1331. we will have to recheck all again in any case.
  1332. */
  1333. /* create new sock for complete connection */
  1334. newsk = unix_create1(net, NULL, 0, sock->type);
  1335. if (IS_ERR(newsk)) {
  1336. err = PTR_ERR(newsk);
  1337. newsk = NULL;
  1338. goto out;
  1339. }
  1340. err = -ENOMEM;
  1341. /* Allocate skb for sending to listening sock */
  1342. skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
  1343. if (skb == NULL)
  1344. goto out;
  1345. restart:
  1346. /* Find listening sock. */
  1347. other = unix_find_other(net, sunaddr, addr_len, sk->sk_type);
  1348. if (IS_ERR(other)) {
  1349. err = PTR_ERR(other);
  1350. other = NULL;
  1351. goto out;
  1352. }
  1353. unix_state_lock(other);
  1354. /* Apparently VFS overslept socket death. Retry. */
  1355. if (sock_flag(other, SOCK_DEAD)) {
  1356. unix_state_unlock(other);
  1357. sock_put(other);
  1358. goto restart;
  1359. }
  1360. err = -ECONNREFUSED;
  1361. if (other->sk_state != TCP_LISTEN)
  1362. goto out_unlock;
  1363. if (other->sk_shutdown & RCV_SHUTDOWN)
  1364. goto out_unlock;
  1365. if (unix_recvq_full_lockless(other)) {
  1366. err = -EAGAIN;
  1367. if (!timeo)
  1368. goto out_unlock;
  1369. timeo = unix_wait_for_peer(other, timeo);
  1370. err = sock_intr_errno(timeo);
  1371. if (signal_pending(current))
  1372. goto out;
  1373. sock_put(other);
  1374. goto restart;
  1375. }
  1376. /* self connect and simultaneous connect are eliminated
  1377. * by rejecting TCP_LISTEN socket to avoid deadlock.
  1378. */
  1379. state = READ_ONCE(sk->sk_state);
  1380. if (unlikely(state != TCP_CLOSE)) {
  1381. err = state == TCP_ESTABLISHED ? -EISCONN : -EINVAL;
  1382. goto out_unlock;
  1383. }
  1384. unix_state_lock(sk);
  1385. if (unlikely(sk->sk_state != TCP_CLOSE)) {
  1386. err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EINVAL;
  1387. unix_state_unlock(sk);
  1388. goto out_unlock;
  1389. }
  1390. err = security_unix_stream_connect(sk, other, newsk);
  1391. if (err) {
  1392. unix_state_unlock(sk);
  1393. goto out_unlock;
  1394. }
  1395. /* The way is open! Fastly set all the necessary fields... */
  1396. sock_hold(sk);
  1397. unix_peer(newsk) = sk;
  1398. newsk->sk_state = TCP_ESTABLISHED;
  1399. newsk->sk_type = sk->sk_type;
  1400. init_peercred(newsk);
  1401. newu = unix_sk(newsk);
  1402. newu->listener = other;
  1403. RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
  1404. otheru = unix_sk(other);
  1405. /* copy address information from listening to new sock
  1406. *
  1407. * The contents of *(otheru->addr) and otheru->path
  1408. * are seen fully set up here, since we have found
  1409. * otheru in hash under its lock. Insertion into the
  1410. * hash chain we'd found it in had been done in an
  1411. * earlier critical area protected by the chain's lock,
  1412. * the same one where we'd set *(otheru->addr) contents,
  1413. * as well as otheru->path and otheru->addr itself.
  1414. *
  1415. * Using smp_store_release() here to set newu->addr
  1416. * is enough to make those stores, as well as stores
  1417. * to newu->path visible to anyone who gets newu->addr
  1418. * by smp_load_acquire(). IOW, the same warranties
  1419. * as for unix_sock instances bound in unix_bind() or
  1420. * in unix_autobind().
  1421. */
  1422. if (otheru->path.dentry) {
  1423. path_get(&otheru->path);
  1424. newu->path = otheru->path;
  1425. }
  1426. refcount_inc(&otheru->addr->refcnt);
  1427. smp_store_release(&newu->addr, otheru->addr);
  1428. /* Set credentials */
  1429. copy_peercred(sk, other);
  1430. sock->state = SS_CONNECTED;
  1431. WRITE_ONCE(sk->sk_state, TCP_ESTABLISHED);
  1432. sock_hold(newsk);
  1433. smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
  1434. unix_peer(sk) = newsk;
  1435. unix_state_unlock(sk);
  1436. /* take ten and send info to listening sock */
  1437. spin_lock(&other->sk_receive_queue.lock);
  1438. __skb_queue_tail(&other->sk_receive_queue, skb);
  1439. spin_unlock(&other->sk_receive_queue.lock);
  1440. unix_state_unlock(other);
  1441. other->sk_data_ready(other);
  1442. sock_put(other);
  1443. return 0;
  1444. out_unlock:
  1445. if (other)
  1446. unix_state_unlock(other);
  1447. out:
  1448. kfree_skb(skb);
  1449. if (newsk)
  1450. unix_release_sock(newsk, 0);
  1451. if (other)
  1452. sock_put(other);
  1453. return err;
  1454. }
  1455. static int unix_socketpair(struct socket *socka, struct socket *sockb)
  1456. {
  1457. struct sock *ska = socka->sk, *skb = sockb->sk;
  1458. /* Join our sockets back to back */
  1459. sock_hold(ska);
  1460. sock_hold(skb);
  1461. unix_peer(ska) = skb;
  1462. unix_peer(skb) = ska;
  1463. init_peercred(ska);
  1464. init_peercred(skb);
  1465. ska->sk_state = TCP_ESTABLISHED;
  1466. skb->sk_state = TCP_ESTABLISHED;
  1467. socka->state = SS_CONNECTED;
  1468. sockb->state = SS_CONNECTED;
  1469. return 0;
  1470. }
  1471. static void unix_sock_inherit_flags(const struct socket *old,
  1472. struct socket *new)
  1473. {
  1474. if (test_bit(SOCK_PASSCRED, &old->flags))
  1475. set_bit(SOCK_PASSCRED, &new->flags);
  1476. if (test_bit(SOCK_PASSPIDFD, &old->flags))
  1477. set_bit(SOCK_PASSPIDFD, &new->flags);
  1478. if (test_bit(SOCK_PASSSEC, &old->flags))
  1479. set_bit(SOCK_PASSSEC, &new->flags);
  1480. }
  1481. static int unix_accept(struct socket *sock, struct socket *newsock,
  1482. struct proto_accept_arg *arg)
  1483. {
  1484. struct sock *sk = sock->sk;
  1485. struct sk_buff *skb;
  1486. struct sock *tsk;
  1487. arg->err = -EOPNOTSUPP;
  1488. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  1489. goto out;
  1490. arg->err = -EINVAL;
  1491. if (READ_ONCE(sk->sk_state) != TCP_LISTEN)
  1492. goto out;
  1493. /* If socket state is TCP_LISTEN it cannot change (for now...),
  1494. * so that no locks are necessary.
  1495. */
  1496. skb = skb_recv_datagram(sk, (arg->flags & O_NONBLOCK) ? MSG_DONTWAIT : 0,
  1497. &arg->err);
  1498. if (!skb) {
  1499. /* This means receive shutdown. */
  1500. if (arg->err == 0)
  1501. arg->err = -EINVAL;
  1502. goto out;
  1503. }
  1504. tsk = skb->sk;
  1505. skb_free_datagram(sk, skb);
  1506. wake_up_interruptible(&unix_sk(sk)->peer_wait);
  1507. /* attach accepted sock to socket */
  1508. unix_state_lock(tsk);
  1509. unix_update_edges(unix_sk(tsk));
  1510. newsock->state = SS_CONNECTED;
  1511. unix_sock_inherit_flags(sock, newsock);
  1512. sock_graft(tsk, newsock);
  1513. unix_state_unlock(tsk);
  1514. return 0;
  1515. out:
  1516. return arg->err;
  1517. }
  1518. static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
  1519. {
  1520. struct sock *sk = sock->sk;
  1521. struct unix_address *addr;
  1522. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr);
  1523. int err = 0;
  1524. if (peer) {
  1525. sk = unix_peer_get(sk);
  1526. err = -ENOTCONN;
  1527. if (!sk)
  1528. goto out;
  1529. err = 0;
  1530. } else {
  1531. sock_hold(sk);
  1532. }
  1533. addr = smp_load_acquire(&unix_sk(sk)->addr);
  1534. if (!addr) {
  1535. sunaddr->sun_family = AF_UNIX;
  1536. sunaddr->sun_path[0] = 0;
  1537. err = offsetof(struct sockaddr_un, sun_path);
  1538. } else {
  1539. err = addr->len;
  1540. memcpy(sunaddr, addr->name, addr->len);
  1541. if (peer)
  1542. BPF_CGROUP_RUN_SA_PROG(sk, uaddr, &err,
  1543. CGROUP_UNIX_GETPEERNAME);
  1544. else
  1545. BPF_CGROUP_RUN_SA_PROG(sk, uaddr, &err,
  1546. CGROUP_UNIX_GETSOCKNAME);
  1547. }
  1548. sock_put(sk);
  1549. out:
  1550. return err;
  1551. }
  1552. /* The "user->unix_inflight" variable is protected by the garbage
  1553. * collection lock, and we just read it locklessly here. If you go
  1554. * over the limit, there might be a tiny race in actually noticing
  1555. * it across threads. Tough.
  1556. */
  1557. static inline bool too_many_unix_fds(struct task_struct *p)
  1558. {
  1559. struct user_struct *user = current_user();
  1560. if (unlikely(READ_ONCE(user->unix_inflight) > task_rlimit(p, RLIMIT_NOFILE)))
  1561. return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
  1562. return false;
  1563. }
  1564. static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1565. {
  1566. if (too_many_unix_fds(current))
  1567. return -ETOOMANYREFS;
  1568. UNIXCB(skb).fp = scm->fp;
  1569. scm->fp = NULL;
  1570. if (unix_prepare_fpl(UNIXCB(skb).fp))
  1571. return -ENOMEM;
  1572. return 0;
  1573. }
  1574. static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1575. {
  1576. scm->fp = UNIXCB(skb).fp;
  1577. UNIXCB(skb).fp = NULL;
  1578. unix_destroy_fpl(scm->fp);
  1579. }
  1580. static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)
  1581. {
  1582. scm->fp = scm_fp_dup(UNIXCB(skb).fp);
  1583. }
  1584. static void unix_destruct_scm(struct sk_buff *skb)
  1585. {
  1586. struct scm_cookie scm;
  1587. memset(&scm, 0, sizeof(scm));
  1588. scm.pid = UNIXCB(skb).pid;
  1589. if (UNIXCB(skb).fp)
  1590. unix_detach_fds(&scm, skb);
  1591. /* Alas, it calls VFS */
  1592. /* So fscking what? fput() had been SMP-safe since the last Summer */
  1593. scm_destroy(&scm);
  1594. sock_wfree(skb);
  1595. }
  1596. static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
  1597. {
  1598. int err = 0;
  1599. UNIXCB(skb).pid = get_pid(scm->pid);
  1600. UNIXCB(skb).uid = scm->creds.uid;
  1601. UNIXCB(skb).gid = scm->creds.gid;
  1602. UNIXCB(skb).fp = NULL;
  1603. unix_get_secdata(scm, skb);
  1604. if (scm->fp && send_fds)
  1605. err = unix_attach_fds(scm, skb);
  1606. skb->destructor = unix_destruct_scm;
  1607. return err;
  1608. }
  1609. static bool unix_passcred_enabled(const struct socket *sock,
  1610. const struct sock *other)
  1611. {
  1612. return test_bit(SOCK_PASSCRED, &sock->flags) ||
  1613. test_bit(SOCK_PASSPIDFD, &sock->flags) ||
  1614. !other->sk_socket ||
  1615. test_bit(SOCK_PASSCRED, &other->sk_socket->flags) ||
  1616. test_bit(SOCK_PASSPIDFD, &other->sk_socket->flags);
  1617. }
  1618. /*
  1619. * Some apps rely on write() giving SCM_CREDENTIALS
  1620. * We include credentials if source or destination socket
  1621. * asserted SOCK_PASSCRED.
  1622. */
  1623. static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
  1624. const struct sock *other)
  1625. {
  1626. if (UNIXCB(skb).pid)
  1627. return;
  1628. if (unix_passcred_enabled(sock, other)) {
  1629. UNIXCB(skb).pid = get_pid(task_tgid(current));
  1630. current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
  1631. }
  1632. }
  1633. static bool unix_skb_scm_eq(struct sk_buff *skb,
  1634. struct scm_cookie *scm)
  1635. {
  1636. return UNIXCB(skb).pid == scm->pid &&
  1637. uid_eq(UNIXCB(skb).uid, scm->creds.uid) &&
  1638. gid_eq(UNIXCB(skb).gid, scm->creds.gid) &&
  1639. unix_secdata_eq(scm, skb);
  1640. }
  1641. static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
  1642. {
  1643. struct scm_fp_list *fp = UNIXCB(skb).fp;
  1644. struct unix_sock *u = unix_sk(sk);
  1645. if (unlikely(fp && fp->count)) {
  1646. atomic_add(fp->count, &u->scm_stat.nr_fds);
  1647. unix_add_edges(fp, u);
  1648. }
  1649. }
  1650. static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
  1651. {
  1652. struct scm_fp_list *fp = UNIXCB(skb).fp;
  1653. struct unix_sock *u = unix_sk(sk);
  1654. if (unlikely(fp && fp->count)) {
  1655. atomic_sub(fp->count, &u->scm_stat.nr_fds);
  1656. unix_del_edges(fp);
  1657. }
  1658. }
  1659. /*
  1660. * Send AF_UNIX data.
  1661. */
  1662. static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
  1663. size_t len)
  1664. {
  1665. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
  1666. struct sock *sk = sock->sk, *other = NULL;
  1667. struct unix_sock *u = unix_sk(sk);
  1668. struct scm_cookie scm;
  1669. struct sk_buff *skb;
  1670. int data_len = 0;
  1671. int sk_locked;
  1672. long timeo;
  1673. int err;
  1674. err = scm_send(sock, msg, &scm, false);
  1675. if (err < 0)
  1676. return err;
  1677. wait_for_unix_gc(scm.fp);
  1678. err = -EOPNOTSUPP;
  1679. if (msg->msg_flags&MSG_OOB)
  1680. goto out;
  1681. if (msg->msg_namelen) {
  1682. err = unix_validate_addr(sunaddr, msg->msg_namelen);
  1683. if (err)
  1684. goto out;
  1685. err = BPF_CGROUP_RUN_PROG_UNIX_SENDMSG_LOCK(sk,
  1686. msg->msg_name,
  1687. &msg->msg_namelen,
  1688. NULL);
  1689. if (err)
  1690. goto out;
  1691. } else {
  1692. sunaddr = NULL;
  1693. err = -ENOTCONN;
  1694. other = unix_peer_get(sk);
  1695. if (!other)
  1696. goto out;
  1697. }
  1698. if ((test_bit(SOCK_PASSCRED, &sock->flags) ||
  1699. test_bit(SOCK_PASSPIDFD, &sock->flags)) &&
  1700. !READ_ONCE(u->addr)) {
  1701. err = unix_autobind(sk);
  1702. if (err)
  1703. goto out;
  1704. }
  1705. err = -EMSGSIZE;
  1706. if (len > READ_ONCE(sk->sk_sndbuf) - 32)
  1707. goto out;
  1708. if (len > SKB_MAX_ALLOC) {
  1709. data_len = min_t(size_t,
  1710. len - SKB_MAX_ALLOC,
  1711. MAX_SKB_FRAGS * PAGE_SIZE);
  1712. data_len = PAGE_ALIGN(data_len);
  1713. BUILD_BUG_ON(SKB_MAX_ALLOC < PAGE_SIZE);
  1714. }
  1715. skb = sock_alloc_send_pskb(sk, len - data_len, data_len,
  1716. msg->msg_flags & MSG_DONTWAIT, &err,
  1717. PAGE_ALLOC_COSTLY_ORDER);
  1718. if (skb == NULL)
  1719. goto out;
  1720. err = unix_scm_to_skb(&scm, skb, true);
  1721. if (err < 0)
  1722. goto out_free;
  1723. skb_put(skb, len - data_len);
  1724. skb->data_len = data_len;
  1725. skb->len = len;
  1726. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
  1727. if (err)
  1728. goto out_free;
  1729. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1730. restart:
  1731. if (!other) {
  1732. err = -ECONNRESET;
  1733. if (sunaddr == NULL)
  1734. goto out_free;
  1735. other = unix_find_other(sock_net(sk), sunaddr, msg->msg_namelen,
  1736. sk->sk_type);
  1737. if (IS_ERR(other)) {
  1738. err = PTR_ERR(other);
  1739. other = NULL;
  1740. goto out_free;
  1741. }
  1742. }
  1743. if (sk_filter(other, skb) < 0) {
  1744. /* Toss the packet but do not return any error to the sender */
  1745. err = len;
  1746. goto out_free;
  1747. }
  1748. sk_locked = 0;
  1749. unix_state_lock(other);
  1750. restart_locked:
  1751. err = -EPERM;
  1752. if (!unix_may_send(sk, other))
  1753. goto out_unlock;
  1754. if (unlikely(sock_flag(other, SOCK_DEAD))) {
  1755. /*
  1756. * Check with 1003.1g - what should
  1757. * datagram error
  1758. */
  1759. unix_state_unlock(other);
  1760. sock_put(other);
  1761. if (!sk_locked)
  1762. unix_state_lock(sk);
  1763. err = 0;
  1764. if (sk->sk_type == SOCK_SEQPACKET) {
  1765. /* We are here only when racing with unix_release_sock()
  1766. * is clearing @other. Never change state to TCP_CLOSE
  1767. * unlike SOCK_DGRAM wants.
  1768. */
  1769. unix_state_unlock(sk);
  1770. err = -EPIPE;
  1771. } else if (unix_peer(sk) == other) {
  1772. unix_peer(sk) = NULL;
  1773. unix_dgram_peer_wake_disconnect_wakeup(sk, other);
  1774. WRITE_ONCE(sk->sk_state, TCP_CLOSE);
  1775. unix_state_unlock(sk);
  1776. unix_dgram_disconnected(sk, other);
  1777. sock_put(other);
  1778. err = -ECONNREFUSED;
  1779. } else {
  1780. unix_state_unlock(sk);
  1781. }
  1782. other = NULL;
  1783. if (err)
  1784. goto out_free;
  1785. goto restart;
  1786. }
  1787. err = -EPIPE;
  1788. if (other->sk_shutdown & RCV_SHUTDOWN)
  1789. goto out_unlock;
  1790. if (sk->sk_type != SOCK_SEQPACKET) {
  1791. err = security_unix_may_send(sk->sk_socket, other->sk_socket);
  1792. if (err)
  1793. goto out_unlock;
  1794. }
  1795. /* other == sk && unix_peer(other) != sk if
  1796. * - unix_peer(sk) == NULL, destination address bound to sk
  1797. * - unix_peer(sk) == sk by time of get but disconnected before lock
  1798. */
  1799. if (other != sk &&
  1800. unlikely(unix_peer(other) != sk &&
  1801. unix_recvq_full_lockless(other))) {
  1802. if (timeo) {
  1803. timeo = unix_wait_for_peer(other, timeo);
  1804. err = sock_intr_errno(timeo);
  1805. if (signal_pending(current))
  1806. goto out_free;
  1807. goto restart;
  1808. }
  1809. if (!sk_locked) {
  1810. unix_state_unlock(other);
  1811. unix_state_double_lock(sk, other);
  1812. }
  1813. if (unix_peer(sk) != other ||
  1814. unix_dgram_peer_wake_me(sk, other)) {
  1815. err = -EAGAIN;
  1816. sk_locked = 1;
  1817. goto out_unlock;
  1818. }
  1819. if (!sk_locked) {
  1820. sk_locked = 1;
  1821. goto restart_locked;
  1822. }
  1823. }
  1824. if (unlikely(sk_locked))
  1825. unix_state_unlock(sk);
  1826. if (sock_flag(other, SOCK_RCVTSTAMP))
  1827. __net_timestamp(skb);
  1828. maybe_add_creds(skb, sock, other);
  1829. scm_stat_add(other, skb);
  1830. skb_queue_tail(&other->sk_receive_queue, skb);
  1831. unix_state_unlock(other);
  1832. other->sk_data_ready(other);
  1833. sock_put(other);
  1834. scm_destroy(&scm);
  1835. return len;
  1836. out_unlock:
  1837. if (sk_locked)
  1838. unix_state_unlock(sk);
  1839. unix_state_unlock(other);
  1840. out_free:
  1841. kfree_skb(skb);
  1842. out:
  1843. if (other)
  1844. sock_put(other);
  1845. scm_destroy(&scm);
  1846. return err;
  1847. }
  1848. /* We use paged skbs for stream sockets, and limit occupancy to 32768
  1849. * bytes, and a minimum of a full page.
  1850. */
  1851. #define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768))
  1852. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  1853. static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other,
  1854. struct scm_cookie *scm, bool fds_sent)
  1855. {
  1856. struct unix_sock *ousk = unix_sk(other);
  1857. struct sk_buff *skb;
  1858. int err = 0;
  1859. skb = sock_alloc_send_skb(sock->sk, 1, msg->msg_flags & MSG_DONTWAIT, &err);
  1860. if (!skb)
  1861. return err;
  1862. err = unix_scm_to_skb(scm, skb, !fds_sent);
  1863. if (err < 0) {
  1864. kfree_skb(skb);
  1865. return err;
  1866. }
  1867. skb_put(skb, 1);
  1868. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, 1);
  1869. if (err) {
  1870. kfree_skb(skb);
  1871. return err;
  1872. }
  1873. unix_state_lock(other);
  1874. if (sock_flag(other, SOCK_DEAD) ||
  1875. (other->sk_shutdown & RCV_SHUTDOWN)) {
  1876. unix_state_unlock(other);
  1877. kfree_skb(skb);
  1878. return -EPIPE;
  1879. }
  1880. maybe_add_creds(skb, sock, other);
  1881. scm_stat_add(other, skb);
  1882. spin_lock(&other->sk_receive_queue.lock);
  1883. WRITE_ONCE(ousk->oob_skb, skb);
  1884. __skb_queue_tail(&other->sk_receive_queue, skb);
  1885. spin_unlock(&other->sk_receive_queue.lock);
  1886. sk_send_sigurg(other);
  1887. unix_state_unlock(other);
  1888. other->sk_data_ready(other);
  1889. return err;
  1890. }
  1891. #endif
  1892. static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
  1893. size_t len)
  1894. {
  1895. struct sock *sk = sock->sk;
  1896. struct sock *other = NULL;
  1897. int err, size;
  1898. struct sk_buff *skb;
  1899. int sent = 0;
  1900. struct scm_cookie scm;
  1901. bool fds_sent = false;
  1902. int data_len;
  1903. err = scm_send(sock, msg, &scm, false);
  1904. if (err < 0)
  1905. return err;
  1906. wait_for_unix_gc(scm.fp);
  1907. err = -EOPNOTSUPP;
  1908. if (msg->msg_flags & MSG_OOB) {
  1909. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  1910. if (len)
  1911. len--;
  1912. else
  1913. #endif
  1914. goto out_err;
  1915. }
  1916. if (msg->msg_namelen) {
  1917. err = READ_ONCE(sk->sk_state) == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
  1918. goto out_err;
  1919. } else {
  1920. err = -ENOTCONN;
  1921. other = unix_peer(sk);
  1922. if (!other)
  1923. goto out_err;
  1924. }
  1925. if (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN)
  1926. goto pipe_err;
  1927. while (sent < len) {
  1928. size = len - sent;
  1929. if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) {
  1930. skb = sock_alloc_send_pskb(sk, 0, 0,
  1931. msg->msg_flags & MSG_DONTWAIT,
  1932. &err, 0);
  1933. } else {
  1934. /* Keep two messages in the pipe so it schedules better */
  1935. size = min_t(int, size, (READ_ONCE(sk->sk_sndbuf) >> 1) - 64);
  1936. /* allow fallback to order-0 allocations */
  1937. size = min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ);
  1938. data_len = max_t(int, 0, size - SKB_MAX_HEAD(0));
  1939. data_len = min_t(size_t, size, PAGE_ALIGN(data_len));
  1940. skb = sock_alloc_send_pskb(sk, size - data_len, data_len,
  1941. msg->msg_flags & MSG_DONTWAIT, &err,
  1942. get_order(UNIX_SKB_FRAGS_SZ));
  1943. }
  1944. if (!skb)
  1945. goto out_err;
  1946. /* Only send the fds in the first buffer */
  1947. err = unix_scm_to_skb(&scm, skb, !fds_sent);
  1948. if (err < 0) {
  1949. kfree_skb(skb);
  1950. goto out_err;
  1951. }
  1952. fds_sent = true;
  1953. if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) {
  1954. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1955. err = skb_splice_from_iter(skb, &msg->msg_iter, size,
  1956. sk->sk_allocation);
  1957. if (err < 0) {
  1958. kfree_skb(skb);
  1959. goto out_err;
  1960. }
  1961. size = err;
  1962. refcount_add(size, &sk->sk_wmem_alloc);
  1963. } else {
  1964. skb_put(skb, size - data_len);
  1965. skb->data_len = data_len;
  1966. skb->len = size;
  1967. err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
  1968. if (err) {
  1969. kfree_skb(skb);
  1970. goto out_err;
  1971. }
  1972. }
  1973. unix_state_lock(other);
  1974. if (sock_flag(other, SOCK_DEAD) ||
  1975. (other->sk_shutdown & RCV_SHUTDOWN))
  1976. goto pipe_err_free;
  1977. maybe_add_creds(skb, sock, other);
  1978. scm_stat_add(other, skb);
  1979. skb_queue_tail(&other->sk_receive_queue, skb);
  1980. unix_state_unlock(other);
  1981. other->sk_data_ready(other);
  1982. sent += size;
  1983. }
  1984. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  1985. if (msg->msg_flags & MSG_OOB) {
  1986. err = queue_oob(sock, msg, other, &scm, fds_sent);
  1987. if (err)
  1988. goto out_err;
  1989. sent++;
  1990. }
  1991. #endif
  1992. scm_destroy(&scm);
  1993. return sent;
  1994. pipe_err_free:
  1995. unix_state_unlock(other);
  1996. kfree_skb(skb);
  1997. pipe_err:
  1998. if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
  1999. send_sig(SIGPIPE, current, 0);
  2000. err = -EPIPE;
  2001. out_err:
  2002. scm_destroy(&scm);
  2003. return sent ? : err;
  2004. }
  2005. static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
  2006. size_t len)
  2007. {
  2008. int err;
  2009. struct sock *sk = sock->sk;
  2010. err = sock_error(sk);
  2011. if (err)
  2012. return err;
  2013. if (READ_ONCE(sk->sk_state) != TCP_ESTABLISHED)
  2014. return -ENOTCONN;
  2015. if (msg->msg_namelen)
  2016. msg->msg_namelen = 0;
  2017. return unix_dgram_sendmsg(sock, msg, len);
  2018. }
  2019. static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
  2020. size_t size, int flags)
  2021. {
  2022. struct sock *sk = sock->sk;
  2023. if (READ_ONCE(sk->sk_state) != TCP_ESTABLISHED)
  2024. return -ENOTCONN;
  2025. return unix_dgram_recvmsg(sock, msg, size, flags);
  2026. }
  2027. static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
  2028. {
  2029. struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr);
  2030. if (addr) {
  2031. msg->msg_namelen = addr->len;
  2032. memcpy(msg->msg_name, addr->name, addr->len);
  2033. }
  2034. }
  2035. int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
  2036. int flags)
  2037. {
  2038. struct scm_cookie scm;
  2039. struct socket *sock = sk->sk_socket;
  2040. struct unix_sock *u = unix_sk(sk);
  2041. struct sk_buff *skb, *last;
  2042. long timeo;
  2043. int skip;
  2044. int err;
  2045. err = -EOPNOTSUPP;
  2046. if (flags&MSG_OOB)
  2047. goto out;
  2048. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  2049. do {
  2050. mutex_lock(&u->iolock);
  2051. skip = sk_peek_offset(sk, flags);
  2052. skb = __skb_try_recv_datagram(sk, &sk->sk_receive_queue, flags,
  2053. &skip, &err, &last);
  2054. if (skb) {
  2055. if (!(flags & MSG_PEEK))
  2056. scm_stat_del(sk, skb);
  2057. break;
  2058. }
  2059. mutex_unlock(&u->iolock);
  2060. if (err != -EAGAIN)
  2061. break;
  2062. } while (timeo &&
  2063. !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue,
  2064. &err, &timeo, last));
  2065. if (!skb) { /* implies iolock unlocked */
  2066. unix_state_lock(sk);
  2067. /* Signal EOF on disconnected non-blocking SEQPACKET socket. */
  2068. if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
  2069. (sk->sk_shutdown & RCV_SHUTDOWN))
  2070. err = 0;
  2071. unix_state_unlock(sk);
  2072. goto out;
  2073. }
  2074. if (wq_has_sleeper(&u->peer_wait))
  2075. wake_up_interruptible_sync_poll(&u->peer_wait,
  2076. EPOLLOUT | EPOLLWRNORM |
  2077. EPOLLWRBAND);
  2078. if (msg->msg_name) {
  2079. unix_copy_addr(msg, skb->sk);
  2080. BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
  2081. msg->msg_name,
  2082. &msg->msg_namelen);
  2083. }
  2084. if (size > skb->len - skip)
  2085. size = skb->len - skip;
  2086. else if (size < skb->len - skip)
  2087. msg->msg_flags |= MSG_TRUNC;
  2088. err = skb_copy_datagram_msg(skb, skip, msg, size);
  2089. if (err)
  2090. goto out_free;
  2091. if (sock_flag(sk, SOCK_RCVTSTAMP))
  2092. __sock_recv_timestamp(msg, sk, skb);
  2093. memset(&scm, 0, sizeof(scm));
  2094. scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
  2095. unix_set_secdata(&scm, skb);
  2096. if (!(flags & MSG_PEEK)) {
  2097. if (UNIXCB(skb).fp)
  2098. unix_detach_fds(&scm, skb);
  2099. sk_peek_offset_bwd(sk, skb->len);
  2100. } else {
  2101. /* It is questionable: on PEEK we could:
  2102. - do not return fds - good, but too simple 8)
  2103. - return fds, and do not return them on read (old strategy,
  2104. apparently wrong)
  2105. - clone fds (I chose it for now, it is the most universal
  2106. solution)
  2107. POSIX 1003.1g does not actually define this clearly
  2108. at all. POSIX 1003.1g doesn't define a lot of things
  2109. clearly however!
  2110. */
  2111. sk_peek_offset_fwd(sk, size);
  2112. if (UNIXCB(skb).fp)
  2113. unix_peek_fds(&scm, skb);
  2114. }
  2115. err = (flags & MSG_TRUNC) ? skb->len - skip : size;
  2116. scm_recv_unix(sock, msg, &scm, flags);
  2117. out_free:
  2118. skb_free_datagram(sk, skb);
  2119. mutex_unlock(&u->iolock);
  2120. out:
  2121. return err;
  2122. }
  2123. static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  2124. int flags)
  2125. {
  2126. struct sock *sk = sock->sk;
  2127. #ifdef CONFIG_BPF_SYSCALL
  2128. const struct proto *prot = READ_ONCE(sk->sk_prot);
  2129. if (prot != &unix_dgram_proto)
  2130. return prot->recvmsg(sk, msg, size, flags, NULL);
  2131. #endif
  2132. return __unix_dgram_recvmsg(sk, msg, size, flags);
  2133. }
  2134. static int unix_read_skb(struct sock *sk, skb_read_actor_t recv_actor)
  2135. {
  2136. struct unix_sock *u = unix_sk(sk);
  2137. struct sk_buff *skb;
  2138. int err;
  2139. mutex_lock(&u->iolock);
  2140. skb = skb_recv_datagram(sk, MSG_DONTWAIT, &err);
  2141. mutex_unlock(&u->iolock);
  2142. if (!skb)
  2143. return err;
  2144. return recv_actor(sk, skb);
  2145. }
  2146. /*
  2147. * Sleep until more data has arrived. But check for races..
  2148. */
  2149. static long unix_stream_data_wait(struct sock *sk, long timeo,
  2150. struct sk_buff *last, unsigned int last_len,
  2151. bool freezable)
  2152. {
  2153. unsigned int state = TASK_INTERRUPTIBLE | freezable * TASK_FREEZABLE;
  2154. struct sk_buff *tail;
  2155. DEFINE_WAIT(wait);
  2156. unix_state_lock(sk);
  2157. for (;;) {
  2158. prepare_to_wait(sk_sleep(sk), &wait, state);
  2159. tail = skb_peek_tail(&sk->sk_receive_queue);
  2160. if (tail != last ||
  2161. (tail && tail->len != last_len) ||
  2162. sk->sk_err ||
  2163. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  2164. signal_pending(current) ||
  2165. !timeo)
  2166. break;
  2167. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  2168. unix_state_unlock(sk);
  2169. timeo = schedule_timeout(timeo);
  2170. unix_state_lock(sk);
  2171. if (sock_flag(sk, SOCK_DEAD))
  2172. break;
  2173. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  2174. }
  2175. finish_wait(sk_sleep(sk), &wait);
  2176. unix_state_unlock(sk);
  2177. return timeo;
  2178. }
  2179. struct unix_stream_read_state {
  2180. int (*recv_actor)(struct sk_buff *, int, int,
  2181. struct unix_stream_read_state *);
  2182. struct socket *socket;
  2183. struct msghdr *msg;
  2184. struct pipe_inode_info *pipe;
  2185. size_t size;
  2186. int flags;
  2187. unsigned int splice_flags;
  2188. };
  2189. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  2190. static int unix_stream_recv_urg(struct unix_stream_read_state *state)
  2191. {
  2192. struct sk_buff *oob_skb, *read_skb = NULL;
  2193. struct socket *sock = state->socket;
  2194. struct sock *sk = sock->sk;
  2195. struct unix_sock *u = unix_sk(sk);
  2196. int chunk = 1;
  2197. mutex_lock(&u->iolock);
  2198. unix_state_lock(sk);
  2199. spin_lock(&sk->sk_receive_queue.lock);
  2200. if (sock_flag(sk, SOCK_URGINLINE) || !u->oob_skb) {
  2201. spin_unlock(&sk->sk_receive_queue.lock);
  2202. unix_state_unlock(sk);
  2203. mutex_unlock(&u->iolock);
  2204. return -EINVAL;
  2205. }
  2206. oob_skb = u->oob_skb;
  2207. if (!(state->flags & MSG_PEEK)) {
  2208. WRITE_ONCE(u->oob_skb, NULL);
  2209. if (oob_skb->prev != (struct sk_buff *)&sk->sk_receive_queue &&
  2210. !unix_skb_len(oob_skb->prev)) {
  2211. read_skb = oob_skb->prev;
  2212. __skb_unlink(read_skb, &sk->sk_receive_queue);
  2213. }
  2214. }
  2215. spin_unlock(&sk->sk_receive_queue.lock);
  2216. unix_state_unlock(sk);
  2217. chunk = state->recv_actor(oob_skb, 0, chunk, state);
  2218. if (!(state->flags & MSG_PEEK))
  2219. UNIXCB(oob_skb).consumed += 1;
  2220. mutex_unlock(&u->iolock);
  2221. consume_skb(read_skb);
  2222. if (chunk < 0)
  2223. return -EFAULT;
  2224. state->msg->msg_flags |= MSG_OOB;
  2225. return 1;
  2226. }
  2227. static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
  2228. int flags, int copied)
  2229. {
  2230. struct sk_buff *read_skb = NULL, *unread_skb = NULL;
  2231. struct unix_sock *u = unix_sk(sk);
  2232. if (likely(unix_skb_len(skb) && skb != READ_ONCE(u->oob_skb)))
  2233. return skb;
  2234. spin_lock(&sk->sk_receive_queue.lock);
  2235. if (!unix_skb_len(skb)) {
  2236. if (copied && (!u->oob_skb || skb == u->oob_skb)) {
  2237. skb = NULL;
  2238. } else if (flags & MSG_PEEK) {
  2239. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2240. } else {
  2241. read_skb = skb;
  2242. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2243. __skb_unlink(read_skb, &sk->sk_receive_queue);
  2244. }
  2245. if (!skb)
  2246. goto unlock;
  2247. }
  2248. if (skb != u->oob_skb)
  2249. goto unlock;
  2250. if (copied) {
  2251. skb = NULL;
  2252. } else if (!(flags & MSG_PEEK)) {
  2253. WRITE_ONCE(u->oob_skb, NULL);
  2254. if (!sock_flag(sk, SOCK_URGINLINE)) {
  2255. __skb_unlink(skb, &sk->sk_receive_queue);
  2256. unread_skb = skb;
  2257. skb = skb_peek(&sk->sk_receive_queue);
  2258. }
  2259. } else if (!sock_flag(sk, SOCK_URGINLINE)) {
  2260. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2261. }
  2262. unlock:
  2263. spin_unlock(&sk->sk_receive_queue.lock);
  2264. consume_skb(read_skb);
  2265. kfree_skb(unread_skb);
  2266. return skb;
  2267. }
  2268. #endif
  2269. static int unix_stream_read_skb(struct sock *sk, skb_read_actor_t recv_actor)
  2270. {
  2271. struct unix_sock *u = unix_sk(sk);
  2272. struct sk_buff *skb;
  2273. int err;
  2274. if (unlikely(READ_ONCE(sk->sk_state) != TCP_ESTABLISHED))
  2275. return -ENOTCONN;
  2276. mutex_lock(&u->iolock);
  2277. skb = skb_recv_datagram(sk, MSG_DONTWAIT, &err);
  2278. mutex_unlock(&u->iolock);
  2279. if (!skb)
  2280. return err;
  2281. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  2282. if (unlikely(skb == READ_ONCE(u->oob_skb))) {
  2283. bool drop = false;
  2284. unix_state_lock(sk);
  2285. if (sock_flag(sk, SOCK_DEAD)) {
  2286. unix_state_unlock(sk);
  2287. kfree_skb(skb);
  2288. return -ECONNRESET;
  2289. }
  2290. spin_lock(&sk->sk_receive_queue.lock);
  2291. if (likely(skb == u->oob_skb)) {
  2292. WRITE_ONCE(u->oob_skb, NULL);
  2293. drop = true;
  2294. }
  2295. spin_unlock(&sk->sk_receive_queue.lock);
  2296. unix_state_unlock(sk);
  2297. if (drop) {
  2298. kfree_skb(skb);
  2299. return -EAGAIN;
  2300. }
  2301. }
  2302. #endif
  2303. return recv_actor(sk, skb);
  2304. }
  2305. static int unix_stream_read_generic(struct unix_stream_read_state *state,
  2306. bool freezable)
  2307. {
  2308. struct scm_cookie scm;
  2309. struct socket *sock = state->socket;
  2310. struct sock *sk = sock->sk;
  2311. struct unix_sock *u = unix_sk(sk);
  2312. int copied = 0;
  2313. int flags = state->flags;
  2314. int noblock = flags & MSG_DONTWAIT;
  2315. bool check_creds = false;
  2316. int target;
  2317. int err = 0;
  2318. long timeo;
  2319. int skip;
  2320. size_t size = state->size;
  2321. unsigned int last_len;
  2322. if (unlikely(READ_ONCE(sk->sk_state) != TCP_ESTABLISHED)) {
  2323. err = -EINVAL;
  2324. goto out;
  2325. }
  2326. if (unlikely(flags & MSG_OOB)) {
  2327. err = -EOPNOTSUPP;
  2328. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  2329. err = unix_stream_recv_urg(state);
  2330. #endif
  2331. goto out;
  2332. }
  2333. target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
  2334. timeo = sock_rcvtimeo(sk, noblock);
  2335. memset(&scm, 0, sizeof(scm));
  2336. /* Lock the socket to prevent queue disordering
  2337. * while sleeps in memcpy_tomsg
  2338. */
  2339. mutex_lock(&u->iolock);
  2340. skip = max(sk_peek_offset(sk, flags), 0);
  2341. do {
  2342. struct sk_buff *skb, *last;
  2343. int chunk;
  2344. redo:
  2345. unix_state_lock(sk);
  2346. if (sock_flag(sk, SOCK_DEAD)) {
  2347. err = -ECONNRESET;
  2348. goto unlock;
  2349. }
  2350. last = skb = skb_peek(&sk->sk_receive_queue);
  2351. last_len = last ? last->len : 0;
  2352. again:
  2353. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  2354. if (skb) {
  2355. skb = manage_oob(skb, sk, flags, copied);
  2356. if (!skb && copied) {
  2357. unix_state_unlock(sk);
  2358. break;
  2359. }
  2360. }
  2361. #endif
  2362. if (skb == NULL) {
  2363. if (copied >= target)
  2364. goto unlock;
  2365. /*
  2366. * POSIX 1003.1g mandates this order.
  2367. */
  2368. err = sock_error(sk);
  2369. if (err)
  2370. goto unlock;
  2371. if (sk->sk_shutdown & RCV_SHUTDOWN)
  2372. goto unlock;
  2373. unix_state_unlock(sk);
  2374. if (!timeo) {
  2375. err = -EAGAIN;
  2376. break;
  2377. }
  2378. mutex_unlock(&u->iolock);
  2379. timeo = unix_stream_data_wait(sk, timeo, last,
  2380. last_len, freezable);
  2381. if (signal_pending(current)) {
  2382. err = sock_intr_errno(timeo);
  2383. scm_destroy(&scm);
  2384. goto out;
  2385. }
  2386. mutex_lock(&u->iolock);
  2387. goto redo;
  2388. unlock:
  2389. unix_state_unlock(sk);
  2390. break;
  2391. }
  2392. while (skip >= unix_skb_len(skb)) {
  2393. skip -= unix_skb_len(skb);
  2394. last = skb;
  2395. last_len = skb->len;
  2396. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2397. if (!skb)
  2398. goto again;
  2399. }
  2400. unix_state_unlock(sk);
  2401. if (check_creds) {
  2402. /* Never glue messages from different writers */
  2403. if (!unix_skb_scm_eq(skb, &scm))
  2404. break;
  2405. } else if (test_bit(SOCK_PASSCRED, &sock->flags) ||
  2406. test_bit(SOCK_PASSPIDFD, &sock->flags)) {
  2407. /* Copy credentials */
  2408. scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
  2409. unix_set_secdata(&scm, skb);
  2410. check_creds = true;
  2411. }
  2412. /* Copy address just once */
  2413. if (state->msg && state->msg->msg_name) {
  2414. DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr,
  2415. state->msg->msg_name);
  2416. unix_copy_addr(state->msg, skb->sk);
  2417. BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
  2418. state->msg->msg_name,
  2419. &state->msg->msg_namelen);
  2420. sunaddr = NULL;
  2421. }
  2422. chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size);
  2423. chunk = state->recv_actor(skb, skip, chunk, state);
  2424. if (chunk < 0) {
  2425. if (copied == 0)
  2426. copied = -EFAULT;
  2427. break;
  2428. }
  2429. copied += chunk;
  2430. size -= chunk;
  2431. /* Mark read part of skb as used */
  2432. if (!(flags & MSG_PEEK)) {
  2433. UNIXCB(skb).consumed += chunk;
  2434. sk_peek_offset_bwd(sk, chunk);
  2435. if (UNIXCB(skb).fp) {
  2436. scm_stat_del(sk, skb);
  2437. unix_detach_fds(&scm, skb);
  2438. }
  2439. if (unix_skb_len(skb))
  2440. break;
  2441. skb_unlink(skb, &sk->sk_receive_queue);
  2442. consume_skb(skb);
  2443. if (scm.fp)
  2444. break;
  2445. } else {
  2446. /* It is questionable, see note in unix_dgram_recvmsg.
  2447. */
  2448. if (UNIXCB(skb).fp)
  2449. unix_peek_fds(&scm, skb);
  2450. sk_peek_offset_fwd(sk, chunk);
  2451. if (UNIXCB(skb).fp)
  2452. break;
  2453. skip = 0;
  2454. last = skb;
  2455. last_len = skb->len;
  2456. unix_state_lock(sk);
  2457. skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2458. if (skb)
  2459. goto again;
  2460. unix_state_unlock(sk);
  2461. break;
  2462. }
  2463. } while (size);
  2464. mutex_unlock(&u->iolock);
  2465. if (state->msg)
  2466. scm_recv_unix(sock, state->msg, &scm, flags);
  2467. else
  2468. scm_destroy(&scm);
  2469. out:
  2470. return copied ? : err;
  2471. }
  2472. static int unix_stream_read_actor(struct sk_buff *skb,
  2473. int skip, int chunk,
  2474. struct unix_stream_read_state *state)
  2475. {
  2476. int ret;
  2477. ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,
  2478. state->msg, chunk);
  2479. return ret ?: chunk;
  2480. }
  2481. int __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg,
  2482. size_t size, int flags)
  2483. {
  2484. struct unix_stream_read_state state = {
  2485. .recv_actor = unix_stream_read_actor,
  2486. .socket = sk->sk_socket,
  2487. .msg = msg,
  2488. .size = size,
  2489. .flags = flags
  2490. };
  2491. return unix_stream_read_generic(&state, true);
  2492. }
  2493. static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
  2494. size_t size, int flags)
  2495. {
  2496. struct unix_stream_read_state state = {
  2497. .recv_actor = unix_stream_read_actor,
  2498. .socket = sock,
  2499. .msg = msg,
  2500. .size = size,
  2501. .flags = flags
  2502. };
  2503. #ifdef CONFIG_BPF_SYSCALL
  2504. struct sock *sk = sock->sk;
  2505. const struct proto *prot = READ_ONCE(sk->sk_prot);
  2506. if (prot != &unix_stream_proto)
  2507. return prot->recvmsg(sk, msg, size, flags, NULL);
  2508. #endif
  2509. return unix_stream_read_generic(&state, true);
  2510. }
  2511. static int unix_stream_splice_actor(struct sk_buff *skb,
  2512. int skip, int chunk,
  2513. struct unix_stream_read_state *state)
  2514. {
  2515. return skb_splice_bits(skb, state->socket->sk,
  2516. UNIXCB(skb).consumed + skip,
  2517. state->pipe, chunk, state->splice_flags);
  2518. }
  2519. static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos,
  2520. struct pipe_inode_info *pipe,
  2521. size_t size, unsigned int flags)
  2522. {
  2523. struct unix_stream_read_state state = {
  2524. .recv_actor = unix_stream_splice_actor,
  2525. .socket = sock,
  2526. .pipe = pipe,
  2527. .size = size,
  2528. .splice_flags = flags,
  2529. };
  2530. if (unlikely(*ppos))
  2531. return -ESPIPE;
  2532. if (sock->file->f_flags & O_NONBLOCK ||
  2533. flags & SPLICE_F_NONBLOCK)
  2534. state.flags = MSG_DONTWAIT;
  2535. return unix_stream_read_generic(&state, false);
  2536. }
  2537. static int unix_shutdown(struct socket *sock, int mode)
  2538. {
  2539. struct sock *sk = sock->sk;
  2540. struct sock *other;
  2541. if (mode < SHUT_RD || mode > SHUT_RDWR)
  2542. return -EINVAL;
  2543. /* This maps:
  2544. * SHUT_RD (0) -> RCV_SHUTDOWN (1)
  2545. * SHUT_WR (1) -> SEND_SHUTDOWN (2)
  2546. * SHUT_RDWR (2) -> SHUTDOWN_MASK (3)
  2547. */
  2548. ++mode;
  2549. unix_state_lock(sk);
  2550. WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | mode);
  2551. other = unix_peer(sk);
  2552. if (other)
  2553. sock_hold(other);
  2554. unix_state_unlock(sk);
  2555. sk->sk_state_change(sk);
  2556. if (other &&
  2557. (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
  2558. int peer_mode = 0;
  2559. const struct proto *prot = READ_ONCE(other->sk_prot);
  2560. if (prot->unhash)
  2561. prot->unhash(other);
  2562. if (mode&RCV_SHUTDOWN)
  2563. peer_mode |= SEND_SHUTDOWN;
  2564. if (mode&SEND_SHUTDOWN)
  2565. peer_mode |= RCV_SHUTDOWN;
  2566. unix_state_lock(other);
  2567. WRITE_ONCE(other->sk_shutdown, other->sk_shutdown | peer_mode);
  2568. unix_state_unlock(other);
  2569. other->sk_state_change(other);
  2570. if (peer_mode == SHUTDOWN_MASK)
  2571. sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP);
  2572. else if (peer_mode & RCV_SHUTDOWN)
  2573. sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN);
  2574. }
  2575. if (other)
  2576. sock_put(other);
  2577. return 0;
  2578. }
  2579. long unix_inq_len(struct sock *sk)
  2580. {
  2581. struct sk_buff *skb;
  2582. long amount = 0;
  2583. if (READ_ONCE(sk->sk_state) == TCP_LISTEN)
  2584. return -EINVAL;
  2585. spin_lock(&sk->sk_receive_queue.lock);
  2586. if (sk->sk_type == SOCK_STREAM ||
  2587. sk->sk_type == SOCK_SEQPACKET) {
  2588. skb_queue_walk(&sk->sk_receive_queue, skb)
  2589. amount += unix_skb_len(skb);
  2590. } else {
  2591. skb = skb_peek(&sk->sk_receive_queue);
  2592. if (skb)
  2593. amount = skb->len;
  2594. }
  2595. spin_unlock(&sk->sk_receive_queue.lock);
  2596. return amount;
  2597. }
  2598. EXPORT_SYMBOL_GPL(unix_inq_len);
  2599. long unix_outq_len(struct sock *sk)
  2600. {
  2601. return sk_wmem_alloc_get(sk);
  2602. }
  2603. EXPORT_SYMBOL_GPL(unix_outq_len);
  2604. static int unix_open_file(struct sock *sk)
  2605. {
  2606. struct path path;
  2607. struct file *f;
  2608. int fd;
  2609. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  2610. return -EPERM;
  2611. if (!smp_load_acquire(&unix_sk(sk)->addr))
  2612. return -ENOENT;
  2613. path = unix_sk(sk)->path;
  2614. if (!path.dentry)
  2615. return -ENOENT;
  2616. path_get(&path);
  2617. fd = get_unused_fd_flags(O_CLOEXEC);
  2618. if (fd < 0)
  2619. goto out;
  2620. f = dentry_open(&path, O_PATH, current_cred());
  2621. if (IS_ERR(f)) {
  2622. put_unused_fd(fd);
  2623. fd = PTR_ERR(f);
  2624. goto out;
  2625. }
  2626. fd_install(fd, f);
  2627. out:
  2628. path_put(&path);
  2629. return fd;
  2630. }
  2631. static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2632. {
  2633. struct sock *sk = sock->sk;
  2634. long amount = 0;
  2635. int err;
  2636. switch (cmd) {
  2637. case SIOCOUTQ:
  2638. amount = unix_outq_len(sk);
  2639. err = put_user(amount, (int __user *)arg);
  2640. break;
  2641. case SIOCINQ:
  2642. amount = unix_inq_len(sk);
  2643. if (amount < 0)
  2644. err = amount;
  2645. else
  2646. err = put_user(amount, (int __user *)arg);
  2647. break;
  2648. case SIOCUNIXFILE:
  2649. err = unix_open_file(sk);
  2650. break;
  2651. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  2652. case SIOCATMARK:
  2653. {
  2654. struct unix_sock *u = unix_sk(sk);
  2655. struct sk_buff *skb;
  2656. int answ = 0;
  2657. mutex_lock(&u->iolock);
  2658. skb = skb_peek(&sk->sk_receive_queue);
  2659. if (skb) {
  2660. struct sk_buff *oob_skb = READ_ONCE(u->oob_skb);
  2661. struct sk_buff *next_skb;
  2662. next_skb = skb_peek_next(skb, &sk->sk_receive_queue);
  2663. if (skb == oob_skb ||
  2664. (!unix_skb_len(skb) &&
  2665. (!oob_skb || next_skb == oob_skb)))
  2666. answ = 1;
  2667. }
  2668. mutex_unlock(&u->iolock);
  2669. err = put_user(answ, (int __user *)arg);
  2670. }
  2671. break;
  2672. #endif
  2673. default:
  2674. err = -ENOIOCTLCMD;
  2675. break;
  2676. }
  2677. return err;
  2678. }
  2679. #ifdef CONFIG_COMPAT
  2680. static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2681. {
  2682. return unix_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
  2683. }
  2684. #endif
  2685. static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wait)
  2686. {
  2687. struct sock *sk = sock->sk;
  2688. unsigned char state;
  2689. __poll_t mask;
  2690. u8 shutdown;
  2691. sock_poll_wait(file, sock, wait);
  2692. mask = 0;
  2693. shutdown = READ_ONCE(sk->sk_shutdown);
  2694. state = READ_ONCE(sk->sk_state);
  2695. /* exceptional events? */
  2696. if (READ_ONCE(sk->sk_err))
  2697. mask |= EPOLLERR;
  2698. if (shutdown == SHUTDOWN_MASK)
  2699. mask |= EPOLLHUP;
  2700. if (shutdown & RCV_SHUTDOWN)
  2701. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  2702. /* readable? */
  2703. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  2704. mask |= EPOLLIN | EPOLLRDNORM;
  2705. if (sk_is_readable(sk))
  2706. mask |= EPOLLIN | EPOLLRDNORM;
  2707. #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
  2708. if (READ_ONCE(unix_sk(sk)->oob_skb))
  2709. mask |= EPOLLPRI;
  2710. #endif
  2711. /* Connection-based need to check for termination and startup */
  2712. if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
  2713. state == TCP_CLOSE)
  2714. mask |= EPOLLHUP;
  2715. /*
  2716. * we set writable also when the other side has shut down the
  2717. * connection. This prevents stuck sockets.
  2718. */
  2719. if (unix_writable(sk, state))
  2720. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  2721. return mask;
  2722. }
  2723. static __poll_t unix_dgram_poll(struct file *file, struct socket *sock,
  2724. poll_table *wait)
  2725. {
  2726. struct sock *sk = sock->sk, *other;
  2727. unsigned int writable;
  2728. unsigned char state;
  2729. __poll_t mask;
  2730. u8 shutdown;
  2731. sock_poll_wait(file, sock, wait);
  2732. mask = 0;
  2733. shutdown = READ_ONCE(sk->sk_shutdown);
  2734. state = READ_ONCE(sk->sk_state);
  2735. /* exceptional events? */
  2736. if (READ_ONCE(sk->sk_err) ||
  2737. !skb_queue_empty_lockless(&sk->sk_error_queue))
  2738. mask |= EPOLLERR |
  2739. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
  2740. if (shutdown & RCV_SHUTDOWN)
  2741. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  2742. if (shutdown == SHUTDOWN_MASK)
  2743. mask |= EPOLLHUP;
  2744. /* readable? */
  2745. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  2746. mask |= EPOLLIN | EPOLLRDNORM;
  2747. if (sk_is_readable(sk))
  2748. mask |= EPOLLIN | EPOLLRDNORM;
  2749. /* Connection-based need to check for termination and startup */
  2750. if (sk->sk_type == SOCK_SEQPACKET && state == TCP_CLOSE)
  2751. mask |= EPOLLHUP;
  2752. /* No write status requested, avoid expensive OUT tests. */
  2753. if (!(poll_requested_events(wait) & (EPOLLWRBAND|EPOLLWRNORM|EPOLLOUT)))
  2754. return mask;
  2755. writable = unix_writable(sk, state);
  2756. if (writable) {
  2757. unix_state_lock(sk);
  2758. other = unix_peer(sk);
  2759. if (other && unix_peer(other) != sk &&
  2760. unix_recvq_full_lockless(other) &&
  2761. unix_dgram_peer_wake_me(sk, other))
  2762. writable = 0;
  2763. unix_state_unlock(sk);
  2764. }
  2765. if (writable)
  2766. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  2767. else
  2768. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  2769. return mask;
  2770. }
  2771. #ifdef CONFIG_PROC_FS
  2772. #define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
  2773. #define get_bucket(x) ((x) >> BUCKET_SPACE)
  2774. #define get_offset(x) ((x) & ((1UL << BUCKET_SPACE) - 1))
  2775. #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
  2776. static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos)
  2777. {
  2778. unsigned long offset = get_offset(*pos);
  2779. unsigned long bucket = get_bucket(*pos);
  2780. unsigned long count = 0;
  2781. struct sock *sk;
  2782. for (sk = sk_head(&seq_file_net(seq)->unx.table.buckets[bucket]);
  2783. sk; sk = sk_next(sk)) {
  2784. if (++count == offset)
  2785. break;
  2786. }
  2787. return sk;
  2788. }
  2789. static struct sock *unix_get_first(struct seq_file *seq, loff_t *pos)
  2790. {
  2791. unsigned long bucket = get_bucket(*pos);
  2792. struct net *net = seq_file_net(seq);
  2793. struct sock *sk;
  2794. while (bucket < UNIX_HASH_SIZE) {
  2795. spin_lock(&net->unx.table.locks[bucket]);
  2796. sk = unix_from_bucket(seq, pos);
  2797. if (sk)
  2798. return sk;
  2799. spin_unlock(&net->unx.table.locks[bucket]);
  2800. *pos = set_bucket_offset(++bucket, 1);
  2801. }
  2802. return NULL;
  2803. }
  2804. static struct sock *unix_get_next(struct seq_file *seq, struct sock *sk,
  2805. loff_t *pos)
  2806. {
  2807. unsigned long bucket = get_bucket(*pos);
  2808. sk = sk_next(sk);
  2809. if (sk)
  2810. return sk;
  2811. spin_unlock(&seq_file_net(seq)->unx.table.locks[bucket]);
  2812. *pos = set_bucket_offset(++bucket, 1);
  2813. return unix_get_first(seq, pos);
  2814. }
  2815. static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
  2816. {
  2817. if (!*pos)
  2818. return SEQ_START_TOKEN;
  2819. return unix_get_first(seq, pos);
  2820. }
  2821. static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2822. {
  2823. ++*pos;
  2824. if (v == SEQ_START_TOKEN)
  2825. return unix_get_first(seq, pos);
  2826. return unix_get_next(seq, v, pos);
  2827. }
  2828. static void unix_seq_stop(struct seq_file *seq, void *v)
  2829. {
  2830. struct sock *sk = v;
  2831. if (sk)
  2832. spin_unlock(&seq_file_net(seq)->unx.table.locks[sk->sk_hash]);
  2833. }
  2834. static int unix_seq_show(struct seq_file *seq, void *v)
  2835. {
  2836. if (v == SEQ_START_TOKEN)
  2837. seq_puts(seq, "Num RefCount Protocol Flags Type St "
  2838. "Inode Path\n");
  2839. else {
  2840. struct sock *s = v;
  2841. struct unix_sock *u = unix_sk(s);
  2842. unix_state_lock(s);
  2843. seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu",
  2844. s,
  2845. refcount_read(&s->sk_refcnt),
  2846. 0,
  2847. s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
  2848. s->sk_type,
  2849. s->sk_socket ?
  2850. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
  2851. (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
  2852. sock_i_ino(s));
  2853. if (u->addr) { // under a hash table lock here
  2854. int i, len;
  2855. seq_putc(seq, ' ');
  2856. i = 0;
  2857. len = u->addr->len -
  2858. offsetof(struct sockaddr_un, sun_path);
  2859. if (u->addr->name->sun_path[0]) {
  2860. len--;
  2861. } else {
  2862. seq_putc(seq, '@');
  2863. i++;
  2864. }
  2865. for ( ; i < len; i++)
  2866. seq_putc(seq, u->addr->name->sun_path[i] ?:
  2867. '@');
  2868. }
  2869. unix_state_unlock(s);
  2870. seq_putc(seq, '\n');
  2871. }
  2872. return 0;
  2873. }
  2874. static const struct seq_operations unix_seq_ops = {
  2875. .start = unix_seq_start,
  2876. .next = unix_seq_next,
  2877. .stop = unix_seq_stop,
  2878. .show = unix_seq_show,
  2879. };
  2880. #ifdef CONFIG_BPF_SYSCALL
  2881. struct bpf_unix_iter_state {
  2882. struct seq_net_private p;
  2883. unsigned int cur_sk;
  2884. unsigned int end_sk;
  2885. unsigned int max_sk;
  2886. struct sock **batch;
  2887. bool st_bucket_done;
  2888. };
  2889. struct bpf_iter__unix {
  2890. __bpf_md_ptr(struct bpf_iter_meta *, meta);
  2891. __bpf_md_ptr(struct unix_sock *, unix_sk);
  2892. uid_t uid __aligned(8);
  2893. };
  2894. static int unix_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta,
  2895. struct unix_sock *unix_sk, uid_t uid)
  2896. {
  2897. struct bpf_iter__unix ctx;
  2898. meta->seq_num--; /* skip SEQ_START_TOKEN */
  2899. ctx.meta = meta;
  2900. ctx.unix_sk = unix_sk;
  2901. ctx.uid = uid;
  2902. return bpf_iter_run_prog(prog, &ctx);
  2903. }
  2904. static int bpf_iter_unix_hold_batch(struct seq_file *seq, struct sock *start_sk)
  2905. {
  2906. struct bpf_unix_iter_state *iter = seq->private;
  2907. unsigned int expected = 1;
  2908. struct sock *sk;
  2909. sock_hold(start_sk);
  2910. iter->batch[iter->end_sk++] = start_sk;
  2911. for (sk = sk_next(start_sk); sk; sk = sk_next(sk)) {
  2912. if (iter->end_sk < iter->max_sk) {
  2913. sock_hold(sk);
  2914. iter->batch[iter->end_sk++] = sk;
  2915. }
  2916. expected++;
  2917. }
  2918. spin_unlock(&seq_file_net(seq)->unx.table.locks[start_sk->sk_hash]);
  2919. return expected;
  2920. }
  2921. static void bpf_iter_unix_put_batch(struct bpf_unix_iter_state *iter)
  2922. {
  2923. while (iter->cur_sk < iter->end_sk)
  2924. sock_put(iter->batch[iter->cur_sk++]);
  2925. }
  2926. static int bpf_iter_unix_realloc_batch(struct bpf_unix_iter_state *iter,
  2927. unsigned int new_batch_sz)
  2928. {
  2929. struct sock **new_batch;
  2930. new_batch = kvmalloc(sizeof(*new_batch) * new_batch_sz,
  2931. GFP_USER | __GFP_NOWARN);
  2932. if (!new_batch)
  2933. return -ENOMEM;
  2934. bpf_iter_unix_put_batch(iter);
  2935. kvfree(iter->batch);
  2936. iter->batch = new_batch;
  2937. iter->max_sk = new_batch_sz;
  2938. return 0;
  2939. }
  2940. static struct sock *bpf_iter_unix_batch(struct seq_file *seq,
  2941. loff_t *pos)
  2942. {
  2943. struct bpf_unix_iter_state *iter = seq->private;
  2944. unsigned int expected;
  2945. bool resized = false;
  2946. struct sock *sk;
  2947. if (iter->st_bucket_done)
  2948. *pos = set_bucket_offset(get_bucket(*pos) + 1, 1);
  2949. again:
  2950. /* Get a new batch */
  2951. iter->cur_sk = 0;
  2952. iter->end_sk = 0;
  2953. sk = unix_get_first(seq, pos);
  2954. if (!sk)
  2955. return NULL; /* Done */
  2956. expected = bpf_iter_unix_hold_batch(seq, sk);
  2957. if (iter->end_sk == expected) {
  2958. iter->st_bucket_done = true;
  2959. return sk;
  2960. }
  2961. if (!resized && !bpf_iter_unix_realloc_batch(iter, expected * 3 / 2)) {
  2962. resized = true;
  2963. goto again;
  2964. }
  2965. return sk;
  2966. }
  2967. static void *bpf_iter_unix_seq_start(struct seq_file *seq, loff_t *pos)
  2968. {
  2969. if (!*pos)
  2970. return SEQ_START_TOKEN;
  2971. /* bpf iter does not support lseek, so it always
  2972. * continue from where it was stop()-ped.
  2973. */
  2974. return bpf_iter_unix_batch(seq, pos);
  2975. }
  2976. static void *bpf_iter_unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2977. {
  2978. struct bpf_unix_iter_state *iter = seq->private;
  2979. struct sock *sk;
  2980. /* Whenever seq_next() is called, the iter->cur_sk is
  2981. * done with seq_show(), so advance to the next sk in
  2982. * the batch.
  2983. */
  2984. if (iter->cur_sk < iter->end_sk)
  2985. sock_put(iter->batch[iter->cur_sk++]);
  2986. ++*pos;
  2987. if (iter->cur_sk < iter->end_sk)
  2988. sk = iter->batch[iter->cur_sk];
  2989. else
  2990. sk = bpf_iter_unix_batch(seq, pos);
  2991. return sk;
  2992. }
  2993. static int bpf_iter_unix_seq_show(struct seq_file *seq, void *v)
  2994. {
  2995. struct bpf_iter_meta meta;
  2996. struct bpf_prog *prog;
  2997. struct sock *sk = v;
  2998. uid_t uid;
  2999. bool slow;
  3000. int ret;
  3001. if (v == SEQ_START_TOKEN)
  3002. return 0;
  3003. slow = lock_sock_fast(sk);
  3004. if (unlikely(sk_unhashed(sk))) {
  3005. ret = SEQ_SKIP;
  3006. goto unlock;
  3007. }
  3008. uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk));
  3009. meta.seq = seq;
  3010. prog = bpf_iter_get_info(&meta, false);
  3011. ret = unix_prog_seq_show(prog, &meta, v, uid);
  3012. unlock:
  3013. unlock_sock_fast(sk, slow);
  3014. return ret;
  3015. }
  3016. static void bpf_iter_unix_seq_stop(struct seq_file *seq, void *v)
  3017. {
  3018. struct bpf_unix_iter_state *iter = seq->private;
  3019. struct bpf_iter_meta meta;
  3020. struct bpf_prog *prog;
  3021. if (!v) {
  3022. meta.seq = seq;
  3023. prog = bpf_iter_get_info(&meta, true);
  3024. if (prog)
  3025. (void)unix_prog_seq_show(prog, &meta, v, 0);
  3026. }
  3027. if (iter->cur_sk < iter->end_sk)
  3028. bpf_iter_unix_put_batch(iter);
  3029. }
  3030. static const struct seq_operations bpf_iter_unix_seq_ops = {
  3031. .start = bpf_iter_unix_seq_start,
  3032. .next = bpf_iter_unix_seq_next,
  3033. .stop = bpf_iter_unix_seq_stop,
  3034. .show = bpf_iter_unix_seq_show,
  3035. };
  3036. #endif
  3037. #endif
  3038. static const struct net_proto_family unix_family_ops = {
  3039. .family = PF_UNIX,
  3040. .create = unix_create,
  3041. .owner = THIS_MODULE,
  3042. };
  3043. static int __net_init unix_net_init(struct net *net)
  3044. {
  3045. int i;
  3046. net->unx.sysctl_max_dgram_qlen = 10;
  3047. if (unix_sysctl_register(net))
  3048. goto out;
  3049. #ifdef CONFIG_PROC_FS
  3050. if (!proc_create_net("unix", 0, net->proc_net, &unix_seq_ops,
  3051. sizeof(struct seq_net_private)))
  3052. goto err_sysctl;
  3053. #endif
  3054. net->unx.table.locks = kvmalloc_array(UNIX_HASH_SIZE,
  3055. sizeof(spinlock_t), GFP_KERNEL);
  3056. if (!net->unx.table.locks)
  3057. goto err_proc;
  3058. net->unx.table.buckets = kvmalloc_array(UNIX_HASH_SIZE,
  3059. sizeof(struct hlist_head),
  3060. GFP_KERNEL);
  3061. if (!net->unx.table.buckets)
  3062. goto free_locks;
  3063. for (i = 0; i < UNIX_HASH_SIZE; i++) {
  3064. spin_lock_init(&net->unx.table.locks[i]);
  3065. lock_set_cmp_fn(&net->unx.table.locks[i], unix_table_lock_cmp_fn, NULL);
  3066. INIT_HLIST_HEAD(&net->unx.table.buckets[i]);
  3067. }
  3068. return 0;
  3069. free_locks:
  3070. kvfree(net->unx.table.locks);
  3071. err_proc:
  3072. #ifdef CONFIG_PROC_FS
  3073. remove_proc_entry("unix", net->proc_net);
  3074. err_sysctl:
  3075. #endif
  3076. unix_sysctl_unregister(net);
  3077. out:
  3078. return -ENOMEM;
  3079. }
  3080. static void __net_exit unix_net_exit(struct net *net)
  3081. {
  3082. kvfree(net->unx.table.buckets);
  3083. kvfree(net->unx.table.locks);
  3084. unix_sysctl_unregister(net);
  3085. remove_proc_entry("unix", net->proc_net);
  3086. }
  3087. static struct pernet_operations unix_net_ops = {
  3088. .init = unix_net_init,
  3089. .exit = unix_net_exit,
  3090. };
  3091. #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
  3092. DEFINE_BPF_ITER_FUNC(unix, struct bpf_iter_meta *meta,
  3093. struct unix_sock *unix_sk, uid_t uid)
  3094. #define INIT_BATCH_SZ 16
  3095. static int bpf_iter_init_unix(void *priv_data, struct bpf_iter_aux_info *aux)
  3096. {
  3097. struct bpf_unix_iter_state *iter = priv_data;
  3098. int err;
  3099. err = bpf_iter_init_seq_net(priv_data, aux);
  3100. if (err)
  3101. return err;
  3102. err = bpf_iter_unix_realloc_batch(iter, INIT_BATCH_SZ);
  3103. if (err) {
  3104. bpf_iter_fini_seq_net(priv_data);
  3105. return err;
  3106. }
  3107. return 0;
  3108. }
  3109. static void bpf_iter_fini_unix(void *priv_data)
  3110. {
  3111. struct bpf_unix_iter_state *iter = priv_data;
  3112. bpf_iter_fini_seq_net(priv_data);
  3113. kvfree(iter->batch);
  3114. }
  3115. static const struct bpf_iter_seq_info unix_seq_info = {
  3116. .seq_ops = &bpf_iter_unix_seq_ops,
  3117. .init_seq_private = bpf_iter_init_unix,
  3118. .fini_seq_private = bpf_iter_fini_unix,
  3119. .seq_priv_size = sizeof(struct bpf_unix_iter_state),
  3120. };
  3121. static const struct bpf_func_proto *
  3122. bpf_iter_unix_get_func_proto(enum bpf_func_id func_id,
  3123. const struct bpf_prog *prog)
  3124. {
  3125. switch (func_id) {
  3126. case BPF_FUNC_setsockopt:
  3127. return &bpf_sk_setsockopt_proto;
  3128. case BPF_FUNC_getsockopt:
  3129. return &bpf_sk_getsockopt_proto;
  3130. default:
  3131. return NULL;
  3132. }
  3133. }
  3134. static struct bpf_iter_reg unix_reg_info = {
  3135. .target = "unix",
  3136. .ctx_arg_info_size = 1,
  3137. .ctx_arg_info = {
  3138. { offsetof(struct bpf_iter__unix, unix_sk),
  3139. PTR_TO_BTF_ID_OR_NULL },
  3140. },
  3141. .get_func_proto = bpf_iter_unix_get_func_proto,
  3142. .seq_info = &unix_seq_info,
  3143. };
  3144. static void __init bpf_iter_register(void)
  3145. {
  3146. unix_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UNIX];
  3147. if (bpf_iter_reg_target(&unix_reg_info))
  3148. pr_warn("Warning: could not register bpf iterator unix\n");
  3149. }
  3150. #endif
  3151. static int __init af_unix_init(void)
  3152. {
  3153. int i, rc = -1;
  3154. BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb));
  3155. for (i = 0; i < UNIX_HASH_SIZE / 2; i++) {
  3156. spin_lock_init(&bsd_socket_locks[i]);
  3157. INIT_HLIST_HEAD(&bsd_socket_buckets[i]);
  3158. }
  3159. rc = proto_register(&unix_dgram_proto, 1);
  3160. if (rc != 0) {
  3161. pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
  3162. goto out;
  3163. }
  3164. rc = proto_register(&unix_stream_proto, 1);
  3165. if (rc != 0) {
  3166. pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
  3167. proto_unregister(&unix_dgram_proto);
  3168. goto out;
  3169. }
  3170. sock_register(&unix_family_ops);
  3171. register_pernet_subsys(&unix_net_ops);
  3172. unix_bpf_build_proto();
  3173. #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
  3174. bpf_iter_register();
  3175. #endif
  3176. out:
  3177. return rc;
  3178. }
  3179. /* Later than subsys_initcall() because we depend on stuff initialised there */
  3180. fs_initcall(af_unix_init);