Process.cpp 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Demangle.h>
  27. #include <AK/RefPtr.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <AK/ScopedValueRollback.h>
  30. #include <AK/StdLibExtras.h>
  31. #include <AK/StringBuilder.h>
  32. #include <AK/Time.h>
  33. #include <AK/Types.h>
  34. #include <Kernel/ACPI/Parser.h>
  35. #include <Kernel/Arch/i386/CPU.h>
  36. #include <Kernel/Console.h>
  37. #include <Kernel/Devices/BlockDevice.h>
  38. #include <Kernel/Devices/KeyboardDevice.h>
  39. #include <Kernel/Devices/NullDevice.h>
  40. #include <Kernel/Devices/PCSpeaker.h>
  41. #include <Kernel/Devices/RandomDevice.h>
  42. #include <Kernel/FileSystem/Custody.h>
  43. #include <Kernel/FileSystem/DevPtsFS.h>
  44. #include <Kernel/FileSystem/Ext2FileSystem.h>
  45. #include <Kernel/FileSystem/FIFO.h>
  46. #include <Kernel/FileSystem/FileDescription.h>
  47. #include <Kernel/FileSystem/InodeWatcher.h>
  48. #include <Kernel/FileSystem/ProcFS.h>
  49. #include <Kernel/FileSystem/TmpFS.h>
  50. #include <Kernel/FileSystem/VirtualFileSystem.h>
  51. #include <Kernel/Heap/kmalloc.h>
  52. #include <Kernel/IO.h>
  53. #include <Kernel/KBufferBuilder.h>
  54. #include <Kernel/KSyms.h>
  55. #include <Kernel/Module.h>
  56. #include <Kernel/Multiboot.h>
  57. #include <Kernel/Net/LocalSocket.h>
  58. #include <Kernel/Net/Socket.h>
  59. #include <Kernel/PerformanceEventBuffer.h>
  60. #include <Kernel/Process.h>
  61. #include <Kernel/Profiling.h>
  62. #include <Kernel/Ptrace.h>
  63. #include <Kernel/RTC.h>
  64. #include <Kernel/Random.h>
  65. #include <Kernel/Scheduler.h>
  66. #include <Kernel/SharedBuffer.h>
  67. #include <Kernel/StdLib.h>
  68. #include <Kernel/Syscall.h>
  69. #include <Kernel/TTY/MasterPTY.h>
  70. #include <Kernel/TTY/TTY.h>
  71. #include <Kernel/Thread.h>
  72. #include <Kernel/ThreadTracer.h>
  73. #include <Kernel/Time/TimeManagement.h>
  74. #include <Kernel/VM/PageDirectory.h>
  75. #include <Kernel/VM/PrivateInodeVMObject.h>
  76. #include <Kernel/VM/ProcessPagingScope.h>
  77. #include <Kernel/VM/PurgeableVMObject.h>
  78. #include <Kernel/VM/SharedInodeVMObject.h>
  79. #include <LibC/errno_numbers.h>
  80. #include <LibC/limits.h>
  81. #include <LibC/signal_numbers.h>
  82. #include <LibELF/Loader.h>
  83. #include <LibELF/Validation.h>
  84. #include <LibKeyboard/CharacterMapData.h>
  85. //#define PROCESS_DEBUG
  86. //#define DEBUG_POLL_SELECT
  87. //#define DEBUG_IO
  88. //#define TASK_DEBUG
  89. //#define FORK_DEBUG
  90. //#define EXEC_DEBUG
  91. //#define SIGNAL_DEBUG
  92. //#define SHARED_BUFFER_DEBUG
  93. namespace Kernel {
  94. static void create_signal_trampolines();
  95. Process* Process::current;
  96. static pid_t next_pid;
  97. InlineLinkedList<Process>* g_processes;
  98. static String* s_hostname;
  99. static Lock* s_hostname_lock;
  100. VirtualAddress g_return_to_ring3_from_signal_trampoline;
  101. HashMap<String, OwnPtr<Module>>* g_modules;
  102. pid_t Process::allocate_pid()
  103. {
  104. InterruptDisabler disabler;
  105. return next_pid++;
  106. }
  107. void Process::initialize()
  108. {
  109. g_modules = new HashMap<String, OwnPtr<Module>>;
  110. next_pid = 0;
  111. g_processes = new InlineLinkedList<Process>;
  112. s_hostname = new String("courage");
  113. s_hostname_lock = new Lock;
  114. create_signal_trampolines();
  115. }
  116. Vector<pid_t> Process::all_pids()
  117. {
  118. Vector<pid_t> pids;
  119. InterruptDisabler disabler;
  120. pids.ensure_capacity((int)g_processes->size_slow());
  121. for (auto& process : *g_processes)
  122. pids.append(process.pid());
  123. return pids;
  124. }
  125. Vector<Process*> Process::all_processes()
  126. {
  127. Vector<Process*> processes;
  128. InterruptDisabler disabler;
  129. processes.ensure_capacity((int)g_processes->size_slow());
  130. for (auto& process : *g_processes)
  131. processes.append(&process);
  132. return processes;
  133. }
  134. bool Process::in_group(gid_t gid) const
  135. {
  136. return m_gid == gid || m_extra_gids.contains(gid);
  137. }
  138. Range Process::allocate_range(VirtualAddress vaddr, size_t size, size_t alignment)
  139. {
  140. vaddr.mask(PAGE_MASK);
  141. size = PAGE_ROUND_UP(size);
  142. if (vaddr.is_null())
  143. return page_directory().range_allocator().allocate_anywhere(size, alignment);
  144. return page_directory().range_allocator().allocate_specific(vaddr, size);
  145. }
  146. static unsigned prot_to_region_access_flags(int prot)
  147. {
  148. unsigned access = 0;
  149. if (prot & PROT_READ)
  150. access |= Region::Access::Read;
  151. if (prot & PROT_WRITE)
  152. access |= Region::Access::Write;
  153. if (prot & PROT_EXEC)
  154. access |= Region::Access::Execute;
  155. return access;
  156. }
  157. Region& Process::allocate_split_region(const Region& source_region, const Range& range, size_t offset_in_vmobject)
  158. {
  159. auto& region = add_region(Region::create_user_accessible(range, source_region.vmobject(), offset_in_vmobject, source_region.name(), source_region.access()));
  160. region.set_mmap(source_region.is_mmap());
  161. region.set_stack(source_region.is_stack());
  162. size_t page_offset_in_source_region = (offset_in_vmobject - source_region.offset_in_vmobject()) / PAGE_SIZE;
  163. for (size_t i = 0; i < region.page_count(); ++i) {
  164. if (source_region.should_cow(page_offset_in_source_region + i))
  165. region.set_should_cow(i, true);
  166. }
  167. return region;
  168. }
  169. Region* Process::allocate_region(const Range& range, const String& name, int prot, bool should_commit)
  170. {
  171. ASSERT(range.is_valid());
  172. auto vmobject = AnonymousVMObject::create_with_size(range.size());
  173. auto region = Region::create_user_accessible(range, vmobject, 0, name, prot_to_region_access_flags(prot));
  174. region->map(page_directory());
  175. if (should_commit && !region->commit())
  176. return nullptr;
  177. return &add_region(move(region));
  178. }
  179. Region* Process::allocate_region(VirtualAddress vaddr, size_t size, const String& name, int prot, bool should_commit)
  180. {
  181. auto range = allocate_range(vaddr, size);
  182. if (!range.is_valid())
  183. return nullptr;
  184. return allocate_region(range, name, prot, should_commit);
  185. }
  186. Region* Process::allocate_region_with_vmobject(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot)
  187. {
  188. ASSERT(range.is_valid());
  189. size_t end_in_vmobject = offset_in_vmobject + range.size();
  190. if (end_in_vmobject <= offset_in_vmobject) {
  191. dbg() << "allocate_region_with_vmobject: Overflow (offset + size)";
  192. return nullptr;
  193. }
  194. if (offset_in_vmobject >= vmobject->size()) {
  195. dbg() << "allocate_region_with_vmobject: Attempt to allocate a region with an offset past the end of its VMObject.";
  196. return nullptr;
  197. }
  198. if (end_in_vmobject > vmobject->size()) {
  199. dbg() << "allocate_region_with_vmobject: Attempt to allocate a region with an end past the end of its VMObject.";
  200. return nullptr;
  201. }
  202. offset_in_vmobject &= PAGE_MASK;
  203. auto& region = add_region(Region::create_user_accessible(range, move(vmobject), offset_in_vmobject, name, prot_to_region_access_flags(prot)));
  204. region.map(page_directory());
  205. return &region;
  206. }
  207. Region* Process::allocate_region_with_vmobject(VirtualAddress vaddr, size_t size, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot)
  208. {
  209. auto range = allocate_range(vaddr, size);
  210. if (!range.is_valid())
  211. return nullptr;
  212. return allocate_region_with_vmobject(range, move(vmobject), offset_in_vmobject, name, prot);
  213. }
  214. bool Process::deallocate_region(Region& region)
  215. {
  216. InterruptDisabler disabler;
  217. if (m_region_lookup_cache.region == &region)
  218. m_region_lookup_cache.region = nullptr;
  219. for (size_t i = 0; i < m_regions.size(); ++i) {
  220. if (&m_regions[i] == &region) {
  221. m_regions.unstable_remove(i);
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. Region* Process::region_from_range(const Range& range)
  228. {
  229. if (m_region_lookup_cache.range == range && m_region_lookup_cache.region)
  230. return m_region_lookup_cache.region;
  231. size_t size = PAGE_ROUND_UP(range.size());
  232. for (auto& region : m_regions) {
  233. if (region.vaddr() == range.base() && region.size() == size) {
  234. m_region_lookup_cache.range = range;
  235. m_region_lookup_cache.region = region.make_weak_ptr();
  236. return &region;
  237. }
  238. }
  239. return nullptr;
  240. }
  241. Region* Process::region_containing(const Range& range)
  242. {
  243. for (auto& region : m_regions) {
  244. if (region.contains(range))
  245. return &region;
  246. }
  247. return nullptr;
  248. }
  249. int Process::sys$set_mmap_name(const Syscall::SC_set_mmap_name_params* user_params)
  250. {
  251. REQUIRE_PROMISE(stdio);
  252. Syscall::SC_set_mmap_name_params params;
  253. if (!validate_read_and_copy_typed(&params, user_params))
  254. return -EFAULT;
  255. if (params.name.length > PATH_MAX)
  256. return -ENAMETOOLONG;
  257. auto name = validate_and_copy_string_from_user(params.name);
  258. if (name.is_null())
  259. return -EFAULT;
  260. auto* region = region_from_range({ VirtualAddress(params.addr), params.size });
  261. if (!region)
  262. return -EINVAL;
  263. if (!region->is_mmap())
  264. return -EPERM;
  265. region->set_name(name);
  266. return 0;
  267. }
  268. static bool validate_mmap_prot(int prot, bool map_stack)
  269. {
  270. bool readable = prot & PROT_READ;
  271. bool writable = prot & PROT_WRITE;
  272. bool executable = prot & PROT_EXEC;
  273. if (writable && executable)
  274. return false;
  275. if (map_stack) {
  276. if (executable)
  277. return false;
  278. if (!readable || !writable)
  279. return false;
  280. }
  281. return true;
  282. }
  283. static bool validate_inode_mmap_prot(const Process& process, int prot, const Inode& inode, bool map_shared)
  284. {
  285. auto metadata = inode.metadata();
  286. if ((prot & PROT_READ) && !metadata.may_read(process))
  287. return false;
  288. if (map_shared) {
  289. // FIXME: What about readonly filesystem mounts? We cannot make a
  290. // decision here without knowing the mount flags, so we would need to
  291. // keep a Custody or something from mmap time.
  292. if ((prot & PROT_WRITE) && !metadata.may_write(process))
  293. return false;
  294. InterruptDisabler disabler;
  295. if (inode.shared_vmobject()) {
  296. if ((prot & PROT_EXEC) && inode.shared_vmobject()->writable_mappings())
  297. return false;
  298. if ((prot & PROT_WRITE) && inode.shared_vmobject()->executable_mappings())
  299. return false;
  300. }
  301. }
  302. return true;
  303. }
  304. // Carve out a virtual address range from a region and return the two regions on either side
  305. Vector<Region*, 2> Process::split_region_around_range(const Region& source_region, const Range& desired_range)
  306. {
  307. Range old_region_range = source_region.range();
  308. auto remaining_ranges_after_unmap = old_region_range.carve(desired_range);
  309. ASSERT(!remaining_ranges_after_unmap.is_empty());
  310. auto make_replacement_region = [&](const Range& new_range) -> Region& {
  311. ASSERT(old_region_range.contains(new_range));
  312. size_t new_range_offset_in_vmobject = source_region.offset_in_vmobject() + (new_range.base().get() - old_region_range.base().get());
  313. return allocate_split_region(source_region, new_range, new_range_offset_in_vmobject);
  314. };
  315. Vector<Region*, 2> new_regions;
  316. for (auto& new_range : remaining_ranges_after_unmap) {
  317. new_regions.unchecked_append(&make_replacement_region(new_range));
  318. }
  319. return new_regions;
  320. }
  321. void* Process::sys$mmap(const Syscall::SC_mmap_params* user_params)
  322. {
  323. REQUIRE_PROMISE(stdio);
  324. Syscall::SC_mmap_params params;
  325. if (!validate_read_and_copy_typed(&params, user_params))
  326. return (void*)-EFAULT;
  327. void* addr = (void*)params.addr;
  328. size_t size = params.size;
  329. size_t alignment = params.alignment;
  330. int prot = params.prot;
  331. int flags = params.flags;
  332. int fd = params.fd;
  333. int offset = params.offset;
  334. if (alignment & ~PAGE_MASK)
  335. return (void*)-EINVAL;
  336. if (!is_user_range(VirtualAddress(addr), size))
  337. return (void*)-EFAULT;
  338. String name;
  339. if (params.name.characters) {
  340. if (params.name.length > PATH_MAX)
  341. return (void*)-ENAMETOOLONG;
  342. name = validate_and_copy_string_from_user(params.name);
  343. if (name.is_null())
  344. return (void*)-EFAULT;
  345. }
  346. if (size == 0)
  347. return (void*)-EINVAL;
  348. if ((FlatPtr)addr & ~PAGE_MASK)
  349. return (void*)-EINVAL;
  350. bool map_shared = flags & MAP_SHARED;
  351. bool map_anonymous = flags & MAP_ANONYMOUS;
  352. bool map_purgeable = flags & MAP_PURGEABLE;
  353. bool map_private = flags & MAP_PRIVATE;
  354. bool map_stack = flags & MAP_STACK;
  355. bool map_fixed = flags & MAP_FIXED;
  356. if (map_shared && map_private)
  357. return (void*)-EINVAL;
  358. if (!map_shared && !map_private)
  359. return (void*)-EINVAL;
  360. if (!validate_mmap_prot(prot, map_stack))
  361. return (void*)-EINVAL;
  362. if (map_stack && (!map_private || !map_anonymous))
  363. return (void*)-EINVAL;
  364. Region* region = nullptr;
  365. auto range = allocate_range(VirtualAddress(addr), size, alignment);
  366. if (!range.is_valid())
  367. return (void*)-ENOMEM;
  368. if (map_purgeable) {
  369. auto vmobject = PurgeableVMObject::create_with_size(size);
  370. region = allocate_region_with_vmobject(range, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot);
  371. if (!region && (!map_fixed && addr != 0))
  372. region = allocate_region_with_vmobject({}, size, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot);
  373. } else if (map_anonymous) {
  374. region = allocate_region(range, !name.is_null() ? name : "mmap", prot, false);
  375. if (!region && (!map_fixed && addr != 0))
  376. region = allocate_region(allocate_range({}, size), !name.is_null() ? name : "mmap", prot, false);
  377. } else {
  378. if (offset < 0)
  379. return (void*)-EINVAL;
  380. if (static_cast<size_t>(offset) & ~PAGE_MASK)
  381. return (void*)-EINVAL;
  382. auto description = file_description(fd);
  383. if (!description)
  384. return (void*)-EBADF;
  385. if (description->is_directory())
  386. return (void*)-ENODEV;
  387. // Require read access even when read protection is not requested.
  388. if (!description->is_readable())
  389. return (void*)-EACCES;
  390. if (map_shared) {
  391. if ((prot & PROT_WRITE) && !description->is_writable())
  392. return (void*)-EACCES;
  393. }
  394. if (description->inode()) {
  395. if (!validate_inode_mmap_prot(*this, prot, *description->inode(), map_shared))
  396. return (void*)-EACCES;
  397. }
  398. auto region_or_error = description->mmap(*this, VirtualAddress(addr), static_cast<size_t>(offset), size, prot, map_shared);
  399. if (region_or_error.is_error()) {
  400. // Fail if MAP_FIXED or address is 0, retry otherwise
  401. if (map_fixed || addr == 0)
  402. return (void*)(int)region_or_error.error();
  403. region_or_error = description->mmap(*this, {}, static_cast<size_t>(offset), size, prot, map_shared);
  404. }
  405. if (region_or_error.is_error())
  406. return (void*)(int)region_or_error.error();
  407. region = region_or_error.value();
  408. }
  409. if (!region)
  410. return (void*)-ENOMEM;
  411. region->set_mmap(true);
  412. if (map_shared)
  413. region->set_shared(true);
  414. if (map_stack)
  415. region->set_stack(true);
  416. if (!name.is_null())
  417. region->set_name(name);
  418. return region->vaddr().as_ptr();
  419. }
  420. int Process::sys$munmap(void* addr, size_t size)
  421. {
  422. REQUIRE_PROMISE(stdio);
  423. if (!size)
  424. return -EINVAL;
  425. if (!is_user_range(VirtualAddress(addr), size))
  426. return -EFAULT;
  427. Range range_to_unmap { VirtualAddress(addr), size };
  428. if (auto* whole_region = region_from_range(range_to_unmap)) {
  429. if (!whole_region->is_mmap())
  430. return -EPERM;
  431. bool success = deallocate_region(*whole_region);
  432. ASSERT(success);
  433. return 0;
  434. }
  435. if (auto* old_region = region_containing(range_to_unmap)) {
  436. if (!old_region->is_mmap())
  437. return -EPERM;
  438. auto new_regions = split_region_around_range(*old_region, range_to_unmap);
  439. // We manually unmap the old region here, specifying that we *don't* want the VM deallocated.
  440. old_region->unmap(Region::ShouldDeallocateVirtualMemoryRange::No);
  441. deallocate_region(*old_region);
  442. // Instead we give back the unwanted VM manually.
  443. page_directory().range_allocator().deallocate(range_to_unmap);
  444. // And finally we map the new region(s) using our page directory (they were just allocated and don't have one).
  445. for (auto* new_region : new_regions) {
  446. new_region->map(page_directory());
  447. }
  448. return 0;
  449. }
  450. // FIXME: We should also support munmap() across multiple regions. (#175)
  451. return -EINVAL;
  452. }
  453. int Process::sys$mprotect(void* addr, size_t size, int prot)
  454. {
  455. REQUIRE_PROMISE(stdio);
  456. if (!size)
  457. return -EINVAL;
  458. if (!is_user_range(VirtualAddress(addr), size))
  459. return -EFAULT;
  460. Range range_to_mprotect = { VirtualAddress(addr), size };
  461. if (auto* whole_region = region_from_range(range_to_mprotect)) {
  462. if (!whole_region->is_mmap())
  463. return -EPERM;
  464. if (!validate_mmap_prot(prot, whole_region->is_stack()))
  465. return -EINVAL;
  466. if (whole_region->access() == prot_to_region_access_flags(prot))
  467. return 0;
  468. if (whole_region->vmobject().is_inode()
  469. && !validate_inode_mmap_prot(*this, prot, static_cast<const InodeVMObject&>(whole_region->vmobject()).inode(), whole_region->is_shared())) {
  470. return -EACCES;
  471. }
  472. whole_region->set_readable(prot & PROT_READ);
  473. whole_region->set_writable(prot & PROT_WRITE);
  474. whole_region->set_executable(prot & PROT_EXEC);
  475. whole_region->remap();
  476. return 0;
  477. }
  478. // Check if we can carve out the desired range from an existing region
  479. if (auto* old_region = region_containing(range_to_mprotect)) {
  480. if (!old_region->is_mmap())
  481. return -EPERM;
  482. if (!validate_mmap_prot(prot, old_region->is_stack()))
  483. return -EINVAL;
  484. if (old_region->access() == prot_to_region_access_flags(prot))
  485. return 0;
  486. if (old_region->vmobject().is_inode()
  487. && !validate_inode_mmap_prot(*this, prot, static_cast<const InodeVMObject&>(old_region->vmobject()).inode(), old_region->is_shared())) {
  488. return -EACCES;
  489. }
  490. // This vector is the region(s) adjacent to our range.
  491. // We need to allocate a new region for the range we wanted to change permission bits on.
  492. auto adjacent_regions = split_region_around_range(*old_region, range_to_mprotect);
  493. size_t new_range_offset_in_vmobject = old_region->offset_in_vmobject() + (range_to_mprotect.base().get() - old_region->range().base().get());
  494. auto& new_region = allocate_split_region(*old_region, range_to_mprotect, new_range_offset_in_vmobject);
  495. new_region.set_readable(prot & PROT_READ);
  496. new_region.set_writable(prot & PROT_WRITE);
  497. new_region.set_executable(prot & PROT_EXEC);
  498. // Unmap the old region here, specifying that we *don't* want the VM deallocated.
  499. old_region->unmap(Region::ShouldDeallocateVirtualMemoryRange::No);
  500. deallocate_region(*old_region);
  501. // Map the new regions using our page directory (they were just allocated and don't have one).
  502. for (auto* adjacent_region : adjacent_regions) {
  503. adjacent_region->map(page_directory());
  504. }
  505. new_region.map(page_directory());
  506. return 0;
  507. }
  508. // FIXME: We should also support mprotect() across multiple regions. (#175) (#964)
  509. return -EINVAL;
  510. }
  511. int Process::sys$madvise(void* address, size_t size, int advice)
  512. {
  513. REQUIRE_PROMISE(stdio);
  514. if (!size)
  515. return -EINVAL;
  516. if (!is_user_range(VirtualAddress(address), size))
  517. return -EFAULT;
  518. auto* region = region_from_range({ VirtualAddress(address), size });
  519. if (!region)
  520. return -EINVAL;
  521. if (!region->is_mmap())
  522. return -EPERM;
  523. if ((advice & MADV_SET_VOLATILE) && (advice & MADV_SET_NONVOLATILE))
  524. return -EINVAL;
  525. if (advice & MADV_SET_VOLATILE) {
  526. if (!region->vmobject().is_purgeable())
  527. return -EPERM;
  528. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  529. vmobject.set_volatile(true);
  530. return 0;
  531. }
  532. if (advice & MADV_SET_NONVOLATILE) {
  533. if (!region->vmobject().is_purgeable())
  534. return -EPERM;
  535. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  536. if (!vmobject.is_volatile())
  537. return 0;
  538. vmobject.set_volatile(false);
  539. bool was_purged = vmobject.was_purged();
  540. vmobject.set_was_purged(false);
  541. return was_purged ? 1 : 0;
  542. }
  543. if (advice & MADV_GET_VOLATILE) {
  544. if (!region->vmobject().is_purgeable())
  545. return -EPERM;
  546. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  547. return vmobject.is_volatile() ? 0 : 1;
  548. }
  549. return -EINVAL;
  550. }
  551. int Process::sys$minherit(void* address, size_t size, int inherit)
  552. {
  553. REQUIRE_PROMISE(stdio);
  554. auto* region = region_from_range({ VirtualAddress(address), size });
  555. if (!region)
  556. return -EINVAL;
  557. if (!region->is_mmap())
  558. return -EINVAL;
  559. if (region->is_shared())
  560. return -EINVAL;
  561. if (!region->vmobject().is_anonymous())
  562. return -EINVAL;
  563. switch (inherit) {
  564. case MAP_INHERIT_ZERO:
  565. region->set_inherit_mode(Region::InheritMode::ZeroedOnFork);
  566. return 0;
  567. }
  568. return -EINVAL;
  569. }
  570. int Process::sys$purge(int mode)
  571. {
  572. REQUIRE_NO_PROMISES;
  573. if (!is_superuser())
  574. return -EPERM;
  575. int purged_page_count = 0;
  576. if (mode & PURGE_ALL_VOLATILE) {
  577. NonnullRefPtrVector<PurgeableVMObject> vmobjects;
  578. {
  579. InterruptDisabler disabler;
  580. MM.for_each_vmobject_of_type<PurgeableVMObject>([&](auto& vmobject) {
  581. vmobjects.append(vmobject);
  582. return IterationDecision::Continue;
  583. });
  584. }
  585. for (auto& vmobject : vmobjects) {
  586. purged_page_count += vmobject.purge();
  587. }
  588. }
  589. if (mode & PURGE_ALL_CLEAN_INODE) {
  590. NonnullRefPtrVector<InodeVMObject> vmobjects;
  591. {
  592. InterruptDisabler disabler;
  593. MM.for_each_vmobject_of_type<InodeVMObject>([&](auto& vmobject) {
  594. vmobjects.append(vmobject);
  595. return IterationDecision::Continue;
  596. });
  597. }
  598. for (auto& vmobject : vmobjects) {
  599. purged_page_count += vmobject.release_all_clean_pages();
  600. }
  601. }
  602. return purged_page_count;
  603. }
  604. int Process::sys$gethostname(char* buffer, ssize_t size)
  605. {
  606. REQUIRE_PROMISE(stdio);
  607. if (size < 0)
  608. return -EINVAL;
  609. if (!validate_write(buffer, size))
  610. return -EFAULT;
  611. LOCKER(*s_hostname_lock, Lock::Mode::Shared);
  612. if ((size_t)size < (s_hostname->length() + 1))
  613. return -ENAMETOOLONG;
  614. copy_to_user(buffer, s_hostname->characters(), s_hostname->length() + 1);
  615. return 0;
  616. }
  617. int Process::sys$sethostname(const char* hostname, ssize_t length)
  618. {
  619. REQUIRE_NO_PROMISES;
  620. if (!is_superuser())
  621. return -EPERM;
  622. if (length < 0)
  623. return -EINVAL;
  624. LOCKER(*s_hostname_lock, Lock::Mode::Exclusive);
  625. if (length > 64)
  626. return -ENAMETOOLONG;
  627. *s_hostname = validate_and_copy_string_from_user(hostname, length);
  628. return 0;
  629. }
  630. pid_t Process::sys$fork(RegisterState& regs)
  631. {
  632. REQUIRE_PROMISE(proc);
  633. Thread* child_first_thread = nullptr;
  634. auto* child = new Process(child_first_thread, m_name, m_uid, m_gid, m_pid, m_ring, m_cwd, m_executable, m_tty, this);
  635. child->m_root_directory = m_root_directory;
  636. child->m_root_directory_relative_to_global_root = m_root_directory_relative_to_global_root;
  637. child->m_promises = m_promises;
  638. child->m_execpromises = m_execpromises;
  639. child->m_veil_state = m_veil_state;
  640. child->m_unveiled_paths = m_unveiled_paths;
  641. child->m_fds = m_fds;
  642. child->m_sid = m_sid;
  643. child->m_pgid = m_pgid;
  644. child->m_umask = m_umask;
  645. #ifdef FORK_DEBUG
  646. dbg() << "fork: child=" << child;
  647. #endif
  648. for (auto& region : m_regions) {
  649. #ifdef FORK_DEBUG
  650. dbg() << "fork: cloning Region{" << &region << "} '" << region.name() << "' @ " << region.vaddr();
  651. #endif
  652. auto& child_region = child->add_region(region.clone());
  653. child_region.map(child->page_directory());
  654. if (&region == m_master_tls_region)
  655. child->m_master_tls_region = child_region.make_weak_ptr();
  656. }
  657. child->m_extra_gids = m_extra_gids;
  658. auto& child_tss = child_first_thread->m_tss;
  659. child_tss.eax = 0; // fork() returns 0 in the child :^)
  660. child_tss.ebx = regs.ebx;
  661. child_tss.ecx = regs.ecx;
  662. child_tss.edx = regs.edx;
  663. child_tss.ebp = regs.ebp;
  664. child_tss.esp = regs.userspace_esp;
  665. child_tss.esi = regs.esi;
  666. child_tss.edi = regs.edi;
  667. child_tss.eflags = regs.eflags;
  668. child_tss.eip = regs.eip;
  669. child_tss.cs = regs.cs;
  670. child_tss.ds = regs.ds;
  671. child_tss.es = regs.es;
  672. child_tss.fs = regs.fs;
  673. child_tss.gs = regs.gs;
  674. child_tss.ss = regs.userspace_ss;
  675. #ifdef FORK_DEBUG
  676. dbg() << "fork: child will begin executing at " << String::format("%w", child_tss.cs) << ":" << String::format("%x", child_tss.eip) << " with stack " << String::format("%w", child_tss.ss) << ":" << String::format("%x", child_tss.esp) << ", kstack " << String::format("%w", child_tss.ss0) << ":" << String::format("%x", child_tss.esp0);
  677. #endif
  678. {
  679. InterruptDisabler disabler;
  680. g_processes->prepend(child);
  681. }
  682. #ifdef TASK_DEBUG
  683. klog() << "Process " << child->pid() << " (" << child->name().characters() << ") forked from " << m_pid << " @ " << String::format("%p", child_tss.eip);
  684. #endif
  685. child_first_thread->set_state(Thread::State::Skip1SchedulerPass);
  686. return child->pid();
  687. }
  688. void Process::kill_threads_except_self()
  689. {
  690. InterruptDisabler disabler;
  691. if (m_thread_count <= 1)
  692. return;
  693. for_each_thread([&](Thread& thread) {
  694. if (&thread == Thread::current
  695. || thread.state() == Thread::State::Dead
  696. || thread.state() == Thread::State::Dying)
  697. return IterationDecision::Continue;
  698. // At this point, we have no joiner anymore
  699. thread.m_joiner = nullptr;
  700. thread.set_should_die();
  701. if (thread.state() != Thread::State::Dead)
  702. thread.set_state(Thread::State::Dying);
  703. return IterationDecision::Continue;
  704. });
  705. big_lock().clear_waiters();
  706. }
  707. void Process::kill_all_threads()
  708. {
  709. for_each_thread([&](Thread& thread) {
  710. thread.set_should_die();
  711. return IterationDecision::Continue;
  712. });
  713. }
  714. int Process::do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description)
  715. {
  716. ASSERT(is_ring3());
  717. auto path = main_program_description->absolute_path();
  718. #ifdef EXEC_DEBUG
  719. dbg() << "do_exec(" << path << ")";
  720. #endif
  721. size_t total_blob_size = 0;
  722. for (auto& a : arguments)
  723. total_blob_size += a.length() + 1;
  724. for (auto& e : environment)
  725. total_blob_size += e.length() + 1;
  726. size_t total_meta_size = sizeof(char*) * (arguments.size() + 1) + sizeof(char*) * (environment.size() + 1);
  727. // FIXME: How much stack space does process startup need?
  728. if ((total_blob_size + total_meta_size) >= Thread::default_userspace_stack_size)
  729. return -E2BIG;
  730. auto parts = path.split('/');
  731. if (parts.is_empty())
  732. return -ENOENT;
  733. auto& inode = interpreter_description ? *interpreter_description->inode() : *main_program_description->inode();
  734. auto vmobject = SharedInodeVMObject::create_with_inode(inode);
  735. if (static_cast<const SharedInodeVMObject&>(*vmobject).writable_mappings()) {
  736. dbg() << "Refusing to execute a write-mapped program";
  737. return -ETXTBSY;
  738. }
  739. // Disable profiling temporarily in case it's running on this process.
  740. bool was_profiling = is_profiling();
  741. TemporaryChange profiling_disabler(m_profiling, false);
  742. // Mark this thread as the current thread that does exec
  743. // No other thread from this process will be scheduled to run
  744. m_exec_tid = Thread::current->tid();
  745. auto old_page_directory = move(m_page_directory);
  746. auto old_regions = move(m_regions);
  747. m_page_directory = PageDirectory::create_for_userspace(*this);
  748. #ifdef MM_DEBUG
  749. dbg() << "Process " << pid() << " exec: PD=" << m_page_directory.ptr() << " created";
  750. #endif
  751. InodeMetadata loader_metadata;
  752. // FIXME: Hoooo boy this is a hack if I ever saw one.
  753. // This is the 'random' offset we're giving to our ET_DYN exectuables to start as.
  754. // It also happens to be the static Virtual Addresss offset every static exectuable gets :)
  755. // Without this, some assumptions by the ELF loading hooks below are severely broken.
  756. // 0x08000000 is a verified random number chosen by random dice roll https://xkcd.com/221/
  757. u32 totally_random_offset = interpreter_description ? 0x08000000 : 0;
  758. // FIXME: We should be able to load both the PT_INTERP interpreter and the main program... once the RTLD is smart enough
  759. if (interpreter_description) {
  760. loader_metadata = interpreter_description->metadata();
  761. // we don't need the interpreter file desciption after we've loaded (or not) it into memory
  762. interpreter_description = nullptr;
  763. } else {
  764. loader_metadata = main_program_description->metadata();
  765. }
  766. auto region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_ROUND_UP(loader_metadata.size), "ELF loading", Region::Access::Read);
  767. if (!region)
  768. return -ENOMEM;
  769. Region* master_tls_region { nullptr };
  770. size_t master_tls_size = 0;
  771. size_t master_tls_alignment = 0;
  772. u32 entry_eip = 0;
  773. MM.enter_process_paging_scope(*this);
  774. RefPtr<ELF::Loader> loader;
  775. {
  776. ArmedScopeGuard rollback_regions_guard([&]() {
  777. ASSERT(Process::current == this);
  778. m_page_directory = move(old_page_directory);
  779. m_regions = move(old_regions);
  780. MM.enter_process_paging_scope(*this);
  781. });
  782. loader = ELF::Loader::create(region->vaddr().as_ptr(), loader_metadata.size);
  783. // Load the correct executable -- either interp or main program.
  784. // FIXME: Once we actually load both interp and main, we'll need to be more clever about this.
  785. // In that case, both will be ET_DYN objects, so they'll both be completely relocatable.
  786. // That means, we can put them literally anywhere in User VM space (ASLR anyone?).
  787. // ALSO FIXME: Reminder to really really fix that 'totally random offset' business.
  788. loader->map_section_hook = [&](VirtualAddress vaddr, size_t size, size_t alignment, size_t offset_in_image, bool is_readable, bool is_writable, bool is_executable, const String& name) -> u8* {
  789. ASSERT(size);
  790. ASSERT(alignment == PAGE_SIZE);
  791. int prot = 0;
  792. if (is_readable)
  793. prot |= PROT_READ;
  794. if (is_writable)
  795. prot |= PROT_WRITE;
  796. if (is_executable)
  797. prot |= PROT_EXEC;
  798. if (auto* region = allocate_region_with_vmobject(vaddr.offset(totally_random_offset), size, *vmobject, offset_in_image, String(name), prot)) {
  799. region->set_shared(true);
  800. return region->vaddr().as_ptr();
  801. }
  802. return nullptr;
  803. };
  804. loader->alloc_section_hook = [&](VirtualAddress vaddr, size_t size, size_t alignment, bool is_readable, bool is_writable, const String& name) -> u8* {
  805. ASSERT(size);
  806. ASSERT(alignment == PAGE_SIZE);
  807. int prot = 0;
  808. if (is_readable)
  809. prot |= PROT_READ;
  810. if (is_writable)
  811. prot |= PROT_WRITE;
  812. if (auto* region = allocate_region(vaddr.offset(totally_random_offset), size, String(name), prot))
  813. return region->vaddr().as_ptr();
  814. return nullptr;
  815. };
  816. // FIXME: Move TLS region allocation to userspace: LibC and the dynamic loader.
  817. // LibC if we end up with a statically linked executable, and the
  818. // dynamic loader so that it can create new TLS blocks for each shared libarary
  819. // that gets loaded as part of DT_NEEDED processing, and via dlopen()
  820. // If that doesn't happen quickly, at least pass the location of the TLS region
  821. // some ELF Auxilliary Vector so the loader can use it/create new ones as necessary.
  822. loader->tls_section_hook = [&](size_t size, size_t alignment) {
  823. ASSERT(size);
  824. master_tls_region = allocate_region({}, size, String(), PROT_READ | PROT_WRITE);
  825. master_tls_size = size;
  826. master_tls_alignment = alignment;
  827. return master_tls_region->vaddr().as_ptr();
  828. };
  829. bool success = loader->load();
  830. if (!success) {
  831. klog() << "do_exec: Failure loading " << path.characters();
  832. return -ENOEXEC;
  833. }
  834. // FIXME: Validate that this virtual address is within executable region,
  835. // instead of just non-null. You could totally have a DSO with entry point of
  836. // the beginning of the text segement.
  837. if (!loader->entry().offset(totally_random_offset).get()) {
  838. klog() << "do_exec: Failure loading " << path.characters() << ", entry pointer is invalid! (" << loader->entry().offset(totally_random_offset) << ")";
  839. return -ENOEXEC;
  840. }
  841. rollback_regions_guard.disarm();
  842. // NOTE: At this point, we've committed to the new executable.
  843. entry_eip = loader->entry().offset(totally_random_offset).get();
  844. kill_threads_except_self();
  845. #ifdef EXEC_DEBUG
  846. klog() << "Memory layout after ELF load:";
  847. dump_regions();
  848. #endif
  849. }
  850. m_executable = main_program_description->custody();
  851. m_promises = m_execpromises;
  852. m_veil_state = VeilState::None;
  853. m_unveiled_paths.clear();
  854. // Copy of the master TLS region that we will clone for new threads
  855. m_master_tls_region = master_tls_region->make_weak_ptr();
  856. auto main_program_metadata = main_program_description->metadata();
  857. if (!(main_program_description->custody()->mount_flags() & MS_NOSUID)) {
  858. if (main_program_metadata.is_setuid())
  859. m_euid = m_suid = main_program_metadata.uid;
  860. if (main_program_metadata.is_setgid())
  861. m_egid = m_sgid = main_program_metadata.gid;
  862. }
  863. Thread::current->set_default_signal_dispositions();
  864. Thread::current->m_signal_mask = 0;
  865. Thread::current->m_pending_signals = 0;
  866. m_futex_queues.clear();
  867. m_region_lookup_cache = {};
  868. disown_all_shared_buffers();
  869. for (size_t i = 0; i < m_fds.size(); ++i) {
  870. auto& daf = m_fds[i];
  871. if (daf.description && daf.flags & FD_CLOEXEC) {
  872. daf.description->close();
  873. daf = {};
  874. }
  875. }
  876. Thread* new_main_thread = nullptr;
  877. if (Process::current == this) {
  878. new_main_thread = Thread::current;
  879. } else {
  880. for_each_thread([&](auto& thread) {
  881. new_main_thread = &thread;
  882. return IterationDecision::Break;
  883. });
  884. }
  885. ASSERT(new_main_thread);
  886. // NOTE: We create the new stack before disabling interrupts since it will zero-fault
  887. // and we don't want to deal with faults after this point.
  888. u32 new_userspace_esp = new_main_thread->make_userspace_stack_for_main_thread(move(arguments), move(environment));
  889. // We cli() manually here because we don't want to get interrupted between do_exec() and Schedule::yield().
  890. // The reason is that the task redirection we've set up above will be clobbered by the timer IRQ.
  891. // If we used an InterruptDisabler that sti()'d on exit, we might timer tick'd too soon in exec().
  892. if (Process::current == this)
  893. cli();
  894. // NOTE: Be careful to not trigger any page faults below!
  895. Scheduler::prepare_to_modify_tss(*new_main_thread);
  896. m_name = parts.take_last();
  897. new_main_thread->set_name(m_name);
  898. auto& tss = new_main_thread->m_tss;
  899. u32 old_esp0 = tss.esp0;
  900. m_master_tls_size = master_tls_size;
  901. m_master_tls_alignment = master_tls_alignment;
  902. m_pid = new_main_thread->tid();
  903. new_main_thread->make_thread_specific_region({});
  904. new_main_thread->reset_fpu_state();
  905. memset(&tss, 0, sizeof(TSS32));
  906. tss.iomapbase = sizeof(TSS32);
  907. tss.eflags = 0x0202;
  908. tss.eip = entry_eip;
  909. tss.cs = 0x1b;
  910. tss.ds = 0x23;
  911. tss.es = 0x23;
  912. tss.fs = 0x23;
  913. tss.gs = thread_specific_selector() | 3;
  914. tss.ss = 0x23;
  915. tss.cr3 = page_directory().cr3();
  916. tss.esp = new_userspace_esp;
  917. tss.ss0 = 0x10;
  918. tss.esp0 = old_esp0;
  919. tss.ss2 = m_pid;
  920. #ifdef TASK_DEBUG
  921. klog() << "Process exec'd " << path.characters() << " @ " << String::format("%p", tss.eip);
  922. #endif
  923. if (was_profiling)
  924. Profiling::did_exec(path);
  925. new_main_thread->set_state(Thread::State::Skip1SchedulerPass);
  926. big_lock().force_unlock_if_locked();
  927. return 0;
  928. }
  929. static KResultOr<Vector<String>> find_shebang_interpreter_for_executable(const char first_page[], int nread)
  930. {
  931. int word_start = 2;
  932. int word_length = 0;
  933. if (nread > 2 && first_page[0] == '#' && first_page[1] == '!') {
  934. Vector<String> interpreter_words;
  935. for (int i = 2; i < nread; ++i) {
  936. if (first_page[i] == '\n') {
  937. break;
  938. }
  939. if (first_page[i] != ' ') {
  940. ++word_length;
  941. }
  942. if (first_page[i] == ' ') {
  943. if (word_length > 0) {
  944. interpreter_words.append(String(&first_page[word_start], word_length));
  945. }
  946. word_length = 0;
  947. word_start = i + 1;
  948. }
  949. }
  950. if (word_length > 0)
  951. interpreter_words.append(String(&first_page[word_start], word_length));
  952. if (!interpreter_words.is_empty())
  953. return interpreter_words;
  954. }
  955. return KResult(-ENOEXEC);
  956. }
  957. KResultOr<NonnullRefPtr<FileDescription>> Process::find_elf_interpreter_for_executable(const String& path, char (&first_page)[PAGE_SIZE], int nread, size_t file_size)
  958. {
  959. if (nread < (int)sizeof(Elf32_Ehdr))
  960. return KResult(-ENOEXEC);
  961. auto elf_header = (Elf32_Ehdr*)first_page;
  962. if (!ELF::validate_elf_header(*elf_header, file_size)) {
  963. dbg() << "exec(" << path << "): File has invalid ELF header";
  964. return KResult(-ENOEXEC);
  965. }
  966. // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
  967. String interpreter_path;
  968. if (!ELF::validate_program_headers(*elf_header, file_size, (u8*)first_page, nread, interpreter_path)) {
  969. dbg() << "exec(" << path << "): File has invalid ELF Program headers";
  970. return KResult(-ENOEXEC);
  971. }
  972. if (!interpreter_path.is_empty()) {
  973. // Programs with an interpreter better be relocatable executables or we don't know what to do...
  974. if (elf_header->e_type != ET_DYN)
  975. return KResult(-ENOEXEC);
  976. dbg() << "exec(" << path << "): Using program interpreter " << interpreter_path;
  977. auto interp_result = VFS::the().open(interpreter_path, O_EXEC, 0, current_directory());
  978. if (interp_result.is_error()) {
  979. dbg() << "exec(" << path << "): Unable to open program interpreter " << interpreter_path;
  980. return interp_result.error();
  981. }
  982. auto interpreter_description = interp_result.value();
  983. auto interp_metadata = interpreter_description->metadata();
  984. ASSERT(interpreter_description->inode());
  985. // Validate the program interpreter as a valid elf binary.
  986. // If your program interpreter is a #! file or something, it's time to stop playing games :)
  987. if (interp_metadata.size < (int)sizeof(Elf32_Ehdr))
  988. return KResult(-ENOEXEC);
  989. memset(first_page, 0, sizeof(first_page));
  990. nread = interpreter_description->read((u8*)&first_page, sizeof(first_page));
  991. if (nread < (int)sizeof(Elf32_Ehdr))
  992. return KResult(-ENOEXEC);
  993. elf_header = (Elf32_Ehdr*)first_page;
  994. if (!ELF::validate_elf_header(*elf_header, interp_metadata.size)) {
  995. dbg() << "exec(" << path << "): Interpreter (" << interpreter_description->absolute_path() << ") has invalid ELF header";
  996. return KResult(-ENOEXEC);
  997. }
  998. // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
  999. String interpreter_interpreter_path;
  1000. if (!ELF::validate_program_headers(*elf_header, interp_metadata.size, (u8*)first_page, nread, interpreter_interpreter_path)) {
  1001. dbg() << "exec(" << path << "): Interpreter (" << interpreter_description->absolute_path() << ") has invalid ELF Program headers";
  1002. return KResult(-ENOEXEC);
  1003. }
  1004. if (!interpreter_interpreter_path.is_empty()) {
  1005. dbg() << "exec(" << path << "): Interpreter (" << interpreter_description->absolute_path() << ") has its own interpreter (" << interpreter_interpreter_path << ")! No thank you!";
  1006. return KResult(-ELOOP);
  1007. }
  1008. return interpreter_description;
  1009. }
  1010. if (elf_header->e_type != ET_EXEC) {
  1011. // We can't exec an ET_REL, that's just an object file from the compiler
  1012. // If it's ET_DYN with no PT_INTERP, then we can't load it properly either
  1013. return KResult(-ENOEXEC);
  1014. }
  1015. // No interpreter, but, path refers to a valid elf image
  1016. return KResult(KSuccess);
  1017. }
  1018. int Process::exec(String path, Vector<String> arguments, Vector<String> environment, int recursion_depth)
  1019. {
  1020. if (recursion_depth > 2) {
  1021. dbg() << "exec(" << path << "): SHENANIGANS! recursed too far trying to find #! interpreter";
  1022. return -ELOOP;
  1023. }
  1024. // Open the file to check what kind of binary format it is
  1025. // Currently supported formats:
  1026. // - #! interpreted file
  1027. // - ELF32
  1028. // * ET_EXEC binary that just gets loaded
  1029. // * ET_DYN binary that requires a program interpreter
  1030. //
  1031. auto result = VFS::the().open(path, O_EXEC, 0, current_directory());
  1032. if (result.is_error())
  1033. return result.error();
  1034. auto description = result.value();
  1035. auto metadata = description->metadata();
  1036. // Always gonna need at least 3 bytes. these are for #!X
  1037. if (metadata.size < 3)
  1038. return -ENOEXEC;
  1039. ASSERT(description->inode());
  1040. // Read the first page of the program into memory so we can validate the binfmt of it
  1041. char first_page[PAGE_SIZE];
  1042. int nread = description->read((u8*)&first_page, sizeof(first_page));
  1043. // 1) #! interpreted file
  1044. auto shebang_result = find_shebang_interpreter_for_executable(first_page, nread);
  1045. if (!shebang_result.is_error()) {
  1046. Vector<String> new_arguments(shebang_result.value());
  1047. new_arguments.append(path);
  1048. arguments.remove(0);
  1049. new_arguments.append(move(arguments));
  1050. return exec(shebang_result.value().first(), move(new_arguments), move(environment), ++recursion_depth);
  1051. }
  1052. // #2) ELF32 for i386
  1053. auto elf_result = find_elf_interpreter_for_executable(path, first_page, nread, metadata.size);
  1054. RefPtr<FileDescription> interpreter_description;
  1055. // We're getting either an interpreter, an error, or KSuccess (i.e. no interpreter but file checks out)
  1056. if (!elf_result.is_error())
  1057. interpreter_description = elf_result.value();
  1058. else if (elf_result.error().is_error())
  1059. return elf_result.error();
  1060. // The bulk of exec() is done by do_exec(), which ensures that all locals
  1061. // are cleaned up by the time we yield-teleport below.
  1062. int rc = do_exec(move(description), move(arguments), move(environment), move(interpreter_description));
  1063. m_exec_tid = 0;
  1064. if (rc < 0)
  1065. return rc;
  1066. if (m_wait_for_tracer_at_next_execve) {
  1067. ASSERT(Thread::current->state() == Thread::State::Skip1SchedulerPass);
  1068. // State::Skip1SchedulerPass is irrelevant since we block the thread
  1069. Thread::current->set_state(Thread::State::Running);
  1070. Thread::current->send_urgent_signal_to_self(SIGSTOP);
  1071. }
  1072. if (Process::current == this) {
  1073. Scheduler::yield();
  1074. ASSERT_NOT_REACHED();
  1075. }
  1076. return 0;
  1077. }
  1078. int Process::sys$execve(const Syscall::SC_execve_params* user_params)
  1079. {
  1080. REQUIRE_PROMISE(exec);
  1081. // NOTE: Be extremely careful with allocating any kernel memory in exec().
  1082. // On success, the kernel stack will be lost.
  1083. Syscall::SC_execve_params params;
  1084. if (!validate_read_and_copy_typed(&params, user_params))
  1085. return -EFAULT;
  1086. if (params.arguments.length > ARG_MAX || params.environment.length > ARG_MAX)
  1087. return -E2BIG;
  1088. if (m_wait_for_tracer_at_next_execve)
  1089. Thread::current->send_urgent_signal_to_self(SIGSTOP);
  1090. String path;
  1091. {
  1092. auto path_arg = get_syscall_path_argument(params.path);
  1093. if (path_arg.is_error())
  1094. return path_arg.error();
  1095. path = path_arg.value();
  1096. }
  1097. auto copy_user_strings = [&](const auto& list, auto& output) {
  1098. if (!list.length)
  1099. return true;
  1100. if (!validate_read_typed(list.strings, list.length))
  1101. return false;
  1102. Vector<Syscall::StringArgument, 32> strings;
  1103. strings.resize(list.length);
  1104. copy_from_user(strings.data(), list.strings, list.length * sizeof(Syscall::StringArgument));
  1105. for (size_t i = 0; i < list.length; ++i) {
  1106. auto string = validate_and_copy_string_from_user(strings[i]);
  1107. if (string.is_null())
  1108. return false;
  1109. output.append(move(string));
  1110. }
  1111. return true;
  1112. };
  1113. Vector<String> arguments;
  1114. if (!copy_user_strings(params.arguments, arguments))
  1115. return -EFAULT;
  1116. Vector<String> environment;
  1117. if (!copy_user_strings(params.environment, environment))
  1118. return -EFAULT;
  1119. int rc = exec(move(path), move(arguments), move(environment));
  1120. ASSERT(rc < 0); // We should never continue after a successful exec!
  1121. return rc;
  1122. }
  1123. Process* Process::create_user_process(Thread*& first_thread, const String& path, uid_t uid, gid_t gid, pid_t parent_pid, int& error, Vector<String>&& arguments, Vector<String>&& environment, TTY* tty)
  1124. {
  1125. auto parts = path.split('/');
  1126. if (arguments.is_empty()) {
  1127. arguments.append(parts.last());
  1128. }
  1129. RefPtr<Custody> cwd;
  1130. RefPtr<Custody> root;
  1131. {
  1132. InterruptDisabler disabler;
  1133. if (auto* parent = Process::from_pid(parent_pid)) {
  1134. cwd = parent->m_cwd;
  1135. root = parent->m_root_directory;
  1136. }
  1137. }
  1138. if (!cwd)
  1139. cwd = VFS::the().root_custody();
  1140. if (!root)
  1141. root = VFS::the().root_custody();
  1142. auto* process = new Process(first_thread, parts.take_last(), uid, gid, parent_pid, Ring3, move(cwd), nullptr, tty);
  1143. process->m_fds.resize(m_max_open_file_descriptors);
  1144. auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : NullDevice::the();
  1145. auto description = device_to_use_as_tty.open(O_RDWR).value();
  1146. process->m_fds[0].set(*description);
  1147. process->m_fds[1].set(*description);
  1148. process->m_fds[2].set(*description);
  1149. error = process->exec(path, move(arguments), move(environment));
  1150. if (error != 0) {
  1151. dbg() << "Failed to exec " << path << ": " << error;
  1152. delete first_thread;
  1153. delete process;
  1154. return nullptr;
  1155. }
  1156. {
  1157. InterruptDisabler disabler;
  1158. g_processes->prepend(process);
  1159. }
  1160. #ifdef TASK_DEBUG
  1161. klog() << "Process " << process->pid() << " (" << process->name().characters() << ") spawned @ " << String::format("%p", first_thread->tss().eip);
  1162. #endif
  1163. error = 0;
  1164. return process;
  1165. }
  1166. Process* Process::create_kernel_process(Thread*& first_thread, String&& name, void (*e)())
  1167. {
  1168. auto* process = new Process(first_thread, move(name), (uid_t)0, (gid_t)0, (pid_t)0, Ring0);
  1169. first_thread->tss().eip = (FlatPtr)e;
  1170. if (process->pid() != 0) {
  1171. InterruptDisabler disabler;
  1172. g_processes->prepend(process);
  1173. #ifdef TASK_DEBUG
  1174. klog() << "Kernel process " << process->pid() << " (" << process->name().characters() << ") spawned @ " << String::format("%p", first_thread->tss().eip);
  1175. #endif
  1176. }
  1177. first_thread->set_state(Thread::State::Runnable);
  1178. return process;
  1179. }
  1180. Process::Process(Thread*& first_thread, const String& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
  1181. : m_name(move(name))
  1182. , m_pid(allocate_pid())
  1183. , m_euid(uid)
  1184. , m_egid(gid)
  1185. , m_uid(uid)
  1186. , m_gid(gid)
  1187. , m_suid(uid)
  1188. , m_sgid(gid)
  1189. , m_ring(ring)
  1190. , m_executable(move(executable))
  1191. , m_cwd(move(cwd))
  1192. , m_tty(tty)
  1193. , m_ppid(ppid)
  1194. {
  1195. #ifdef PROCESS_DEBUG
  1196. dbg() << "Created new process " << m_name << "(" << m_pid << ")";
  1197. #endif
  1198. m_page_directory = PageDirectory::create_for_userspace(*this, fork_parent ? &fork_parent->page_directory().range_allocator() : nullptr);
  1199. #ifdef MM_DEBUG
  1200. dbg() << "Process " << pid() << " ctor: PD=" << m_page_directory.ptr() << " created";
  1201. #endif
  1202. if (fork_parent) {
  1203. // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
  1204. first_thread = Thread::current->clone(*this);
  1205. } else {
  1206. // NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
  1207. first_thread = new Thread(*this);
  1208. }
  1209. }
  1210. Process::~Process()
  1211. {
  1212. ASSERT(thread_count() == 0);
  1213. }
  1214. void Process::dump_regions()
  1215. {
  1216. klog() << "Process regions:";
  1217. klog() << "BEGIN END SIZE ACCESS NAME";
  1218. for (auto& region : m_regions) {
  1219. klog() << String::format("%08x", region.vaddr().get()) << " -- " << String::format("%08x", region.vaddr().offset(region.size() - 1).get()) << " " << String::format("%08x", region.size()) << " " << (region.is_readable() ? 'R' : ' ') << (region.is_writable() ? 'W' : ' ') << (region.is_executable() ? 'X' : ' ') << (region.is_shared() ? 'S' : ' ') << (region.is_stack() ? 'T' : ' ') << (region.vmobject().is_purgeable() ? 'P' : ' ') << " " << region.name().characters();
  1220. }
  1221. MM.dump_kernel_regions();
  1222. }
  1223. void Process::sys$exit(int status)
  1224. {
  1225. cli();
  1226. #ifdef TASK_DEBUG
  1227. klog() << "sys$exit: exit with status " << status;
  1228. #endif
  1229. if (status != 0)
  1230. dump_backtrace();
  1231. m_termination_status = status;
  1232. m_termination_signal = 0;
  1233. die();
  1234. Thread::current->die_if_needed();
  1235. ASSERT_NOT_REACHED();
  1236. }
  1237. void signal_trampoline_dummy(void)
  1238. {
  1239. // The trampoline preserves the current eax, pushes the signal code and
  1240. // then calls the signal handler. We do this because, when interrupting a
  1241. // blocking syscall, that syscall may return some special error code in eax;
  1242. // This error code would likely be overwritten by the signal handler, so it's
  1243. // neccessary to preserve it here.
  1244. asm(
  1245. ".intel_syntax noprefix\n"
  1246. "asm_signal_trampoline:\n"
  1247. "push ebp\n"
  1248. "mov ebp, esp\n"
  1249. "push eax\n" // we have to store eax 'cause it might be the return value from a syscall
  1250. "sub esp, 4\n" // align the stack to 16 bytes
  1251. "mov eax, [ebp+12]\n" // push the signal code
  1252. "push eax\n"
  1253. "call [ebp+8]\n" // call the signal handler
  1254. "add esp, 8\n"
  1255. "mov eax, %P0\n"
  1256. "int 0x82\n" // sigreturn syscall
  1257. "asm_signal_trampoline_end:\n"
  1258. ".att_syntax" ::"i"(Syscall::SC_sigreturn));
  1259. }
  1260. extern "C" void asm_signal_trampoline(void);
  1261. extern "C" void asm_signal_trampoline_end(void);
  1262. void create_signal_trampolines()
  1263. {
  1264. InterruptDisabler disabler;
  1265. // NOTE: We leak this region.
  1266. auto* trampoline_region = MM.allocate_user_accessible_kernel_region(PAGE_SIZE, "Signal trampolines", Region::Access::Read | Region::Access::Write | Region::Access::Execute, false).leak_ptr();
  1267. g_return_to_ring3_from_signal_trampoline = trampoline_region->vaddr();
  1268. u8* trampoline = (u8*)asm_signal_trampoline;
  1269. u8* trampoline_end = (u8*)asm_signal_trampoline_end;
  1270. size_t trampoline_size = trampoline_end - trampoline;
  1271. {
  1272. SmapDisabler disabler;
  1273. u8* code_ptr = (u8*)trampoline_region->vaddr().as_ptr();
  1274. memcpy(code_ptr, trampoline, trampoline_size);
  1275. }
  1276. trampoline_region->set_writable(false);
  1277. trampoline_region->remap();
  1278. }
  1279. int Process::sys$sigreturn(RegisterState& registers)
  1280. {
  1281. REQUIRE_PROMISE(stdio);
  1282. SmapDisabler disabler;
  1283. //Here, we restore the state pushed by dispatch signal and asm_signal_trampoline.
  1284. u32* stack_ptr = (u32*)registers.userspace_esp;
  1285. u32 smuggled_eax = *stack_ptr;
  1286. //pop the stored eax, ebp, return address, handler and signal code
  1287. stack_ptr += 5;
  1288. Thread::current->m_signal_mask = *stack_ptr;
  1289. stack_ptr++;
  1290. //pop edi, esi, ebp, esp, ebx, edx, ecx and eax
  1291. memcpy(&registers.edi, stack_ptr, 8 * sizeof(FlatPtr));
  1292. stack_ptr += 8;
  1293. registers.eip = *stack_ptr;
  1294. stack_ptr++;
  1295. registers.eflags = *stack_ptr;
  1296. stack_ptr++;
  1297. registers.userspace_esp = registers.esp;
  1298. return smuggled_eax;
  1299. }
  1300. void Process::crash(int signal, u32 eip, bool out_of_memory)
  1301. {
  1302. ASSERT_INTERRUPTS_DISABLED();
  1303. ASSERT(!is_dead());
  1304. ASSERT(Process::current == this);
  1305. if (out_of_memory) {
  1306. dbg() << "\033[31;1mOut of memory\033[m, killing: " << *this;
  1307. } else {
  1308. if (eip >= 0xc0000000 && g_kernel_symbols_available) {
  1309. auto* symbol = symbolicate_kernel_address(eip);
  1310. dbg() << "\033[31;1m" << String::format("%p", eip) << " " << (symbol ? demangle(symbol->name) : "(k?)") << " +" << (symbol ? eip - symbol->address : 0) << "\033[0m\n";
  1311. } else if (auto elf_bundle = this->elf_bundle()) {
  1312. dbg() << "\033[31;1m" << String::format("%p", eip) << " " << elf_bundle->elf_loader->symbolicate(eip) << "\033[0m\n";
  1313. } else {
  1314. dbg() << "\033[31;1m" << String::format("%p", eip) << " (?)\033[0m\n";
  1315. }
  1316. dump_backtrace();
  1317. }
  1318. m_termination_signal = signal;
  1319. dump_regions();
  1320. ASSERT(is_ring3());
  1321. die();
  1322. // We can not return from here, as there is nowhere
  1323. // to unwind to, so die right away.
  1324. Thread::current->die_if_needed();
  1325. ASSERT_NOT_REACHED();
  1326. }
  1327. Process* Process::from_pid(pid_t pid)
  1328. {
  1329. ASSERT_INTERRUPTS_DISABLED();
  1330. for (auto& process : *g_processes) {
  1331. if (process.pid() == pid)
  1332. return &process;
  1333. }
  1334. return nullptr;
  1335. }
  1336. RefPtr<FileDescription> Process::file_description(int fd) const
  1337. {
  1338. if (fd < 0)
  1339. return nullptr;
  1340. if (static_cast<size_t>(fd) < m_fds.size())
  1341. return m_fds[fd].description.ptr();
  1342. return nullptr;
  1343. }
  1344. int Process::fd_flags(int fd) const
  1345. {
  1346. if (fd < 0)
  1347. return -1;
  1348. if (static_cast<size_t>(fd) < m_fds.size())
  1349. return m_fds[fd].flags;
  1350. return -1;
  1351. }
  1352. ssize_t Process::sys$get_dir_entries(int fd, void* buffer, ssize_t size)
  1353. {
  1354. REQUIRE_PROMISE(stdio);
  1355. if (size < 0)
  1356. return -EINVAL;
  1357. if (!validate_write(buffer, size))
  1358. return -EFAULT;
  1359. auto description = file_description(fd);
  1360. if (!description)
  1361. return -EBADF;
  1362. return description->get_dir_entries((u8*)buffer, size);
  1363. }
  1364. int Process::sys$lseek(int fd, off_t offset, int whence)
  1365. {
  1366. REQUIRE_PROMISE(stdio);
  1367. auto description = file_description(fd);
  1368. if (!description)
  1369. return -EBADF;
  1370. return description->seek(offset, whence);
  1371. }
  1372. int Process::sys$ttyname_r(int fd, char* buffer, ssize_t size)
  1373. {
  1374. REQUIRE_PROMISE(tty);
  1375. if (size < 0)
  1376. return -EINVAL;
  1377. if (!validate_write(buffer, size))
  1378. return -EFAULT;
  1379. auto description = file_description(fd);
  1380. if (!description)
  1381. return -EBADF;
  1382. if (!description->is_tty())
  1383. return -ENOTTY;
  1384. String tty_name = description->tty()->tty_name();
  1385. if ((size_t)size < tty_name.length() + 1)
  1386. return -ERANGE;
  1387. copy_to_user(buffer, tty_name.characters(), tty_name.length() + 1);
  1388. return 0;
  1389. }
  1390. int Process::sys$ptsname_r(int fd, char* buffer, ssize_t size)
  1391. {
  1392. REQUIRE_PROMISE(tty);
  1393. if (size < 0)
  1394. return -EINVAL;
  1395. if (!validate_write(buffer, size))
  1396. return -EFAULT;
  1397. auto description = file_description(fd);
  1398. if (!description)
  1399. return -EBADF;
  1400. auto* master_pty = description->master_pty();
  1401. if (!master_pty)
  1402. return -ENOTTY;
  1403. auto pts_name = master_pty->pts_name();
  1404. if ((size_t)size < pts_name.length() + 1)
  1405. return -ERANGE;
  1406. copy_to_user(buffer, pts_name.characters(), pts_name.length() + 1);
  1407. return 0;
  1408. }
  1409. ssize_t Process::sys$writev(int fd, const struct iovec* iov, int iov_count)
  1410. {
  1411. REQUIRE_PROMISE(stdio);
  1412. if (iov_count < 0)
  1413. return -EINVAL;
  1414. if (!validate_read_typed(iov, iov_count))
  1415. return -EFAULT;
  1416. u64 total_length = 0;
  1417. Vector<iovec, 32> vecs;
  1418. vecs.resize(iov_count);
  1419. copy_from_user(vecs.data(), iov, iov_count * sizeof(iovec));
  1420. for (auto& vec : vecs) {
  1421. if (!validate_read(vec.iov_base, vec.iov_len))
  1422. return -EFAULT;
  1423. total_length += vec.iov_len;
  1424. if (total_length > INT32_MAX)
  1425. return -EINVAL;
  1426. }
  1427. auto description = file_description(fd);
  1428. if (!description)
  1429. return -EBADF;
  1430. if (!description->is_writable())
  1431. return -EBADF;
  1432. int nwritten = 0;
  1433. for (auto& vec : vecs) {
  1434. int rc = do_write(*description, (const u8*)vec.iov_base, vec.iov_len);
  1435. if (rc < 0) {
  1436. if (nwritten == 0)
  1437. return rc;
  1438. return nwritten;
  1439. }
  1440. nwritten += rc;
  1441. }
  1442. return nwritten;
  1443. }
  1444. ssize_t Process::do_write(FileDescription& description, const u8* data, int data_size)
  1445. {
  1446. ssize_t nwritten = 0;
  1447. if (!description.is_blocking()) {
  1448. if (!description.can_write())
  1449. return -EAGAIN;
  1450. }
  1451. if (description.should_append()) {
  1452. #ifdef IO_DEBUG
  1453. dbg() << "seeking to end (O_APPEND)";
  1454. #endif
  1455. description.seek(0, SEEK_END);
  1456. }
  1457. while (nwritten < data_size) {
  1458. #ifdef IO_DEBUG
  1459. dbg() << "while " << nwritten << " < " << size;
  1460. #endif
  1461. if (!description.can_write()) {
  1462. #ifdef IO_DEBUG
  1463. dbg() << "block write on " << description.absolute_path();
  1464. #endif
  1465. if (Thread::current->block<Thread::WriteBlocker>(description) != Thread::BlockResult::WokeNormally) {
  1466. if (nwritten == 0)
  1467. return -EINTR;
  1468. }
  1469. }
  1470. ssize_t rc = description.write(data + nwritten, data_size - nwritten);
  1471. #ifdef IO_DEBUG
  1472. dbg() << " -> write returned " << rc;
  1473. #endif
  1474. if (rc < 0) {
  1475. if (nwritten)
  1476. return nwritten;
  1477. return rc;
  1478. }
  1479. if (rc == 0)
  1480. break;
  1481. nwritten += rc;
  1482. }
  1483. return nwritten;
  1484. }
  1485. ssize_t Process::sys$write(int fd, const u8* data, ssize_t size)
  1486. {
  1487. REQUIRE_PROMISE(stdio);
  1488. if (size < 0)
  1489. return -EINVAL;
  1490. if (size == 0)
  1491. return 0;
  1492. if (!validate_read(data, size))
  1493. return -EFAULT;
  1494. #ifdef DEBUG_IO
  1495. dbg() << "sys$write(" << fd << ", " << (const void*)(data) << ", " << size << ")";
  1496. #endif
  1497. auto description = file_description(fd);
  1498. if (!description)
  1499. return -EBADF;
  1500. if (!description->is_writable())
  1501. return -EBADF;
  1502. return do_write(*description, data, size);
  1503. }
  1504. ssize_t Process::sys$read(int fd, u8* buffer, ssize_t size)
  1505. {
  1506. REQUIRE_PROMISE(stdio);
  1507. if (size < 0)
  1508. return -EINVAL;
  1509. if (size == 0)
  1510. return 0;
  1511. if (!validate_write(buffer, size))
  1512. return -EFAULT;
  1513. #ifdef DEBUG_IO
  1514. dbg() << "sys$read(" << fd << ", " << (const void*)buffer << ", " << size << ")";
  1515. #endif
  1516. auto description = file_description(fd);
  1517. if (!description)
  1518. return -EBADF;
  1519. if (!description->is_readable())
  1520. return -EBADF;
  1521. if (description->is_directory())
  1522. return -EISDIR;
  1523. if (description->is_blocking()) {
  1524. if (!description->can_read()) {
  1525. if (Thread::current->block<Thread::ReadBlocker>(*description) != Thread::BlockResult::WokeNormally)
  1526. return -EINTR;
  1527. if (!description->can_read())
  1528. return -EAGAIN;
  1529. }
  1530. }
  1531. return description->read(buffer, size);
  1532. }
  1533. int Process::sys$close(int fd)
  1534. {
  1535. REQUIRE_PROMISE(stdio);
  1536. auto description = file_description(fd);
  1537. #ifdef DEBUG_IO
  1538. dbg() << "sys$close(" << fd << ") " << description.ptr();
  1539. #endif
  1540. if (!description)
  1541. return -EBADF;
  1542. int rc = description->close();
  1543. m_fds[fd] = {};
  1544. return rc;
  1545. }
  1546. int Process::sys$utime(const char* user_path, size_t path_length, const utimbuf* user_buf)
  1547. {
  1548. REQUIRE_PROMISE(fattr);
  1549. if (user_buf && !validate_read_typed(user_buf))
  1550. return -EFAULT;
  1551. auto path = get_syscall_path_argument(user_path, path_length);
  1552. if (path.is_error())
  1553. return path.error();
  1554. utimbuf buf;
  1555. if (user_buf) {
  1556. copy_from_user(&buf, user_buf);
  1557. } else {
  1558. auto now = kgettimeofday();
  1559. buf = { now.tv_sec, now.tv_sec };
  1560. }
  1561. return VFS::the().utime(path.value(), current_directory(), buf.actime, buf.modtime);
  1562. }
  1563. int Process::sys$access(const char* user_path, size_t path_length, int mode)
  1564. {
  1565. REQUIRE_PROMISE(rpath);
  1566. auto path = get_syscall_path_argument(user_path, path_length);
  1567. if (path.is_error())
  1568. return path.error();
  1569. return VFS::the().access(path.value(), mode, current_directory());
  1570. }
  1571. int Process::sys$fcntl(int fd, int cmd, u32 arg)
  1572. {
  1573. REQUIRE_PROMISE(stdio);
  1574. #ifdef DEBUG_IO
  1575. dbg() << "sys$fcntl: fd=" << fd << ", cmd=" << cmd << ", arg=" << arg;
  1576. #endif
  1577. auto description = file_description(fd);
  1578. if (!description)
  1579. return -EBADF;
  1580. // NOTE: The FD flags are not shared between FileDescription objects.
  1581. // This means that dup() doesn't copy the FD_CLOEXEC flag!
  1582. switch (cmd) {
  1583. case F_DUPFD: {
  1584. int arg_fd = (int)arg;
  1585. if (arg_fd < 0)
  1586. return -EINVAL;
  1587. int new_fd = alloc_fd(arg_fd);
  1588. if (new_fd < 0)
  1589. return new_fd;
  1590. m_fds[new_fd].set(*description);
  1591. return new_fd;
  1592. }
  1593. case F_GETFD:
  1594. return m_fds[fd].flags;
  1595. case F_SETFD:
  1596. m_fds[fd].flags = arg;
  1597. break;
  1598. case F_GETFL:
  1599. return description->file_flags();
  1600. case F_SETFL:
  1601. description->set_file_flags(arg);
  1602. break;
  1603. case F_ISTTY:
  1604. return description->is_tty();
  1605. default:
  1606. return -EINVAL;
  1607. }
  1608. return 0;
  1609. }
  1610. int Process::sys$fstat(int fd, stat* user_statbuf)
  1611. {
  1612. REQUIRE_PROMISE(stdio);
  1613. if (!validate_write_typed(user_statbuf))
  1614. return -EFAULT;
  1615. auto description = file_description(fd);
  1616. if (!description)
  1617. return -EBADF;
  1618. stat buffer;
  1619. memset(&buffer, 0, sizeof(buffer));
  1620. int rc = description->fstat(buffer);
  1621. copy_to_user(user_statbuf, &buffer);
  1622. return rc;
  1623. }
  1624. int Process::sys$stat(const Syscall::SC_stat_params* user_params)
  1625. {
  1626. REQUIRE_PROMISE(rpath);
  1627. Syscall::SC_stat_params params;
  1628. if (!validate_read_and_copy_typed(&params, user_params))
  1629. return -EFAULT;
  1630. if (!validate_write_typed(params.statbuf))
  1631. return -EFAULT;
  1632. auto path = get_syscall_path_argument(params.path);
  1633. if (path.is_error())
  1634. return path.error();
  1635. auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory(), params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR);
  1636. if (metadata_or_error.is_error())
  1637. return metadata_or_error.error();
  1638. stat statbuf;
  1639. auto result = metadata_or_error.value().stat(statbuf);
  1640. if (result.is_error())
  1641. return result;
  1642. copy_to_user(params.statbuf, &statbuf);
  1643. return 0;
  1644. }
  1645. template<typename DataType, typename SizeType>
  1646. bool Process::validate(const Syscall::MutableBufferArgument<DataType, SizeType>& buffer)
  1647. {
  1648. return validate_write(buffer.data, buffer.size);
  1649. }
  1650. template<typename DataType, typename SizeType>
  1651. bool Process::validate(const Syscall::ImmutableBufferArgument<DataType, SizeType>& buffer)
  1652. {
  1653. return validate_read(buffer.data, buffer.size);
  1654. }
  1655. String Process::validate_and_copy_string_from_user(const char* user_characters, size_t user_length) const
  1656. {
  1657. if (user_length == 0)
  1658. return String::empty();
  1659. if (!user_characters)
  1660. return {};
  1661. if (!validate_read(user_characters, user_length))
  1662. return {};
  1663. SmapDisabler disabler;
  1664. size_t measured_length = strnlen(user_characters, user_length);
  1665. return String(user_characters, measured_length);
  1666. }
  1667. String Process::validate_and_copy_string_from_user(const Syscall::StringArgument& string) const
  1668. {
  1669. return validate_and_copy_string_from_user(string.characters, string.length);
  1670. }
  1671. int Process::sys$readlink(const Syscall::SC_readlink_params* user_params)
  1672. {
  1673. REQUIRE_PROMISE(rpath);
  1674. Syscall::SC_readlink_params params;
  1675. if (!validate_read_and_copy_typed(&params, user_params))
  1676. return -EFAULT;
  1677. if (!validate(params.buffer))
  1678. return -EFAULT;
  1679. auto path = get_syscall_path_argument(params.path);
  1680. if (path.is_error())
  1681. return path.error();
  1682. auto result = VFS::the().open(path.value(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, current_directory());
  1683. if (result.is_error())
  1684. return result.error();
  1685. auto description = result.value();
  1686. if (!description->metadata().is_symlink())
  1687. return -EINVAL;
  1688. auto contents = description->read_entire_file();
  1689. if (contents.is_error())
  1690. return contents.error();
  1691. auto& link_target = contents.value();
  1692. auto size_to_copy = min(link_target.size(), params.buffer.size);
  1693. copy_to_user(params.buffer.data, link_target.data(), size_to_copy);
  1694. // Note: we return the whole size here, not the copied size.
  1695. return link_target.size();
  1696. }
  1697. int Process::sys$chdir(const char* user_path, size_t path_length)
  1698. {
  1699. REQUIRE_PROMISE(rpath);
  1700. auto path = get_syscall_path_argument(user_path, path_length);
  1701. if (path.is_error())
  1702. return path.error();
  1703. auto directory_or_error = VFS::the().open_directory(path.value(), current_directory());
  1704. if (directory_or_error.is_error())
  1705. return directory_or_error.error();
  1706. m_cwd = *directory_or_error.value();
  1707. return 0;
  1708. }
  1709. int Process::sys$fchdir(int fd)
  1710. {
  1711. REQUIRE_PROMISE(stdio);
  1712. auto description = file_description(fd);
  1713. if (!description)
  1714. return -EBADF;
  1715. if (!description->is_directory())
  1716. return -ENOTDIR;
  1717. if (!description->metadata().may_execute(*this))
  1718. return -EACCES;
  1719. m_cwd = description->custody();
  1720. return 0;
  1721. }
  1722. int Process::sys$getcwd(char* buffer, ssize_t size)
  1723. {
  1724. REQUIRE_PROMISE(rpath);
  1725. if (size < 0)
  1726. return -EINVAL;
  1727. if (!validate_write(buffer, size))
  1728. return -EFAULT;
  1729. auto path = current_directory().absolute_path();
  1730. if ((size_t)size < path.length() + 1)
  1731. return -ERANGE;
  1732. copy_to_user(buffer, path.characters(), path.length() + 1);
  1733. return 0;
  1734. }
  1735. int Process::number_of_open_file_descriptors() const
  1736. {
  1737. int count = 0;
  1738. for (auto& description : m_fds) {
  1739. if (description)
  1740. ++count;
  1741. }
  1742. return count;
  1743. }
  1744. int Process::sys$open(const Syscall::SC_open_params* user_params)
  1745. {
  1746. Syscall::SC_open_params params;
  1747. if (!validate_read_and_copy_typed(&params, user_params))
  1748. return -EFAULT;
  1749. int dirfd = params.dirfd;
  1750. int options = params.options;
  1751. u16 mode = params.mode;
  1752. if (options & O_NOFOLLOW_NOERROR)
  1753. return -EINVAL;
  1754. if (options & O_UNLINK_INTERNAL)
  1755. return -EINVAL;
  1756. if (options & O_WRONLY)
  1757. REQUIRE_PROMISE(wpath);
  1758. else if (options & O_RDONLY)
  1759. REQUIRE_PROMISE(rpath);
  1760. if (options & O_CREAT)
  1761. REQUIRE_PROMISE(cpath);
  1762. // Ignore everything except permission bits.
  1763. mode &= 04777;
  1764. auto path = get_syscall_path_argument(params.path);
  1765. if (path.is_error())
  1766. return path.error();
  1767. #ifdef DEBUG_IO
  1768. dbg() << "sys$open(dirfd=" << dirfd << ", path=\"" << path.value() << "\", options=" << options << ", mode=" << mode << ")";
  1769. #endif
  1770. int fd = alloc_fd();
  1771. if (fd < 0)
  1772. return fd;
  1773. RefPtr<Custody> base;
  1774. if (dirfd == AT_FDCWD) {
  1775. base = current_directory();
  1776. } else {
  1777. auto base_description = file_description(dirfd);
  1778. if (!base_description)
  1779. return -EBADF;
  1780. if (!base_description->is_directory())
  1781. return -ENOTDIR;
  1782. if (!base_description->custody())
  1783. return -EINVAL;
  1784. base = base_description->custody();
  1785. }
  1786. auto result = VFS::the().open(path.value(), options, mode & ~umask(), *base);
  1787. if (result.is_error())
  1788. return result.error();
  1789. auto description = result.value();
  1790. if (description->inode() && description->inode()->socket())
  1791. return -ENXIO;
  1792. u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
  1793. m_fds[fd].set(move(description), fd_flags);
  1794. return fd;
  1795. }
  1796. int Process::alloc_fd(int first_candidate_fd)
  1797. {
  1798. for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
  1799. if (!m_fds[i])
  1800. return i;
  1801. }
  1802. return -EMFILE;
  1803. }
  1804. int Process::sys$pipe(int pipefd[2], int flags)
  1805. {
  1806. REQUIRE_PROMISE(stdio);
  1807. if (!validate_write_typed(pipefd))
  1808. return -EFAULT;
  1809. if (number_of_open_file_descriptors() + 2 > max_open_file_descriptors())
  1810. return -EMFILE;
  1811. // Reject flags other than O_CLOEXEC.
  1812. if ((flags & O_CLOEXEC) != flags)
  1813. return -EINVAL;
  1814. u32 fd_flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
  1815. auto fifo = FIFO::create(m_uid);
  1816. int reader_fd = alloc_fd();
  1817. m_fds[reader_fd].set(fifo->open_direction(FIFO::Direction::Reader), fd_flags);
  1818. m_fds[reader_fd].description->set_readable(true);
  1819. copy_to_user(&pipefd[0], &reader_fd);
  1820. int writer_fd = alloc_fd();
  1821. m_fds[writer_fd].set(fifo->open_direction(FIFO::Direction::Writer), fd_flags);
  1822. m_fds[writer_fd].description->set_writable(true);
  1823. copy_to_user(&pipefd[1], &writer_fd);
  1824. return 0;
  1825. }
  1826. int Process::sys$killpg(int pgrp, int signum)
  1827. {
  1828. REQUIRE_PROMISE(proc);
  1829. if (signum < 1 || signum >= 32)
  1830. return -EINVAL;
  1831. if (pgrp < 0)
  1832. return -EINVAL;
  1833. return do_killpg(pgrp, signum);
  1834. }
  1835. int Process::sys$seteuid(uid_t euid)
  1836. {
  1837. REQUIRE_PROMISE(id);
  1838. // This has FreeBSD semantics.
  1839. // Linux and Solaris also allow m_euid.
  1840. if (euid != m_uid && euid != m_suid && !is_superuser())
  1841. return -EPERM;
  1842. m_euid = euid;
  1843. return 0;
  1844. }
  1845. int Process::sys$setegid(gid_t egid)
  1846. {
  1847. REQUIRE_PROMISE(id);
  1848. // This has FreeBSD semantics.
  1849. // Linux and Solaris also allow m_egid.
  1850. if (egid != m_gid && egid != m_sgid && !is_superuser())
  1851. return -EPERM;
  1852. m_egid = egid;
  1853. return 0;
  1854. }
  1855. int Process::sys$setuid(uid_t uid)
  1856. {
  1857. REQUIRE_PROMISE(id);
  1858. // Linux and Solaris require real or saved.
  1859. // FreeBSD requires real or effective.
  1860. if (uid != m_uid && uid != m_euid && !is_superuser())
  1861. return -EPERM;
  1862. // Solaris and Linux only set uid and suid if is_superuser(),
  1863. // FreeBSD always sets all 3.
  1864. m_uid = uid;
  1865. m_euid = uid;
  1866. m_suid = uid;
  1867. return 0;
  1868. }
  1869. int Process::sys$setgid(gid_t gid)
  1870. {
  1871. REQUIRE_PROMISE(id);
  1872. // Linux and Solaris require real or saved.
  1873. // FreeBSD requires real or effective.
  1874. if (gid != m_gid && gid != m_egid && !is_superuser())
  1875. return -EPERM;
  1876. // Solaris and Linux only set uid and suid if is_superuser(),
  1877. // FreeBSD always sets all 3.
  1878. m_gid = gid;
  1879. m_egid = gid;
  1880. m_sgid = gid;
  1881. return 0;
  1882. }
  1883. int Process::sys$setresuid(uid_t ruid, uid_t euid, uid_t suid)
  1884. {
  1885. REQUIRE_PROMISE(id);
  1886. if (ruid == (uid_t)-1)
  1887. ruid = m_uid;
  1888. if (euid == (uid_t)-1)
  1889. euid = m_euid;
  1890. if (suid == (uid_t)-1)
  1891. suid = m_suid;
  1892. auto ok = [this](uid_t id) { return id == m_uid || id == m_euid || id == m_suid; };
  1893. if ((!ok(ruid) || !ok(euid) || !ok(suid)) && !is_superuser())
  1894. return -EPERM;
  1895. m_uid = ruid;
  1896. m_euid = euid;
  1897. m_suid = suid;
  1898. return 0;
  1899. }
  1900. int Process::sys$setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  1901. {
  1902. REQUIRE_PROMISE(id);
  1903. if (rgid == (gid_t)-1)
  1904. rgid = m_gid;
  1905. if (egid == (gid_t)-1)
  1906. egid = m_egid;
  1907. if (sgid == (gid_t)-1)
  1908. sgid = m_sgid;
  1909. auto ok = [this](gid_t id) { return id == m_gid || id == m_egid || id == m_sgid; };
  1910. if ((!ok(rgid) || !ok(egid) || !ok(sgid)) && !is_superuser())
  1911. return -EPERM;
  1912. m_gid = rgid;
  1913. m_egid = egid;
  1914. m_sgid = sgid;
  1915. return 0;
  1916. }
  1917. unsigned Process::sys$alarm(unsigned seconds)
  1918. {
  1919. REQUIRE_PROMISE(stdio);
  1920. unsigned previous_alarm_remaining = 0;
  1921. if (m_alarm_deadline && m_alarm_deadline > g_uptime) {
  1922. previous_alarm_remaining = (m_alarm_deadline - g_uptime) / TimeManagement::the().ticks_per_second();
  1923. }
  1924. if (!seconds) {
  1925. m_alarm_deadline = 0;
  1926. return previous_alarm_remaining;
  1927. }
  1928. m_alarm_deadline = g_uptime + seconds * TimeManagement::the().ticks_per_second();
  1929. return previous_alarm_remaining;
  1930. }
  1931. int Process::sys$uname(utsname* buf)
  1932. {
  1933. REQUIRE_PROMISE(stdio);
  1934. if (!validate_write_typed(buf))
  1935. return -EFAULT;
  1936. LOCKER(*s_hostname_lock, Lock::Mode::Shared);
  1937. if (s_hostname->length() + 1 > sizeof(utsname::nodename))
  1938. return -ENAMETOOLONG;
  1939. copy_to_user(buf->sysname, "SerenityOS", 11);
  1940. copy_to_user(buf->release, "1.0-dev", 8);
  1941. copy_to_user(buf->version, "FIXME", 6);
  1942. copy_to_user(buf->machine, "i686", 5);
  1943. copy_to_user(buf->nodename, s_hostname->characters(), s_hostname->length() + 1);
  1944. return 0;
  1945. }
  1946. KResult Process::do_kill(Process& process, int signal)
  1947. {
  1948. // FIXME: Allow sending SIGCONT to everyone in the process group.
  1949. // FIXME: Should setuid processes have some special treatment here?
  1950. if (!is_superuser() && m_euid != process.m_uid && m_uid != process.m_uid)
  1951. return KResult(-EPERM);
  1952. if (process.is_ring0() && signal == SIGKILL) {
  1953. klog() << "attempted to send SIGKILL to ring 0 process " << process.name().characters() << "(" << process.pid() << ")";
  1954. return KResult(-EPERM);
  1955. }
  1956. if (signal != 0)
  1957. return process.send_signal(signal, this);
  1958. return KSuccess;
  1959. }
  1960. KResult Process::do_killpg(pid_t pgrp, int signal)
  1961. {
  1962. InterruptDisabler disabler;
  1963. ASSERT(pgrp >= 0);
  1964. // Send the signal to all processes in the given group.
  1965. if (pgrp == 0) {
  1966. // Send the signal to our own pgrp.
  1967. pgrp = pgid();
  1968. }
  1969. bool group_was_empty = true;
  1970. bool any_succeeded = false;
  1971. KResult error = KSuccess;
  1972. Process::for_each_in_pgrp(pgrp, [&](auto& process) {
  1973. group_was_empty = false;
  1974. KResult res = do_kill(process, signal);
  1975. if (res.is_success())
  1976. any_succeeded = true;
  1977. else
  1978. error = res;
  1979. return IterationDecision::Continue;
  1980. });
  1981. if (group_was_empty)
  1982. return KResult(-ESRCH);
  1983. if (any_succeeded)
  1984. return KSuccess;
  1985. return error;
  1986. }
  1987. KResult Process::do_killall(int signal)
  1988. {
  1989. InterruptDisabler disabler;
  1990. bool any_succeeded = false;
  1991. KResult error = KSuccess;
  1992. // Send the signal to all processes we have access to for.
  1993. for (auto& process : *g_processes) {
  1994. KResult res = KSuccess;
  1995. if (process.pid() == m_pid)
  1996. res = do_killself(signal);
  1997. else
  1998. res = do_kill(process, signal);
  1999. if (res.is_success())
  2000. any_succeeded = true;
  2001. else
  2002. error = res;
  2003. }
  2004. if (any_succeeded)
  2005. return KSuccess;
  2006. return error;
  2007. }
  2008. KResult Process::do_killself(int signal)
  2009. {
  2010. if (signal == 0)
  2011. return KSuccess;
  2012. if (!Thread::current->should_ignore_signal(signal)) {
  2013. Thread::current->send_signal(signal, this);
  2014. (void)Thread::current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
  2015. }
  2016. return KSuccess;
  2017. }
  2018. int Process::sys$kill(pid_t pid, int signal)
  2019. {
  2020. if (pid == m_pid)
  2021. REQUIRE_PROMISE(stdio);
  2022. else
  2023. REQUIRE_PROMISE(proc);
  2024. if (signal < 0 || signal >= 32)
  2025. return -EINVAL;
  2026. if (pid < -1) {
  2027. if (pid == INT32_MIN)
  2028. return -EINVAL;
  2029. return do_killpg(-pid, signal);
  2030. }
  2031. if (pid == -1)
  2032. return do_killall(signal);
  2033. if (pid == m_pid) {
  2034. return do_killself(signal);
  2035. }
  2036. InterruptDisabler disabler;
  2037. auto* peer = Process::from_pid(pid);
  2038. if (!peer)
  2039. return -ESRCH;
  2040. return do_kill(*peer, signal);
  2041. }
  2042. int Process::sys$usleep(useconds_t usec)
  2043. {
  2044. REQUIRE_PROMISE(stdio);
  2045. if (!usec)
  2046. return 0;
  2047. u64 wakeup_time = Thread::current->sleep(usec / 1000);
  2048. if (wakeup_time > g_uptime)
  2049. return -EINTR;
  2050. return 0;
  2051. }
  2052. int Process::sys$sleep(unsigned seconds)
  2053. {
  2054. REQUIRE_PROMISE(stdio);
  2055. if (!seconds)
  2056. return 0;
  2057. u64 wakeup_time = Thread::current->sleep(seconds * TimeManagement::the().ticks_per_second());
  2058. if (wakeup_time > g_uptime) {
  2059. u32 ticks_left_until_original_wakeup_time = wakeup_time - g_uptime;
  2060. return ticks_left_until_original_wakeup_time / TimeManagement::the().ticks_per_second();
  2061. }
  2062. return 0;
  2063. }
  2064. timeval kgettimeofday()
  2065. {
  2066. return g_timeofday;
  2067. }
  2068. void compute_relative_timeout_from_absolute(const timeval& absolute_time, timeval& relative_time)
  2069. {
  2070. // Convert absolute time to relative time of day.
  2071. timeval_sub(absolute_time, kgettimeofday(), relative_time);
  2072. }
  2073. void compute_relative_timeout_from_absolute(const timespec& absolute_time, timeval& relative_time)
  2074. {
  2075. timeval tv_absolute_time;
  2076. timespec_to_timeval(absolute_time, tv_absolute_time);
  2077. compute_relative_timeout_from_absolute(tv_absolute_time, relative_time);
  2078. }
  2079. void kgettimeofday(timeval& tv)
  2080. {
  2081. tv = kgettimeofday();
  2082. }
  2083. int Process::sys$gettimeofday(timeval* user_tv)
  2084. {
  2085. REQUIRE_PROMISE(stdio);
  2086. if (!validate_write_typed(user_tv))
  2087. return -EFAULT;
  2088. auto tv = kgettimeofday();
  2089. copy_to_user(user_tv, &tv);
  2090. return 0;
  2091. }
  2092. uid_t Process::sys$getuid()
  2093. {
  2094. REQUIRE_PROMISE(stdio);
  2095. return m_uid;
  2096. }
  2097. gid_t Process::sys$getgid()
  2098. {
  2099. REQUIRE_PROMISE(stdio);
  2100. return m_gid;
  2101. }
  2102. uid_t Process::sys$geteuid()
  2103. {
  2104. REQUIRE_PROMISE(stdio);
  2105. return m_euid;
  2106. }
  2107. gid_t Process::sys$getegid()
  2108. {
  2109. REQUIRE_PROMISE(stdio);
  2110. return m_egid;
  2111. }
  2112. pid_t Process::sys$getpid()
  2113. {
  2114. REQUIRE_PROMISE(stdio);
  2115. return m_pid;
  2116. }
  2117. pid_t Process::sys$getppid()
  2118. {
  2119. REQUIRE_PROMISE(stdio);
  2120. return m_ppid;
  2121. }
  2122. int Process::sys$getresuid(uid_t* ruid, uid_t* euid, uid_t* suid)
  2123. {
  2124. REQUIRE_PROMISE(stdio);
  2125. if (!validate_write_typed(ruid) || !validate_write_typed(euid) || !validate_write_typed(suid))
  2126. return -EFAULT;
  2127. copy_to_user(ruid, &m_uid);
  2128. copy_to_user(euid, &m_euid);
  2129. copy_to_user(suid, &m_suid);
  2130. return 0;
  2131. }
  2132. int Process::sys$getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid)
  2133. {
  2134. REQUIRE_PROMISE(stdio);
  2135. if (!validate_write_typed(rgid) || !validate_write_typed(egid) || !validate_write_typed(sgid))
  2136. return -EFAULT;
  2137. copy_to_user(rgid, &m_gid);
  2138. copy_to_user(egid, &m_egid);
  2139. copy_to_user(sgid, &m_sgid);
  2140. return 0;
  2141. }
  2142. mode_t Process::sys$umask(mode_t mask)
  2143. {
  2144. REQUIRE_PROMISE(stdio);
  2145. auto old_mask = m_umask;
  2146. m_umask = mask & 0777;
  2147. return old_mask;
  2148. }
  2149. siginfo_t Process::reap(Process& process)
  2150. {
  2151. siginfo_t siginfo;
  2152. memset(&siginfo, 0, sizeof(siginfo));
  2153. siginfo.si_signo = SIGCHLD;
  2154. siginfo.si_pid = process.pid();
  2155. siginfo.si_uid = process.uid();
  2156. if (process.m_termination_signal) {
  2157. siginfo.si_status = process.m_termination_signal;
  2158. siginfo.si_code = CLD_KILLED;
  2159. } else {
  2160. siginfo.si_status = process.m_termination_status;
  2161. siginfo.si_code = CLD_EXITED;
  2162. }
  2163. {
  2164. InterruptDisabler disabler;
  2165. if (process.ppid()) {
  2166. auto* parent = Process::from_pid(process.ppid());
  2167. if (parent) {
  2168. parent->m_ticks_in_user_for_dead_children += process.m_ticks_in_user + process.m_ticks_in_user_for_dead_children;
  2169. parent->m_ticks_in_kernel_for_dead_children += process.m_ticks_in_kernel + process.m_ticks_in_kernel_for_dead_children;
  2170. }
  2171. }
  2172. #ifdef PROCESS_DEBUG
  2173. dbg() << "Reaping process " << process;
  2174. #endif
  2175. ASSERT(process.is_dead());
  2176. g_processes->remove(&process);
  2177. }
  2178. delete &process;
  2179. return siginfo;
  2180. }
  2181. KResultOr<siginfo_t> Process::do_waitid(idtype_t idtype, int id, int options)
  2182. {
  2183. if (idtype == P_PID) {
  2184. InterruptDisabler disabler;
  2185. if (idtype == P_PID && !Process::from_pid(id))
  2186. return KResult(-ECHILD);
  2187. }
  2188. pid_t waitee_pid;
  2189. // FIXME: WaitBlocker should support idtype/id specs directly.
  2190. if (idtype == P_ALL) {
  2191. waitee_pid = -1;
  2192. } else if (idtype == P_PID) {
  2193. waitee_pid = id;
  2194. } else {
  2195. // FIXME: Implement other PID specs.
  2196. return KResult(-EINVAL);
  2197. }
  2198. if (Thread::current->block<Thread::WaitBlocker>(options, waitee_pid) != Thread::BlockResult::WokeNormally)
  2199. return KResult(-EINTR);
  2200. InterruptDisabler disabler;
  2201. // NOTE: If waitee was -1, m_waitee_pid will have been filled in by the scheduler.
  2202. Process* waitee_process = Process::from_pid(waitee_pid);
  2203. if (!waitee_process)
  2204. return KResult(-ECHILD);
  2205. ASSERT(waitee_process);
  2206. if (waitee_process->is_dead()) {
  2207. return reap(*waitee_process);
  2208. } else {
  2209. auto* waitee_thread = Thread::from_tid(waitee_pid);
  2210. if (!waitee_thread)
  2211. return KResult(-ECHILD);
  2212. ASSERT((options & WNOHANG) || waitee_thread->state() == Thread::State::Stopped);
  2213. siginfo_t siginfo;
  2214. memset(&siginfo, 0, sizeof(siginfo));
  2215. siginfo.si_signo = SIGCHLD;
  2216. siginfo.si_pid = waitee_process->pid();
  2217. siginfo.si_uid = waitee_process->uid();
  2218. switch (waitee_thread->state()) {
  2219. case Thread::State::Stopped:
  2220. siginfo.si_code = CLD_STOPPED;
  2221. break;
  2222. case Thread::State::Running:
  2223. case Thread::State::Runnable:
  2224. case Thread::State::Blocked:
  2225. case Thread::State::Dying:
  2226. case Thread::State::Queued:
  2227. siginfo.si_code = CLD_CONTINUED;
  2228. break;
  2229. default:
  2230. ASSERT_NOT_REACHED();
  2231. break;
  2232. }
  2233. siginfo.si_status = waitee_thread->m_stop_signal;
  2234. return siginfo;
  2235. }
  2236. }
  2237. pid_t Process::sys$waitid(const Syscall::SC_waitid_params* user_params)
  2238. {
  2239. REQUIRE_PROMISE(proc);
  2240. Syscall::SC_waitid_params params;
  2241. if (!validate_read_and_copy_typed(&params, user_params))
  2242. return -EFAULT;
  2243. if (!validate_write_typed(params.infop))
  2244. return -EFAULT;
  2245. #ifdef PROCESS_DEBUG
  2246. dbg() << "sys$waitid(" << params.idtype << ", " << params.id << ", " << params.infop << ", " << params.options << ")";
  2247. #endif
  2248. auto siginfo_or_error = do_waitid(static_cast<idtype_t>(params.idtype), params.id, params.options);
  2249. if (siginfo_or_error.is_error())
  2250. return siginfo_or_error.error();
  2251. // While we waited, the process lock was dropped. This gave other threads
  2252. // the opportunity to mess with the memory. For example, it could free the
  2253. // region, and map it to a region to which it has no write permissions.
  2254. // Therefore, we need to re-validate the pointer.
  2255. if (!validate_write_typed(params.infop))
  2256. return -EFAULT;
  2257. copy_to_user(params.infop, &siginfo_or_error.value());
  2258. return 0;
  2259. }
  2260. bool Process::validate_read_from_kernel(VirtualAddress vaddr, size_t size) const
  2261. {
  2262. if (vaddr.is_null())
  2263. return false;
  2264. return MM.validate_kernel_read(*this, vaddr, size);
  2265. }
  2266. bool Process::validate_read(const void* address, size_t size) const
  2267. {
  2268. if (!size)
  2269. return false;
  2270. return MM.validate_user_read(*this, VirtualAddress(address), size);
  2271. }
  2272. bool Process::validate_write(void* address, size_t size) const
  2273. {
  2274. if (!size)
  2275. return false;
  2276. return MM.validate_user_write(*this, VirtualAddress(address), size);
  2277. }
  2278. pid_t Process::sys$getsid(pid_t pid)
  2279. {
  2280. REQUIRE_PROMISE(proc);
  2281. if (pid == 0)
  2282. return m_sid;
  2283. InterruptDisabler disabler;
  2284. auto* process = Process::from_pid(pid);
  2285. if (!process)
  2286. return -ESRCH;
  2287. if (m_sid != process->m_sid)
  2288. return -EPERM;
  2289. return process->m_sid;
  2290. }
  2291. pid_t Process::sys$setsid()
  2292. {
  2293. REQUIRE_PROMISE(proc);
  2294. InterruptDisabler disabler;
  2295. bool found_process_with_same_pgid_as_my_pid = false;
  2296. Process::for_each_in_pgrp(pid(), [&](auto&) {
  2297. found_process_with_same_pgid_as_my_pid = true;
  2298. return IterationDecision::Break;
  2299. });
  2300. if (found_process_with_same_pgid_as_my_pid)
  2301. return -EPERM;
  2302. m_sid = m_pid;
  2303. m_pgid = m_pid;
  2304. m_tty = nullptr;
  2305. return m_sid;
  2306. }
  2307. pid_t Process::sys$getpgid(pid_t pid)
  2308. {
  2309. REQUIRE_PROMISE(proc);
  2310. if (pid == 0)
  2311. return m_pgid;
  2312. InterruptDisabler disabler; // FIXME: Use a ProcessHandle
  2313. auto* process = Process::from_pid(pid);
  2314. if (!process)
  2315. return -ESRCH;
  2316. return process->m_pgid;
  2317. }
  2318. pid_t Process::sys$getpgrp()
  2319. {
  2320. REQUIRE_PROMISE(stdio);
  2321. return m_pgid;
  2322. }
  2323. static pid_t get_sid_from_pgid(pid_t pgid)
  2324. {
  2325. InterruptDisabler disabler;
  2326. auto* group_leader = Process::from_pid(pgid);
  2327. if (!group_leader)
  2328. return -1;
  2329. return group_leader->sid();
  2330. }
  2331. int Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
  2332. {
  2333. REQUIRE_PROMISE(proc);
  2334. InterruptDisabler disabler; // FIXME: Use a ProcessHandle
  2335. pid_t pid = specified_pid ? specified_pid : m_pid;
  2336. if (specified_pgid < 0) {
  2337. // The value of the pgid argument is less than 0, or is not a value supported by the implementation.
  2338. return -EINVAL;
  2339. }
  2340. auto* process = Process::from_pid(pid);
  2341. if (!process)
  2342. return -ESRCH;
  2343. if (process != this && process->ppid() != m_pid) {
  2344. // The value of the pid argument does not match the process ID
  2345. // of the calling process or of a child process of the calling process.
  2346. return -ESRCH;
  2347. }
  2348. if (process->pid() == process->sid()) {
  2349. // The process indicated by the pid argument is a session leader.
  2350. return -EPERM;
  2351. }
  2352. if (process->ppid() == m_pid && process->sid() != sid()) {
  2353. // The value of the pid argument matches the process ID of a child
  2354. // process of the calling process and the child process is not in
  2355. // the same session as the calling process.
  2356. return -EPERM;
  2357. }
  2358. pid_t new_pgid = specified_pgid ? specified_pgid : process->m_pid;
  2359. pid_t current_sid = get_sid_from_pgid(process->m_pgid);
  2360. pid_t new_sid = get_sid_from_pgid(new_pgid);
  2361. if (current_sid != new_sid) {
  2362. // Can't move a process between sessions.
  2363. return -EPERM;
  2364. }
  2365. // FIXME: There are more EPERM conditions to check for here..
  2366. process->m_pgid = new_pgid;
  2367. return 0;
  2368. }
  2369. int Process::sys$ioctl(int fd, unsigned request, FlatPtr arg)
  2370. {
  2371. auto description = file_description(fd);
  2372. if (!description)
  2373. return -EBADF;
  2374. SmapDisabler disabler;
  2375. return description->file().ioctl(*description, request, arg);
  2376. }
  2377. int Process::sys$dup(int old_fd)
  2378. {
  2379. REQUIRE_PROMISE(stdio);
  2380. auto description = file_description(old_fd);
  2381. if (!description)
  2382. return -EBADF;
  2383. int new_fd = alloc_fd();
  2384. if (new_fd < 0)
  2385. return new_fd;
  2386. m_fds[new_fd].set(*description);
  2387. return new_fd;
  2388. }
  2389. int Process::sys$dup2(int old_fd, int new_fd)
  2390. {
  2391. REQUIRE_PROMISE(stdio);
  2392. auto description = file_description(old_fd);
  2393. if (!description)
  2394. return -EBADF;
  2395. if (new_fd < 0 || new_fd >= m_max_open_file_descriptors)
  2396. return -EINVAL;
  2397. m_fds[new_fd].set(*description);
  2398. return new_fd;
  2399. }
  2400. int Process::sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set)
  2401. {
  2402. REQUIRE_PROMISE(sigaction);
  2403. if (old_set) {
  2404. if (!validate_write_typed(old_set))
  2405. return -EFAULT;
  2406. copy_to_user(old_set, &Thread::current->m_signal_mask);
  2407. }
  2408. if (set) {
  2409. if (!validate_read_typed(set))
  2410. return -EFAULT;
  2411. sigset_t set_value;
  2412. copy_from_user(&set_value, set);
  2413. switch (how) {
  2414. case SIG_BLOCK:
  2415. Thread::current->m_signal_mask &= ~set_value;
  2416. break;
  2417. case SIG_UNBLOCK:
  2418. Thread::current->m_signal_mask |= set_value;
  2419. break;
  2420. case SIG_SETMASK:
  2421. Thread::current->m_signal_mask = set_value;
  2422. break;
  2423. default:
  2424. return -EINVAL;
  2425. }
  2426. }
  2427. return 0;
  2428. }
  2429. int Process::sys$sigpending(sigset_t* set)
  2430. {
  2431. REQUIRE_PROMISE(stdio);
  2432. if (!validate_write_typed(set))
  2433. return -EFAULT;
  2434. copy_to_user(set, &Thread::current->m_pending_signals);
  2435. return 0;
  2436. }
  2437. int Process::sys$sigaction(int signum, const sigaction* act, sigaction* old_act)
  2438. {
  2439. REQUIRE_PROMISE(sigaction);
  2440. if (signum < 1 || signum >= 32 || signum == SIGKILL || signum == SIGSTOP)
  2441. return -EINVAL;
  2442. if (!validate_read_typed(act))
  2443. return -EFAULT;
  2444. InterruptDisabler disabler; // FIXME: This should use a narrower lock. Maybe a way to ignore signals temporarily?
  2445. auto& action = Thread::current->m_signal_action_data[signum];
  2446. if (old_act) {
  2447. if (!validate_write_typed(old_act))
  2448. return -EFAULT;
  2449. copy_to_user(&old_act->sa_flags, &action.flags);
  2450. copy_to_user(&old_act->sa_sigaction, &action.handler_or_sigaction, sizeof(action.handler_or_sigaction));
  2451. }
  2452. copy_from_user(&action.flags, &act->sa_flags);
  2453. copy_from_user(&action.handler_or_sigaction, &act->sa_sigaction, sizeof(action.handler_or_sigaction));
  2454. return 0;
  2455. }
  2456. int Process::sys$getgroups(ssize_t count, gid_t* user_gids)
  2457. {
  2458. REQUIRE_PROMISE(stdio);
  2459. if (count < 0)
  2460. return -EINVAL;
  2461. if (!count)
  2462. return m_extra_gids.size();
  2463. if (count != (int)m_extra_gids.size())
  2464. return -EINVAL;
  2465. if (!validate_write_typed(user_gids, m_extra_gids.size()))
  2466. return -EFAULT;
  2467. Vector<gid_t> gids;
  2468. for (auto gid : m_extra_gids)
  2469. gids.append(gid);
  2470. copy_to_user(user_gids, gids.data(), sizeof(gid_t) * count);
  2471. return 0;
  2472. }
  2473. int Process::sys$setgroups(ssize_t count, const gid_t* user_gids)
  2474. {
  2475. REQUIRE_PROMISE(id);
  2476. if (count < 0)
  2477. return -EINVAL;
  2478. if (!is_superuser())
  2479. return -EPERM;
  2480. if (count && !validate_read(user_gids, count))
  2481. return -EFAULT;
  2482. if (!count) {
  2483. m_extra_gids.clear();
  2484. return 0;
  2485. }
  2486. Vector<gid_t> gids;
  2487. gids.resize(count);
  2488. copy_from_user(gids.data(), user_gids, sizeof(gid_t) * count);
  2489. HashTable<gid_t> unique_extra_gids;
  2490. for (auto& gid : gids) {
  2491. if (gid != m_gid)
  2492. unique_extra_gids.set(gid);
  2493. }
  2494. m_extra_gids.resize(unique_extra_gids.size());
  2495. size_t i = 0;
  2496. for (auto& gid : unique_extra_gids) {
  2497. if (gid == m_gid)
  2498. continue;
  2499. m_extra_gids[i++] = gid;
  2500. }
  2501. return 0;
  2502. }
  2503. int Process::sys$mkdir(const char* user_path, size_t path_length, mode_t mode)
  2504. {
  2505. REQUIRE_PROMISE(cpath);
  2506. auto path = get_syscall_path_argument(user_path, path_length);
  2507. if (path.is_error())
  2508. return path.error();
  2509. return VFS::the().mkdir(path.value(), mode & ~umask(), current_directory());
  2510. }
  2511. int Process::sys$realpath(const Syscall::SC_realpath_params* user_params)
  2512. {
  2513. REQUIRE_PROMISE(rpath);
  2514. Syscall::SC_realpath_params params;
  2515. if (!validate_read_and_copy_typed(&params, user_params))
  2516. return -EFAULT;
  2517. if (!validate_write(params.buffer.data, params.buffer.size))
  2518. return -EFAULT;
  2519. auto path = get_syscall_path_argument(params.path);
  2520. if (path.is_error())
  2521. return path.error();
  2522. auto custody_or_error = VFS::the().resolve_path(path.value(), current_directory());
  2523. if (custody_or_error.is_error())
  2524. return custody_or_error.error();
  2525. auto& custody = custody_or_error.value();
  2526. auto absolute_path = custody->absolute_path();
  2527. if (absolute_path.length() + 1 > params.buffer.size)
  2528. return -ENAMETOOLONG;
  2529. copy_to_user(params.buffer.data, absolute_path.characters(), absolute_path.length() + 1);
  2530. return 0;
  2531. };
  2532. clock_t Process::sys$times(tms* times)
  2533. {
  2534. REQUIRE_PROMISE(stdio);
  2535. if (!validate_write_typed(times))
  2536. return -EFAULT;
  2537. copy_to_user(&times->tms_utime, &m_ticks_in_user);
  2538. copy_to_user(&times->tms_stime, &m_ticks_in_kernel);
  2539. copy_to_user(&times->tms_cutime, &m_ticks_in_user_for_dead_children);
  2540. copy_to_user(&times->tms_cstime, &m_ticks_in_kernel_for_dead_children);
  2541. return g_uptime & 0x7fffffff;
  2542. }
  2543. int Process::sys$select(const Syscall::SC_select_params* params)
  2544. {
  2545. REQUIRE_PROMISE(stdio);
  2546. // FIXME: Return -EINVAL if timeout is invalid.
  2547. if (!validate_read_typed(params))
  2548. return -EFAULT;
  2549. SmapDisabler disabler;
  2550. int nfds = params->nfds;
  2551. fd_set* readfds = params->readfds;
  2552. fd_set* writefds = params->writefds;
  2553. fd_set* exceptfds = params->exceptfds;
  2554. const timespec* timeout = params->timeout;
  2555. const sigset_t* sigmask = params->sigmask;
  2556. if (writefds && !validate_write_typed(writefds))
  2557. return -EFAULT;
  2558. if (readfds && !validate_write_typed(readfds))
  2559. return -EFAULT;
  2560. if (exceptfds && !validate_write_typed(exceptfds))
  2561. return -EFAULT;
  2562. if (timeout && !validate_read_typed(timeout))
  2563. return -EFAULT;
  2564. if (sigmask && !validate_read_typed(sigmask))
  2565. return -EFAULT;
  2566. if (nfds < 0)
  2567. return -EINVAL;
  2568. timespec computed_timeout;
  2569. bool select_has_timeout = false;
  2570. if (timeout && (timeout->tv_sec || timeout->tv_nsec)) {
  2571. timespec ts_since_boot;
  2572. timeval_to_timespec(Scheduler::time_since_boot(), ts_since_boot);
  2573. timespec_add(ts_since_boot, *timeout, computed_timeout);
  2574. select_has_timeout = true;
  2575. }
  2576. ScopedValueRollback scoped_sigmask(Thread::current->m_signal_mask);
  2577. if (sigmask)
  2578. Thread::current->m_signal_mask = *sigmask;
  2579. Thread::SelectBlocker::FDVector rfds;
  2580. Thread::SelectBlocker::FDVector wfds;
  2581. Thread::SelectBlocker::FDVector efds;
  2582. auto transfer_fds = [&](auto* fds, auto& vector) -> int {
  2583. vector.clear_with_capacity();
  2584. if (!fds)
  2585. return 0;
  2586. for (int fd = 0; fd < nfds; ++fd) {
  2587. if (FD_ISSET(fd, fds)) {
  2588. if (!file_description(fd)) {
  2589. dbg() << "sys$select: Bad fd number " << fd;
  2590. return -EBADF;
  2591. }
  2592. vector.append(fd);
  2593. }
  2594. }
  2595. return 0;
  2596. };
  2597. if (int error = transfer_fds(writefds, wfds))
  2598. return error;
  2599. if (int error = transfer_fds(readfds, rfds))
  2600. return error;
  2601. if (int error = transfer_fds(exceptfds, efds))
  2602. return error;
  2603. #if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
  2604. dbg() << "selecting on (read:" << rfds.size() << ", write:" << wfds.size() << "), timeout=" << timeout;
  2605. #endif
  2606. if (!timeout || select_has_timeout) {
  2607. if (Thread::current->block<Thread::SelectBlocker>(computed_timeout, select_has_timeout, rfds, wfds, efds) != Thread::BlockResult::WokeNormally)
  2608. return -EINTR;
  2609. // While we blocked, the process lock was dropped. This gave other threads
  2610. // the opportunity to mess with the memory. For example, it could free the
  2611. // region, and map it to a region to which it has no write permissions.
  2612. // Therefore, we need to re-validate all pointers.
  2613. if (writefds && !validate_write_typed(writefds))
  2614. return -EFAULT;
  2615. if (readfds && !validate_write_typed(readfds))
  2616. return -EFAULT;
  2617. // See the fixme below.
  2618. if (exceptfds && !validate_write_typed(exceptfds))
  2619. return -EFAULT;
  2620. }
  2621. int marked_fd_count = 0;
  2622. auto mark_fds = [&](auto* fds, auto& vector, auto should_mark) {
  2623. if (!fds)
  2624. return;
  2625. FD_ZERO(fds);
  2626. for (int fd : vector) {
  2627. if (auto description = file_description(fd); description && should_mark(*description)) {
  2628. FD_SET(fd, fds);
  2629. ++marked_fd_count;
  2630. }
  2631. }
  2632. };
  2633. mark_fds(readfds, rfds, [](auto& description) { return description.can_read(); });
  2634. mark_fds(writefds, wfds, [](auto& description) { return description.can_write(); });
  2635. // FIXME: We should also mark exceptfds as appropriate.
  2636. return marked_fd_count;
  2637. }
  2638. int Process::sys$poll(const Syscall::SC_poll_params* params)
  2639. {
  2640. REQUIRE_PROMISE(stdio);
  2641. // FIXME: Return -EINVAL if timeout is invalid.
  2642. if (!validate_read_typed(params))
  2643. return -EFAULT;
  2644. SmapDisabler disabler;
  2645. pollfd* fds = params->fds;
  2646. unsigned nfds = params->nfds;
  2647. const timespec* timeout = params->timeout;
  2648. const sigset_t* sigmask = params->sigmask;
  2649. if (fds && !validate_read_typed(fds, nfds))
  2650. return -EFAULT;
  2651. if (timeout && !validate_read_typed(timeout))
  2652. return -EFAULT;
  2653. if (sigmask && !validate_read_typed(sigmask))
  2654. return -EFAULT;
  2655. if (!validate_read_typed(fds))
  2656. return -EFAULT;
  2657. Thread::SelectBlocker::FDVector rfds;
  2658. Thread::SelectBlocker::FDVector wfds;
  2659. for (unsigned i = 0; i < nfds; ++i) {
  2660. if (fds[i].events & POLLIN)
  2661. rfds.append(fds[i].fd);
  2662. if (fds[i].events & POLLOUT)
  2663. wfds.append(fds[i].fd);
  2664. }
  2665. timespec actual_timeout;
  2666. bool has_timeout = false;
  2667. if (timeout && (timeout->tv_sec || timeout->tv_nsec)) {
  2668. timespec ts_since_boot;
  2669. timeval_to_timespec(Scheduler::time_since_boot(), ts_since_boot);
  2670. timespec_add(ts_since_boot, *timeout, actual_timeout);
  2671. has_timeout = true;
  2672. }
  2673. ScopedValueRollback scoped_sigmask(Thread::current->m_signal_mask);
  2674. if (sigmask)
  2675. Thread::current->m_signal_mask = *sigmask;
  2676. #if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
  2677. dbg() << "polling on (read:" << rfds.size() << ", write:" << wfds.size() << "), timeout=" << timeout;
  2678. #endif
  2679. if (!timeout || has_timeout) {
  2680. if (Thread::current->block<Thread::SelectBlocker>(actual_timeout, has_timeout, rfds, wfds, Thread::SelectBlocker::FDVector()) != Thread::BlockResult::WokeNormally)
  2681. return -EINTR;
  2682. }
  2683. int fds_with_revents = 0;
  2684. for (unsigned i = 0; i < nfds; ++i) {
  2685. auto description = file_description(fds[i].fd);
  2686. if (!description) {
  2687. fds[i].revents = POLLNVAL;
  2688. continue;
  2689. }
  2690. fds[i].revents = 0;
  2691. if (fds[i].events & POLLIN && description->can_read())
  2692. fds[i].revents |= POLLIN;
  2693. if (fds[i].events & POLLOUT && description->can_write())
  2694. fds[i].revents |= POLLOUT;
  2695. if (fds[i].revents)
  2696. ++fds_with_revents;
  2697. }
  2698. return fds_with_revents;
  2699. }
  2700. Custody& Process::current_directory()
  2701. {
  2702. if (!m_cwd)
  2703. m_cwd = VFS::the().root_custody();
  2704. return *m_cwd;
  2705. }
  2706. int Process::sys$link(const Syscall::SC_link_params* user_params)
  2707. {
  2708. REQUIRE_PROMISE(cpath);
  2709. Syscall::SC_link_params params;
  2710. if (!validate_read_and_copy_typed(&params, user_params))
  2711. return -EFAULT;
  2712. auto old_path = validate_and_copy_string_from_user(params.old_path);
  2713. auto new_path = validate_and_copy_string_from_user(params.new_path);
  2714. if (old_path.is_null() || new_path.is_null())
  2715. return -EFAULT;
  2716. return VFS::the().link(old_path, new_path, current_directory());
  2717. }
  2718. int Process::sys$unlink(const char* user_path, size_t path_length)
  2719. {
  2720. REQUIRE_PROMISE(cpath);
  2721. if (!validate_read(user_path, path_length))
  2722. return -EFAULT;
  2723. auto path = get_syscall_path_argument(user_path, path_length);
  2724. if (path.is_error())
  2725. return path.error();
  2726. return VFS::the().unlink(path.value(), current_directory());
  2727. }
  2728. int Process::sys$symlink(const Syscall::SC_symlink_params* user_params)
  2729. {
  2730. REQUIRE_PROMISE(cpath);
  2731. Syscall::SC_symlink_params params;
  2732. if (!validate_read_and_copy_typed(&params, user_params))
  2733. return -EFAULT;
  2734. auto target = get_syscall_path_argument(params.target);
  2735. if (target.is_error())
  2736. return target.error();
  2737. auto linkpath = get_syscall_path_argument(params.linkpath);
  2738. if (linkpath.is_error())
  2739. return linkpath.error();
  2740. return VFS::the().symlink(target.value(), linkpath.value(), current_directory());
  2741. }
  2742. KResultOr<String> Process::get_syscall_path_argument(const char* user_path, size_t path_length) const
  2743. {
  2744. if (path_length == 0)
  2745. return KResult(-EINVAL);
  2746. if (path_length > PATH_MAX)
  2747. return KResult(-ENAMETOOLONG);
  2748. if (!validate_read(user_path, path_length))
  2749. return KResult(-EFAULT);
  2750. return copy_string_from_user(user_path, path_length);
  2751. }
  2752. KResultOr<String> Process::get_syscall_path_argument(const Syscall::StringArgument& path) const
  2753. {
  2754. return get_syscall_path_argument(path.characters, path.length);
  2755. }
  2756. int Process::sys$rmdir(const char* user_path, size_t path_length)
  2757. {
  2758. REQUIRE_PROMISE(cpath);
  2759. auto path = get_syscall_path_argument(user_path, path_length);
  2760. if (path.is_error())
  2761. return path.error();
  2762. return VFS::the().rmdir(path.value(), current_directory());
  2763. }
  2764. int Process::sys$chmod(const char* user_path, size_t path_length, mode_t mode)
  2765. {
  2766. REQUIRE_PROMISE(fattr);
  2767. auto path = get_syscall_path_argument(user_path, path_length);
  2768. if (path.is_error())
  2769. return path.error();
  2770. return VFS::the().chmod(path.value(), mode, current_directory());
  2771. }
  2772. int Process::sys$fchmod(int fd, mode_t mode)
  2773. {
  2774. REQUIRE_PROMISE(fattr);
  2775. auto description = file_description(fd);
  2776. if (!description)
  2777. return -EBADF;
  2778. return description->chmod(mode);
  2779. }
  2780. int Process::sys$fchown(int fd, uid_t uid, gid_t gid)
  2781. {
  2782. REQUIRE_PROMISE(chown);
  2783. auto description = file_description(fd);
  2784. if (!description)
  2785. return -EBADF;
  2786. return description->chown(uid, gid);
  2787. }
  2788. int Process::sys$chown(const Syscall::SC_chown_params* user_params)
  2789. {
  2790. REQUIRE_PROMISE(chown);
  2791. Syscall::SC_chown_params params;
  2792. if (!validate_read_and_copy_typed(&params, user_params))
  2793. return -EFAULT;
  2794. auto path = get_syscall_path_argument(params.path);
  2795. if (path.is_error())
  2796. return path.error();
  2797. return VFS::the().chown(path.value(), params.uid, params.gid, current_directory());
  2798. }
  2799. void Process::finalize()
  2800. {
  2801. ASSERT(Thread::current == g_finalizer);
  2802. #ifdef PROCESS_DEBUG
  2803. dbg() << "Finalizing process " << *this;
  2804. #endif
  2805. if (m_perf_event_buffer) {
  2806. auto description_or_error = VFS::the().open(String::format("perfcore.%d", m_pid), O_CREAT | O_EXCL, 0400, current_directory(), UidAndGid { m_uid, m_gid });
  2807. if (!description_or_error.is_error()) {
  2808. auto& description = description_or_error.value();
  2809. auto json = m_perf_event_buffer->to_json(m_pid, m_executable ? m_executable->absolute_path() : "");
  2810. description->write(json.data(), json.size());
  2811. }
  2812. }
  2813. m_fds.clear();
  2814. m_tty = nullptr;
  2815. m_executable = nullptr;
  2816. m_cwd = nullptr;
  2817. m_root_directory = nullptr;
  2818. m_root_directory_relative_to_global_root = nullptr;
  2819. disown_all_shared_buffers();
  2820. {
  2821. InterruptDisabler disabler;
  2822. if (auto* parent_thread = Thread::from_tid(m_ppid)) {
  2823. if (parent_thread->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT) {
  2824. // NOTE: If the parent doesn't care about this process, let it go.
  2825. m_ppid = 0;
  2826. } else {
  2827. parent_thread->send_signal(SIGCHLD, this);
  2828. }
  2829. }
  2830. }
  2831. m_regions.clear();
  2832. m_dead = true;
  2833. }
  2834. void Process::die()
  2835. {
  2836. // Let go of the TTY, otherwise a slave PTY may keep the master PTY from
  2837. // getting an EOF when the last process using the slave PTY dies.
  2838. // If the master PTY owner relies on an EOF to know when to wait() on a
  2839. // slave owner, we have to allow the PTY pair to be torn down.
  2840. m_tty = nullptr;
  2841. kill_all_threads();
  2842. }
  2843. size_t Process::amount_dirty_private() const
  2844. {
  2845. // FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
  2846. // The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
  2847. // That's probably a situation that needs to be looked at in general.
  2848. size_t amount = 0;
  2849. for (auto& region : m_regions) {
  2850. if (!region.is_shared())
  2851. amount += region.amount_dirty();
  2852. }
  2853. return amount;
  2854. }
  2855. size_t Process::amount_clean_inode() const
  2856. {
  2857. HashTable<const InodeVMObject*> vmobjects;
  2858. for (auto& region : m_regions) {
  2859. if (region.vmobject().is_inode())
  2860. vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
  2861. }
  2862. size_t amount = 0;
  2863. for (auto& vmobject : vmobjects)
  2864. amount += vmobject->amount_clean();
  2865. return amount;
  2866. }
  2867. size_t Process::amount_virtual() const
  2868. {
  2869. size_t amount = 0;
  2870. for (auto& region : m_regions) {
  2871. amount += region.size();
  2872. }
  2873. return amount;
  2874. }
  2875. size_t Process::amount_resident() const
  2876. {
  2877. // FIXME: This will double count if multiple regions use the same physical page.
  2878. size_t amount = 0;
  2879. for (auto& region : m_regions) {
  2880. amount += region.amount_resident();
  2881. }
  2882. return amount;
  2883. }
  2884. size_t Process::amount_shared() const
  2885. {
  2886. // FIXME: This will double count if multiple regions use the same physical page.
  2887. // FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
  2888. // and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
  2889. // so that every Region contributes +1 ref to each of its PhysicalPages.
  2890. size_t amount = 0;
  2891. for (auto& region : m_regions) {
  2892. amount += region.amount_shared();
  2893. }
  2894. return amount;
  2895. }
  2896. size_t Process::amount_purgeable_volatile() const
  2897. {
  2898. size_t amount = 0;
  2899. for (auto& region : m_regions) {
  2900. if (region.vmobject().is_purgeable() && static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  2901. amount += region.amount_resident();
  2902. }
  2903. return amount;
  2904. }
  2905. size_t Process::amount_purgeable_nonvolatile() const
  2906. {
  2907. size_t amount = 0;
  2908. for (auto& region : m_regions) {
  2909. if (region.vmobject().is_purgeable() && !static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  2910. amount += region.amount_resident();
  2911. }
  2912. return amount;
  2913. }
  2914. #define REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(domain) \
  2915. do { \
  2916. if (domain == AF_INET) \
  2917. REQUIRE_PROMISE(inet); \
  2918. else if (domain == AF_LOCAL) \
  2919. REQUIRE_PROMISE(unix); \
  2920. } while (0)
  2921. int Process::sys$socket(int domain, int type, int protocol)
  2922. {
  2923. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(domain);
  2924. if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser())
  2925. return -EACCES;
  2926. int fd = alloc_fd();
  2927. if (fd < 0)
  2928. return fd;
  2929. auto result = Socket::create(domain, type, protocol);
  2930. if (result.is_error())
  2931. return result.error();
  2932. auto description = FileDescription::create(*result.value());
  2933. description->set_readable(true);
  2934. description->set_writable(true);
  2935. unsigned flags = 0;
  2936. if (type & SOCK_CLOEXEC)
  2937. flags |= FD_CLOEXEC;
  2938. if (type & SOCK_NONBLOCK)
  2939. description->set_blocking(false);
  2940. m_fds[fd].set(move(description), flags);
  2941. return fd;
  2942. }
  2943. int Process::sys$bind(int sockfd, const sockaddr* address, socklen_t address_length)
  2944. {
  2945. if (!validate_read(address, address_length))
  2946. return -EFAULT;
  2947. auto description = file_description(sockfd);
  2948. if (!description)
  2949. return -EBADF;
  2950. if (!description->is_socket())
  2951. return -ENOTSOCK;
  2952. auto& socket = *description->socket();
  2953. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2954. return socket.bind(address, address_length);
  2955. }
  2956. int Process::sys$listen(int sockfd, int backlog)
  2957. {
  2958. if (backlog < 0)
  2959. return -EINVAL;
  2960. auto description = file_description(sockfd);
  2961. if (!description)
  2962. return -EBADF;
  2963. if (!description->is_socket())
  2964. return -ENOTSOCK;
  2965. auto& socket = *description->socket();
  2966. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2967. if (socket.is_connected())
  2968. return -EINVAL;
  2969. return socket.listen(backlog);
  2970. }
  2971. int Process::sys$accept(int accepting_socket_fd, sockaddr* user_address, socklen_t* user_address_size)
  2972. {
  2973. REQUIRE_PROMISE(accept);
  2974. socklen_t address_size = 0;
  2975. if (user_address) {
  2976. if (!validate_write_typed(user_address_size))
  2977. return -EFAULT;
  2978. copy_from_user(&address_size, user_address_size);
  2979. if (!validate_write(user_address, address_size))
  2980. return -EFAULT;
  2981. }
  2982. int accepted_socket_fd = alloc_fd();
  2983. if (accepted_socket_fd < 0)
  2984. return accepted_socket_fd;
  2985. auto accepting_socket_description = file_description(accepting_socket_fd);
  2986. if (!accepting_socket_description)
  2987. return -EBADF;
  2988. if (!accepting_socket_description->is_socket())
  2989. return -ENOTSOCK;
  2990. auto& socket = *accepting_socket_description->socket();
  2991. if (!socket.can_accept()) {
  2992. if (accepting_socket_description->is_blocking()) {
  2993. if (Thread::current->block<Thread::AcceptBlocker>(*accepting_socket_description) != Thread::BlockResult::WokeNormally)
  2994. return -EINTR;
  2995. } else {
  2996. return -EAGAIN;
  2997. }
  2998. }
  2999. auto accepted_socket = socket.accept();
  3000. ASSERT(accepted_socket);
  3001. if (user_address) {
  3002. u8 address_buffer[sizeof(sockaddr_un)];
  3003. address_size = min(sizeof(sockaddr_un), static_cast<size_t>(address_size));
  3004. accepted_socket->get_peer_address((sockaddr*)address_buffer, &address_size);
  3005. copy_to_user(user_address, address_buffer, address_size);
  3006. copy_to_user(user_address_size, &address_size);
  3007. }
  3008. auto accepted_socket_description = FileDescription::create(*accepted_socket);
  3009. accepted_socket_description->set_readable(true);
  3010. accepted_socket_description->set_writable(true);
  3011. // NOTE: The accepted socket inherits fd flags from the accepting socket.
  3012. // I'm not sure if this matches other systems but it makes sense to me.
  3013. accepted_socket_description->set_blocking(accepting_socket_description->is_blocking());
  3014. m_fds[accepted_socket_fd].set(move(accepted_socket_description), m_fds[accepting_socket_fd].flags);
  3015. // NOTE: Moving this state to Completed is what causes connect() to unblock on the client side.
  3016. accepted_socket->set_setup_state(Socket::SetupState::Completed);
  3017. return accepted_socket_fd;
  3018. }
  3019. int Process::sys$connect(int sockfd, const sockaddr* user_address, socklen_t user_address_size)
  3020. {
  3021. if (!validate_read(user_address, user_address_size))
  3022. return -EFAULT;
  3023. int fd = alloc_fd();
  3024. if (fd < 0)
  3025. return fd;
  3026. auto description = file_description(sockfd);
  3027. if (!description)
  3028. return -EBADF;
  3029. if (!description->is_socket())
  3030. return -ENOTSOCK;
  3031. auto& socket = *description->socket();
  3032. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  3033. u8 address[sizeof(sockaddr_un)];
  3034. size_t address_size = min(sizeof(address), static_cast<size_t>(user_address_size));
  3035. copy_from_user(address, user_address, address_size);
  3036. return socket.connect(*description, (const sockaddr*)address, address_size, description->is_blocking() ? ShouldBlock::Yes : ShouldBlock::No);
  3037. }
  3038. int Process::sys$shutdown(int sockfd, int how)
  3039. {
  3040. REQUIRE_PROMISE(stdio);
  3041. if (how & ~SHUT_RDWR)
  3042. return -EINVAL;
  3043. auto description = file_description(sockfd);
  3044. if (!description)
  3045. return -EBADF;
  3046. if (!description->is_socket())
  3047. return -ENOTSOCK;
  3048. auto& socket = *description->socket();
  3049. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  3050. return socket.shutdown(how);
  3051. }
  3052. ssize_t Process::sys$sendto(const Syscall::SC_sendto_params* user_params)
  3053. {
  3054. REQUIRE_PROMISE(stdio);
  3055. Syscall::SC_sendto_params params;
  3056. if (!validate_read_and_copy_typed(&params, user_params))
  3057. return -EFAULT;
  3058. int flags = params.flags;
  3059. const sockaddr* addr = params.addr;
  3060. socklen_t addr_length = params.addr_length;
  3061. if (!validate(params.data))
  3062. return -EFAULT;
  3063. if (addr && !validate_read(addr, addr_length))
  3064. return -EFAULT;
  3065. auto description = file_description(params.sockfd);
  3066. if (!description)
  3067. return -EBADF;
  3068. if (!description->is_socket())
  3069. return -ENOTSOCK;
  3070. auto& socket = *description->socket();
  3071. if (socket.is_shut_down_for_writing())
  3072. return -EPIPE;
  3073. SmapDisabler disabler;
  3074. return socket.sendto(*description, params.data.data, params.data.size, flags, addr, addr_length);
  3075. }
  3076. ssize_t Process::sys$recvfrom(const Syscall::SC_recvfrom_params* user_params)
  3077. {
  3078. REQUIRE_PROMISE(stdio);
  3079. Syscall::SC_recvfrom_params params;
  3080. if (!validate_read_and_copy_typed(&params, user_params))
  3081. return -EFAULT;
  3082. int flags = params.flags;
  3083. sockaddr* addr = params.addr;
  3084. socklen_t* addr_length = params.addr_length;
  3085. SmapDisabler disabler;
  3086. if (!validate(params.buffer))
  3087. return -EFAULT;
  3088. if (addr_length) {
  3089. if (!validate_write_typed(addr_length))
  3090. return -EFAULT;
  3091. if (!validate_write(addr, *addr_length))
  3092. return -EFAULT;
  3093. } else if (addr) {
  3094. return -EINVAL;
  3095. }
  3096. auto description = file_description(params.sockfd);
  3097. if (!description)
  3098. return -EBADF;
  3099. if (!description->is_socket())
  3100. return -ENOTSOCK;
  3101. auto& socket = *description->socket();
  3102. if (socket.is_shut_down_for_reading())
  3103. return 0;
  3104. bool original_blocking = description->is_blocking();
  3105. if (flags & MSG_DONTWAIT)
  3106. description->set_blocking(false);
  3107. auto nrecv = socket.recvfrom(*description, params.buffer.data, params.buffer.size, flags, addr, addr_length);
  3108. if (flags & MSG_DONTWAIT)
  3109. description->set_blocking(original_blocking);
  3110. return nrecv;
  3111. }
  3112. template<bool sockname, typename Params>
  3113. int Process::get_sock_or_peer_name(const Params& params)
  3114. {
  3115. socklen_t addrlen_value;
  3116. if (!validate_read_and_copy_typed(&addrlen_value, params.addrlen))
  3117. return -EFAULT;
  3118. if (addrlen_value <= 0)
  3119. return -EINVAL;
  3120. if (!validate_write(params.addr, addrlen_value))
  3121. return -EFAULT;
  3122. if (!validate_write_typed(params.addrlen))
  3123. return -EFAULT;
  3124. auto description = file_description(params.sockfd);
  3125. if (!description)
  3126. return -EBADF;
  3127. if (!description->is_socket())
  3128. return -ENOTSOCK;
  3129. auto& socket = *description->socket();
  3130. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  3131. u8 address_buffer[sizeof(sockaddr_un)];
  3132. addrlen_value = min(sizeof(sockaddr_un), static_cast<size_t>(addrlen_value));
  3133. if constexpr (sockname)
  3134. socket.get_local_address((sockaddr*)address_buffer, &addrlen_value);
  3135. else
  3136. socket.get_peer_address((sockaddr*)address_buffer, &addrlen_value);
  3137. copy_to_user(params.addr, address_buffer, addrlen_value);
  3138. copy_to_user(params.addrlen, &addrlen_value);
  3139. return 0;
  3140. }
  3141. int Process::sys$getsockname(const Syscall::SC_getsockname_params* user_params)
  3142. {
  3143. Syscall::SC_getsockname_params params;
  3144. if (!validate_read_and_copy_typed(&params, user_params))
  3145. return -EFAULT;
  3146. return get_sock_or_peer_name<true>(params);
  3147. }
  3148. int Process::sys$getpeername(const Syscall::SC_getpeername_params* user_params)
  3149. {
  3150. Syscall::SC_getpeername_params params;
  3151. if (!validate_read_and_copy_typed(&params, user_params))
  3152. return -EFAULT;
  3153. return get_sock_or_peer_name<false>(params);
  3154. }
  3155. int Process::sys$sched_setparam(int tid, const struct sched_param* param)
  3156. {
  3157. REQUIRE_PROMISE(proc);
  3158. if (!validate_read_typed(param))
  3159. return -EFAULT;
  3160. int desired_priority;
  3161. copy_from_user(&desired_priority, &param->sched_priority);
  3162. InterruptDisabler disabler;
  3163. auto* peer = Thread::current;
  3164. if (tid != 0)
  3165. peer = Thread::from_tid(tid);
  3166. if (!peer)
  3167. return -ESRCH;
  3168. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  3169. return -EPERM;
  3170. if (desired_priority < THREAD_PRIORITY_MIN || desired_priority > THREAD_PRIORITY_MAX)
  3171. return -EINVAL;
  3172. peer->set_priority((u32)desired_priority);
  3173. return 0;
  3174. }
  3175. int Process::sys$sched_getparam(pid_t pid, struct sched_param* param)
  3176. {
  3177. REQUIRE_PROMISE(proc);
  3178. if (!validate_write_typed(param))
  3179. return -EFAULT;
  3180. InterruptDisabler disabler;
  3181. auto* peer = Thread::current;
  3182. if (pid != 0)
  3183. peer = Thread::from_tid(pid);
  3184. if (!peer)
  3185. return -ESRCH;
  3186. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  3187. return -EPERM;
  3188. int priority = peer->priority();
  3189. copy_to_user(&param->sched_priority, &priority);
  3190. return 0;
  3191. }
  3192. int Process::sys$getsockopt(const Syscall::SC_getsockopt_params* params)
  3193. {
  3194. if (!validate_read_typed(params))
  3195. return -EFAULT;
  3196. SmapDisabler disabler;
  3197. int sockfd = params->sockfd;
  3198. int level = params->level;
  3199. int option = params->option;
  3200. void* value = params->value;
  3201. socklen_t* value_size = params->value_size;
  3202. if (!validate_write_typed(value_size))
  3203. return -EFAULT;
  3204. if (!validate_write(value, *value_size))
  3205. return -EFAULT;
  3206. auto description = file_description(sockfd);
  3207. if (!description)
  3208. return -EBADF;
  3209. if (!description->is_socket())
  3210. return -ENOTSOCK;
  3211. auto& socket = *description->socket();
  3212. if (has_promised(Pledge::accept) && socket.is_local() && level == SOL_SOCKET && option == SO_PEERCRED) {
  3213. // We make an exception for SOL_SOCKET::SO_PEERCRED on local sockets if you've pledged "accept"
  3214. } else {
  3215. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  3216. }
  3217. return socket.getsockopt(*description, level, option, value, value_size);
  3218. }
  3219. int Process::sys$setsockopt(const Syscall::SC_setsockopt_params* params)
  3220. {
  3221. if (!validate_read_typed(params))
  3222. return -EFAULT;
  3223. SmapDisabler disabler;
  3224. int sockfd = params->sockfd;
  3225. int level = params->level;
  3226. int option = params->option;
  3227. const void* value = params->value;
  3228. socklen_t value_size = params->value_size;
  3229. if (!validate_read(value, value_size))
  3230. return -EFAULT;
  3231. auto description = file_description(sockfd);
  3232. if (!description)
  3233. return -EBADF;
  3234. if (!description->is_socket())
  3235. return -ENOTSOCK;
  3236. auto& socket = *description->socket();
  3237. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  3238. return socket.setsockopt(level, option, value, value_size);
  3239. }
  3240. void Process::disown_all_shared_buffers()
  3241. {
  3242. LOCKER(shared_buffers().lock());
  3243. Vector<SharedBuffer*, 32> buffers_to_disown;
  3244. for (auto& it : shared_buffers().resource())
  3245. buffers_to_disown.append(it.value.ptr());
  3246. for (auto* shared_buffer : buffers_to_disown)
  3247. shared_buffer->disown(m_pid);
  3248. }
  3249. int Process::sys$shbuf_create(int size, void** buffer)
  3250. {
  3251. REQUIRE_PROMISE(shared_buffer);
  3252. if (!size || size < 0)
  3253. return -EINVAL;
  3254. size = PAGE_ROUND_UP(size);
  3255. if (!validate_write_typed(buffer))
  3256. return -EFAULT;
  3257. LOCKER(shared_buffers().lock());
  3258. static int s_next_shbuf_id;
  3259. int shbuf_id = ++s_next_shbuf_id;
  3260. auto shared_buffer = make<SharedBuffer>(shbuf_id, size);
  3261. shared_buffer->share_with(m_pid);
  3262. void* address = shared_buffer->ref_for_process_and_get_address(*this);
  3263. copy_to_user(buffer, &address);
  3264. ASSERT((int)shared_buffer->size() >= size);
  3265. #ifdef SHARED_BUFFER_DEBUG
  3266. klog() << "Created shared buffer " << shbuf_id << " @ " << buffer << " (" << size << " bytes, vmobject is " << shared_buffer->size() << ")";
  3267. #endif
  3268. shared_buffers().resource().set(shbuf_id, move(shared_buffer));
  3269. return shbuf_id;
  3270. }
  3271. int Process::sys$shbuf_allow_pid(int shbuf_id, pid_t peer_pid)
  3272. {
  3273. REQUIRE_PROMISE(shared_buffer);
  3274. if (!peer_pid || peer_pid < 0 || peer_pid == m_pid)
  3275. return -EINVAL;
  3276. LOCKER(shared_buffers().lock());
  3277. auto it = shared_buffers().resource().find(shbuf_id);
  3278. if (it == shared_buffers().resource().end())
  3279. return -EINVAL;
  3280. auto& shared_buffer = *(*it).value;
  3281. if (!shared_buffer.is_shared_with(m_pid))
  3282. return -EPERM;
  3283. {
  3284. InterruptDisabler disabler;
  3285. auto* peer = Process::from_pid(peer_pid);
  3286. if (!peer)
  3287. return -ESRCH;
  3288. }
  3289. shared_buffer.share_with(peer_pid);
  3290. return 0;
  3291. }
  3292. int Process::sys$shbuf_allow_all(int shbuf_id)
  3293. {
  3294. REQUIRE_PROMISE(shared_buffer);
  3295. LOCKER(shared_buffers().lock());
  3296. auto it = shared_buffers().resource().find(shbuf_id);
  3297. if (it == shared_buffers().resource().end())
  3298. return -EINVAL;
  3299. auto& shared_buffer = *(*it).value;
  3300. if (!shared_buffer.is_shared_with(m_pid))
  3301. return -EPERM;
  3302. shared_buffer.share_globally();
  3303. return 0;
  3304. }
  3305. int Process::sys$shbuf_release(int shbuf_id)
  3306. {
  3307. REQUIRE_PROMISE(shared_buffer);
  3308. LOCKER(shared_buffers().lock());
  3309. auto it = shared_buffers().resource().find(shbuf_id);
  3310. if (it == shared_buffers().resource().end())
  3311. return -EINVAL;
  3312. auto& shared_buffer = *(*it).value;
  3313. if (!shared_buffer.is_shared_with(m_pid))
  3314. return -EPERM;
  3315. #ifdef SHARED_BUFFER_DEBUG
  3316. klog() << "Releasing shared buffer " << shbuf_id << ", buffer count: " << shared_buffers().resource().size();
  3317. #endif
  3318. shared_buffer.deref_for_process(*this);
  3319. return 0;
  3320. }
  3321. void* Process::sys$shbuf_get(int shbuf_id, size_t* user_size)
  3322. {
  3323. REQUIRE_PROMISE(shared_buffer);
  3324. if (user_size && !validate_write_typed(user_size))
  3325. return (void*)-EFAULT;
  3326. LOCKER(shared_buffers().lock());
  3327. auto it = shared_buffers().resource().find(shbuf_id);
  3328. if (it == shared_buffers().resource().end())
  3329. return (void*)-EINVAL;
  3330. auto& shared_buffer = *(*it).value;
  3331. if (!shared_buffer.is_shared_with(m_pid))
  3332. return (void*)-EPERM;
  3333. #ifdef SHARED_BUFFER_DEBUG
  3334. klog() << "Retaining shared buffer " << shbuf_id << ", buffer count: " << shared_buffers().resource().size();
  3335. #endif
  3336. if (user_size) {
  3337. size_t size = shared_buffer.size();
  3338. copy_to_user(user_size, &size);
  3339. }
  3340. return shared_buffer.ref_for_process_and_get_address(*this);
  3341. }
  3342. int Process::sys$shbuf_seal(int shbuf_id)
  3343. {
  3344. REQUIRE_PROMISE(shared_buffer);
  3345. LOCKER(shared_buffers().lock());
  3346. auto it = shared_buffers().resource().find(shbuf_id);
  3347. if (it == shared_buffers().resource().end())
  3348. return -EINVAL;
  3349. auto& shared_buffer = *(*it).value;
  3350. if (!shared_buffer.is_shared_with(m_pid))
  3351. return -EPERM;
  3352. #ifdef SHARED_BUFFER_DEBUG
  3353. klog() << "Sealing shared buffer " << shbuf_id;
  3354. #endif
  3355. shared_buffer.seal();
  3356. return 0;
  3357. }
  3358. int Process::sys$shbuf_set_volatile(int shbuf_id, bool state)
  3359. {
  3360. REQUIRE_PROMISE(shared_buffer);
  3361. LOCKER(shared_buffers().lock());
  3362. auto it = shared_buffers().resource().find(shbuf_id);
  3363. if (it == shared_buffers().resource().end())
  3364. return -EINVAL;
  3365. auto& shared_buffer = *(*it).value;
  3366. if (!shared_buffer.is_shared_with(m_pid))
  3367. return -EPERM;
  3368. #ifdef SHARED_BUFFER_DEBUG
  3369. klog() << "Set shared buffer " << shbuf_id << " volatile: " << state;
  3370. #endif
  3371. if (!state) {
  3372. bool was_purged = shared_buffer.vmobject().was_purged();
  3373. shared_buffer.vmobject().set_volatile(state);
  3374. shared_buffer.vmobject().set_was_purged(false);
  3375. return was_purged ? 1 : 0;
  3376. }
  3377. shared_buffer.vmobject().set_volatile(true);
  3378. return 0;
  3379. }
  3380. void Process::terminate_due_to_signal(u8 signal)
  3381. {
  3382. ASSERT_INTERRUPTS_DISABLED();
  3383. ASSERT(signal < 32);
  3384. dbg() << "Terminating due to signal " << signal;
  3385. m_termination_status = 0;
  3386. m_termination_signal = signal;
  3387. die();
  3388. }
  3389. KResult Process::send_signal(u8 signal, Process* sender)
  3390. {
  3391. InterruptDisabler disabler;
  3392. if (auto* thread = Thread::from_tid(m_pid)) {
  3393. thread->send_signal(signal, sender);
  3394. return KSuccess;
  3395. }
  3396. return KResult(-ESRCH);
  3397. }
  3398. int Process::sys$create_thread(void* (*entry)(void*), const Syscall::SC_create_thread_params* user_params)
  3399. {
  3400. REQUIRE_PROMISE(thread);
  3401. if (!validate_read((const void*)entry, sizeof(void*)))
  3402. return -EFAULT;
  3403. Syscall::SC_create_thread_params params;
  3404. if (!validate_read_and_copy_typed(&params, user_params))
  3405. return -EFAULT;
  3406. unsigned detach_state = params.m_detach_state;
  3407. int schedule_priority = params.m_schedule_priority;
  3408. void* stack_location = params.m_stack_location;
  3409. unsigned stack_size = params.m_stack_size;
  3410. if (!validate_write(stack_location, stack_size))
  3411. return -EFAULT;
  3412. u32 user_stack_address = reinterpret_cast<u32>(stack_location) + stack_size;
  3413. if (!MM.validate_user_stack(*this, VirtualAddress(user_stack_address - 4)))
  3414. return -EFAULT;
  3415. // FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
  3416. int requested_thread_priority = schedule_priority;
  3417. if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
  3418. return -EINVAL;
  3419. bool is_thread_joinable = (0 == detach_state);
  3420. // FIXME: Do something with guard pages?
  3421. auto* thread = new Thread(*this);
  3422. // We know this thread is not the main_thread,
  3423. // So give it a unique name until the user calls $set_thread_name on it
  3424. // length + 4 to give space for our extra junk at the end
  3425. StringBuilder builder(m_name.length() + 4);
  3426. builder.append(m_name);
  3427. builder.appendf("[%d]", thread->tid());
  3428. thread->set_name(builder.to_string());
  3429. thread->set_priority(requested_thread_priority);
  3430. thread->set_joinable(is_thread_joinable);
  3431. auto& tss = thread->tss();
  3432. tss.eip = (FlatPtr)entry;
  3433. tss.eflags = 0x0202;
  3434. tss.cr3 = page_directory().cr3();
  3435. tss.esp = user_stack_address;
  3436. thread->make_thread_specific_region({});
  3437. thread->set_state(Thread::State::Runnable);
  3438. return thread->tid();
  3439. }
  3440. void Process::sys$exit_thread(void* exit_value)
  3441. {
  3442. REQUIRE_PROMISE(thread);
  3443. cli();
  3444. Thread::current->m_exit_value = exit_value;
  3445. Thread::current->set_should_die();
  3446. big_lock().force_unlock_if_locked();
  3447. Thread::current->die_if_needed();
  3448. ASSERT_NOT_REACHED();
  3449. }
  3450. int Process::sys$detach_thread(int tid)
  3451. {
  3452. REQUIRE_PROMISE(thread);
  3453. InterruptDisabler disabler;
  3454. auto* thread = Thread::from_tid(tid);
  3455. if (!thread || thread->pid() != pid())
  3456. return -ESRCH;
  3457. if (!thread->is_joinable())
  3458. return -EINVAL;
  3459. thread->set_joinable(false);
  3460. return 0;
  3461. }
  3462. int Process::sys$join_thread(int tid, void** exit_value)
  3463. {
  3464. REQUIRE_PROMISE(thread);
  3465. if (exit_value && !validate_write_typed(exit_value))
  3466. return -EFAULT;
  3467. InterruptDisabler disabler;
  3468. auto* thread = Thread::from_tid(tid);
  3469. if (!thread || thread->pid() != pid())
  3470. return -ESRCH;
  3471. if (thread == Thread::current)
  3472. return -EDEADLK;
  3473. if (thread->m_joinee == Thread::current)
  3474. return -EDEADLK;
  3475. ASSERT(thread->m_joiner != Thread::current);
  3476. if (thread->m_joiner)
  3477. return -EINVAL;
  3478. if (!thread->is_joinable())
  3479. return -EINVAL;
  3480. void* joinee_exit_value = nullptr;
  3481. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  3482. for (;;) {
  3483. auto result = Thread::current->block<Thread::JoinBlocker>(*thread, joinee_exit_value);
  3484. if (result == Thread::BlockResult::InterruptedByDeath) {
  3485. // NOTE: This cleans things up so that Thread::finalize() won't
  3486. // get confused about a missing joiner when finalizing the joinee.
  3487. InterruptDisabler disabler_t;
  3488. if (Thread::current->m_joinee) {
  3489. Thread::current->m_joinee->m_joiner = nullptr;
  3490. Thread::current->m_joinee = nullptr;
  3491. }
  3492. break;
  3493. }
  3494. }
  3495. // NOTE: 'thread' is very possibly deleted at this point. Clear it just to be safe.
  3496. thread = nullptr;
  3497. if (exit_value)
  3498. copy_to_user(exit_value, &joinee_exit_value);
  3499. return 0;
  3500. }
  3501. int Process::sys$set_thread_name(int tid, const char* user_name, size_t user_name_length)
  3502. {
  3503. REQUIRE_PROMISE(thread);
  3504. auto name = validate_and_copy_string_from_user(user_name, user_name_length);
  3505. if (name.is_null())
  3506. return -EFAULT;
  3507. const size_t max_thread_name_size = 64;
  3508. if (name.length() > max_thread_name_size)
  3509. return -EINVAL;
  3510. InterruptDisabler disabler;
  3511. auto* thread = Thread::from_tid(tid);
  3512. if (!thread || thread->pid() != pid())
  3513. return -ESRCH;
  3514. thread->set_name(name);
  3515. return 0;
  3516. }
  3517. int Process::sys$get_thread_name(int tid, char* buffer, size_t buffer_size)
  3518. {
  3519. REQUIRE_PROMISE(thread);
  3520. if (buffer_size == 0)
  3521. return -EINVAL;
  3522. if (!validate_write(buffer, buffer_size))
  3523. return -EFAULT;
  3524. InterruptDisabler disabler;
  3525. auto* thread = Thread::from_tid(tid);
  3526. if (!thread || thread->pid() != pid())
  3527. return -ESRCH;
  3528. if (thread->name().length() + 1 > (size_t)buffer_size)
  3529. return -ENAMETOOLONG;
  3530. copy_to_user(buffer, thread->name().characters(), thread->name().length() + 1);
  3531. return 0;
  3532. }
  3533. int Process::sys$gettid()
  3534. {
  3535. REQUIRE_PROMISE(stdio);
  3536. return Thread::current->tid();
  3537. }
  3538. int Process::sys$donate(int tid)
  3539. {
  3540. REQUIRE_PROMISE(stdio);
  3541. if (tid < 0)
  3542. return -EINVAL;
  3543. InterruptDisabler disabler;
  3544. auto* thread = Thread::from_tid(tid);
  3545. if (!thread || thread->pid() != pid())
  3546. return -ESRCH;
  3547. Scheduler::donate_to(thread, "sys$donate");
  3548. return 0;
  3549. }
  3550. int Process::sys$rename(const Syscall::SC_rename_params* user_params)
  3551. {
  3552. REQUIRE_PROMISE(cpath);
  3553. Syscall::SC_rename_params params;
  3554. if (!validate_read_and_copy_typed(&params, user_params))
  3555. return -EFAULT;
  3556. auto old_path = get_syscall_path_argument(params.old_path);
  3557. if (old_path.is_error())
  3558. return old_path.error();
  3559. auto new_path = get_syscall_path_argument(params.new_path);
  3560. if (new_path.is_error())
  3561. return new_path.error();
  3562. return VFS::the().rename(old_path.value(), new_path.value(), current_directory());
  3563. }
  3564. int Process::sys$ftruncate(int fd, off_t length)
  3565. {
  3566. REQUIRE_PROMISE(stdio);
  3567. if (length < 0)
  3568. return -EINVAL;
  3569. auto description = file_description(fd);
  3570. if (!description)
  3571. return -EBADF;
  3572. if (!description->is_writable())
  3573. return -EBADF;
  3574. return description->truncate(static_cast<u64>(length));
  3575. }
  3576. int Process::sys$watch_file(const char* user_path, size_t path_length)
  3577. {
  3578. REQUIRE_PROMISE(rpath);
  3579. auto path = get_syscall_path_argument(user_path, path_length);
  3580. if (path.is_error())
  3581. return path.error();
  3582. auto custody_or_error = VFS::the().resolve_path(path.value(), current_directory());
  3583. if (custody_or_error.is_error())
  3584. return custody_or_error.error();
  3585. auto& custody = custody_or_error.value();
  3586. auto& inode = custody->inode();
  3587. if (!inode.fs().supports_watchers())
  3588. return -ENOTSUP;
  3589. int fd = alloc_fd();
  3590. if (fd < 0)
  3591. return fd;
  3592. m_fds[fd].set(FileDescription::create(*InodeWatcher::create(inode)));
  3593. m_fds[fd].description->set_readable(true);
  3594. return fd;
  3595. }
  3596. int Process::sys$halt()
  3597. {
  3598. if (!is_superuser())
  3599. return -EPERM;
  3600. REQUIRE_NO_PROMISES;
  3601. dbg() << "acquiring FS locks...";
  3602. FS::lock_all();
  3603. dbg() << "syncing mounted filesystems...";
  3604. FS::sync();
  3605. dbg() << "attempting system shutdown...";
  3606. IO::out16(0x604, 0x2000);
  3607. return 0;
  3608. }
  3609. int Process::sys$reboot()
  3610. {
  3611. if (!is_superuser())
  3612. return -EPERM;
  3613. REQUIRE_NO_PROMISES;
  3614. dbg() << "acquiring FS locks...";
  3615. FS::lock_all();
  3616. dbg() << "syncing mounted filesystems...";
  3617. FS::sync();
  3618. dbg() << "attempting reboot via ACPI";
  3619. if (ACPI::is_enabled())
  3620. ACPI::Parser::the()->try_acpi_reboot();
  3621. dbg() << "attempting reboot via KB Controller...";
  3622. IO::out8(0x64, 0xFE);
  3623. return 0;
  3624. }
  3625. int Process::sys$mount(const Syscall::SC_mount_params* user_params)
  3626. {
  3627. if (!is_superuser())
  3628. return -EPERM;
  3629. REQUIRE_NO_PROMISES;
  3630. Syscall::SC_mount_params params;
  3631. if (!validate_read_and_copy_typed(&params, user_params))
  3632. return -EFAULT;
  3633. auto source_fd = params.source_fd;
  3634. auto target = validate_and_copy_string_from_user(params.target);
  3635. auto fs_type = validate_and_copy_string_from_user(params.fs_type);
  3636. if (target.is_null())
  3637. return -EFAULT;
  3638. auto description = file_description(source_fd);
  3639. if (!description.is_null())
  3640. dbg() << "mount " << fs_type << ": source fd " << source_fd << " @ " << target;
  3641. else
  3642. dbg() << "mount " << fs_type << " @ " << target;
  3643. auto custody_or_error = VFS::the().resolve_path(target, current_directory());
  3644. if (custody_or_error.is_error())
  3645. return custody_or_error.error();
  3646. auto& target_custody = custody_or_error.value();
  3647. if (params.flags & MS_REMOUNT) {
  3648. // We're not creating a new mount, we're updating an existing one!
  3649. return VFS::the().remount(target_custody, params.flags & ~MS_REMOUNT);
  3650. }
  3651. if (params.flags & MS_BIND) {
  3652. // We're doing a bind mount.
  3653. if (description.is_null())
  3654. return -EBADF;
  3655. if (!description->custody()) {
  3656. // We only support bind-mounting inodes, not arbitrary files.
  3657. return -ENODEV;
  3658. }
  3659. return VFS::the().bind_mount(*description->custody(), target_custody, params.flags);
  3660. }
  3661. RefPtr<FS> fs;
  3662. if (fs_type == "ext2" || fs_type == "Ext2FS") {
  3663. if (description.is_null())
  3664. return -EBADF;
  3665. if (!description->file().is_seekable()) {
  3666. dbg() << "mount: this is not a seekable file";
  3667. return -ENODEV;
  3668. }
  3669. dbg() << "mount: attempting to mount " << description->absolute_path() << " on " << target;
  3670. fs = Ext2FS::create(*description);
  3671. } else if (fs_type == "proc" || fs_type == "ProcFS") {
  3672. fs = ProcFS::create();
  3673. } else if (fs_type == "devpts" || fs_type == "DevPtsFS") {
  3674. fs = DevPtsFS::create();
  3675. } else if (fs_type == "tmp" || fs_type == "TmpFS") {
  3676. fs = TmpFS::create();
  3677. } else {
  3678. return -ENODEV;
  3679. }
  3680. if (!fs->initialize()) {
  3681. dbg() << "mount: failed to initialize " << fs_type << " filesystem, fd - " << source_fd;
  3682. return -ENODEV;
  3683. }
  3684. auto result = VFS::the().mount(fs.release_nonnull(), target_custody, params.flags);
  3685. if (!description.is_null())
  3686. dbg() << "mount: successfully mounted " << description->absolute_path() << " on " << target;
  3687. else
  3688. dbg() << "mount: successfully mounted " << target;
  3689. return result;
  3690. }
  3691. int Process::sys$umount(const char* user_mountpoint, size_t mountpoint_length)
  3692. {
  3693. if (!is_superuser())
  3694. return -EPERM;
  3695. REQUIRE_NO_PROMISES;
  3696. if (!validate_read(user_mountpoint, mountpoint_length))
  3697. return -EFAULT;
  3698. auto mountpoint = get_syscall_path_argument(user_mountpoint, mountpoint_length);
  3699. if (mountpoint.is_error())
  3700. return mountpoint.error();
  3701. auto custody_or_error = VFS::the().resolve_path(mountpoint.value(), current_directory());
  3702. if (custody_or_error.is_error())
  3703. return custody_or_error.error();
  3704. auto& guest_inode = custody_or_error.value()->inode();
  3705. return VFS::the().unmount(guest_inode);
  3706. }
  3707. void Process::FileDescriptionAndFlags::clear()
  3708. {
  3709. description = nullptr;
  3710. flags = 0;
  3711. }
  3712. void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& d, u32 f)
  3713. {
  3714. description = move(d);
  3715. flags = f;
  3716. }
  3717. int Process::sys$mknod(const Syscall::SC_mknod_params* user_params)
  3718. {
  3719. REQUIRE_PROMISE(dpath);
  3720. Syscall::SC_mknod_params params;
  3721. if (!validate_read_and_copy_typed(&params, user_params))
  3722. return -EFAULT;
  3723. if (!is_superuser() && !is_regular_file(params.mode) && !is_fifo(params.mode) && !is_socket(params.mode))
  3724. return -EPERM;
  3725. auto path = get_syscall_path_argument(params.path);
  3726. if (path.is_error())
  3727. return path.error();
  3728. return VFS::the().mknod(path.value(), params.mode & ~umask(), params.dev, current_directory());
  3729. }
  3730. int Process::sys$dump_backtrace()
  3731. {
  3732. dump_backtrace();
  3733. return 0;
  3734. }
  3735. int Process::sys$dbgputch(u8 ch)
  3736. {
  3737. IO::out8(0xe9, ch);
  3738. return 0;
  3739. }
  3740. int Process::sys$dbgputstr(const u8* characters, int length)
  3741. {
  3742. if (!length)
  3743. return 0;
  3744. if (!validate_read(characters, length))
  3745. return -EFAULT;
  3746. SmapDisabler disabler;
  3747. for (int i = 0; i < length; ++i)
  3748. IO::out8(0xe9, characters[i]);
  3749. return 0;
  3750. }
  3751. KBuffer Process::backtrace(ProcessInspectionHandle& handle) const
  3752. {
  3753. KBufferBuilder builder;
  3754. for_each_thread([&](Thread& thread) {
  3755. builder.appendf("Thread %d (%s):\n", thread.tid(), thread.name().characters());
  3756. builder.append(thread.backtrace(handle));
  3757. return IterationDecision::Continue;
  3758. });
  3759. return builder.build();
  3760. }
  3761. int Process::sys$set_process_icon(int icon_id)
  3762. {
  3763. REQUIRE_PROMISE(shared_buffer);
  3764. LOCKER(shared_buffers().lock());
  3765. auto it = shared_buffers().resource().find(icon_id);
  3766. if (it == shared_buffers().resource().end())
  3767. return -EINVAL;
  3768. auto& shared_buffer = *(*it).value;
  3769. if (!shared_buffer.is_shared_with(m_pid))
  3770. return -EPERM;
  3771. m_icon_id = icon_id;
  3772. return 0;
  3773. }
  3774. int Process::sys$get_process_name(char* buffer, int buffer_size)
  3775. {
  3776. REQUIRE_PROMISE(stdio);
  3777. if (buffer_size <= 0)
  3778. return -EINVAL;
  3779. if (!validate_write(buffer, buffer_size))
  3780. return -EFAULT;
  3781. if (m_name.length() + 1 > (size_t)buffer_size)
  3782. return -ENAMETOOLONG;
  3783. copy_to_user(buffer, m_name.characters(), m_name.length() + 1);
  3784. return 0;
  3785. }
  3786. // We don't use the flag yet, but we could use it for distinguishing
  3787. // random source like Linux, unlike the OpenBSD equivalent. However, if we
  3788. // do, we should be able of the caveats that Linux has dealt with.
  3789. ssize_t Process::sys$getrandom(void* buffer, size_t buffer_size, unsigned int flags __attribute__((unused)))
  3790. {
  3791. REQUIRE_PROMISE(stdio);
  3792. if (buffer_size <= 0)
  3793. return -EINVAL;
  3794. if (!validate_write(buffer, buffer_size))
  3795. return -EFAULT;
  3796. SmapDisabler disabler;
  3797. get_good_random_bytes((u8*)buffer, buffer_size);
  3798. return 0;
  3799. }
  3800. int Process::sys$setkeymap(const Syscall::SC_setkeymap_params* user_params)
  3801. {
  3802. REQUIRE_PROMISE(setkeymap);
  3803. if (!is_superuser())
  3804. return -EPERM;
  3805. Syscall::SC_setkeymap_params params;
  3806. if (!validate_read_and_copy_typed(&params, user_params))
  3807. return -EFAULT;
  3808. Keyboard::CharacterMapData character_map_data;
  3809. if (!validate_read(params.map, CHAR_MAP_SIZE))
  3810. return -EFAULT;
  3811. if (!validate_read(params.shift_map, CHAR_MAP_SIZE))
  3812. return -EFAULT;
  3813. if (!validate_read(params.alt_map, CHAR_MAP_SIZE))
  3814. return -EFAULT;
  3815. if (!validate_read(params.altgr_map, CHAR_MAP_SIZE))
  3816. return -EFAULT;
  3817. copy_from_user(character_map_data.map, params.map, CHAR_MAP_SIZE * sizeof(u32));
  3818. copy_from_user(character_map_data.shift_map, params.shift_map, CHAR_MAP_SIZE * sizeof(u32));
  3819. copy_from_user(character_map_data.alt_map, params.alt_map, CHAR_MAP_SIZE * sizeof(u32));
  3820. copy_from_user(character_map_data.altgr_map, params.altgr_map, CHAR_MAP_SIZE * sizeof(u32));
  3821. KeyboardDevice::the().set_maps(character_map_data);
  3822. return 0;
  3823. }
  3824. int Process::sys$clock_gettime(clockid_t clock_id, timespec* user_ts)
  3825. {
  3826. REQUIRE_PROMISE(stdio);
  3827. if (!validate_write_typed(user_ts))
  3828. return -EFAULT;
  3829. timespec ts;
  3830. memset(&ts, 0, sizeof(ts));
  3831. switch (clock_id) {
  3832. case CLOCK_MONOTONIC:
  3833. ts.tv_sec = TimeManagement::the().seconds_since_boot();
  3834. ts.tv_nsec = TimeManagement::the().ticks_this_second() * 1000000;
  3835. break;
  3836. case CLOCK_REALTIME:
  3837. ts.tv_sec = TimeManagement::the().epoch_time();
  3838. ts.tv_nsec = TimeManagement::the().ticks_this_second() * 1000000;
  3839. break;
  3840. default:
  3841. return -EINVAL;
  3842. }
  3843. copy_to_user(user_ts, &ts);
  3844. return 0;
  3845. }
  3846. int Process::sys$clock_settime(clockid_t clock_id, timespec* user_ts)
  3847. {
  3848. REQUIRE_PROMISE(settime);
  3849. if (!is_superuser())
  3850. return -EPERM;
  3851. timespec ts;
  3852. if (!validate_read_and_copy_typed(&ts, user_ts))
  3853. return -EFAULT;
  3854. switch (clock_id) {
  3855. case CLOCK_REALTIME:
  3856. TimeManagement::the().set_epoch_time(ts.tv_sec);
  3857. break;
  3858. default:
  3859. return -EINVAL;
  3860. }
  3861. return 0;
  3862. }
  3863. int Process::sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params* user_params)
  3864. {
  3865. REQUIRE_PROMISE(stdio);
  3866. Syscall::SC_clock_nanosleep_params params;
  3867. if (!validate_read_and_copy_typed(&params, user_params))
  3868. return -EFAULT;
  3869. if (params.requested_sleep && !validate_read_typed(params.requested_sleep))
  3870. return -EFAULT;
  3871. timespec requested_sleep;
  3872. copy_from_user(&requested_sleep, params.requested_sleep);
  3873. if (params.remaining_sleep && !validate_write_typed(params.remaining_sleep))
  3874. return -EFAULT;
  3875. bool is_absolute = params.flags & TIMER_ABSTIME;
  3876. switch (params.clock_id) {
  3877. case CLOCK_MONOTONIC: {
  3878. u64 wakeup_time;
  3879. if (is_absolute) {
  3880. u64 time_to_wake = (requested_sleep.tv_sec * 1000 + requested_sleep.tv_nsec / 1000000);
  3881. wakeup_time = Thread::current->sleep_until(time_to_wake);
  3882. } else {
  3883. u32 ticks_to_sleep = (requested_sleep.tv_sec * 1000 + requested_sleep.tv_nsec / 1000000);
  3884. if (!ticks_to_sleep)
  3885. return 0;
  3886. wakeup_time = Thread::current->sleep(ticks_to_sleep);
  3887. }
  3888. if (wakeup_time > g_uptime) {
  3889. u32 ticks_left = wakeup_time - g_uptime;
  3890. if (!is_absolute && params.remaining_sleep) {
  3891. if (!validate_write_typed(params.remaining_sleep)) {
  3892. // This can happen because the lock is dropped while
  3893. // sleeping, thus giving other threads the opportunity
  3894. // to make the region unwritable.
  3895. return -EFAULT;
  3896. }
  3897. timespec remaining_sleep;
  3898. memset(&remaining_sleep, 0, sizeof(timespec));
  3899. remaining_sleep.tv_sec = ticks_left / TimeManagement::the().ticks_per_second();
  3900. ticks_left -= remaining_sleep.tv_sec * TimeManagement::the().ticks_per_second();
  3901. remaining_sleep.tv_nsec = ticks_left * 1000000;
  3902. copy_to_user(params.remaining_sleep, &remaining_sleep);
  3903. }
  3904. return -EINTR;
  3905. }
  3906. return 0;
  3907. }
  3908. default:
  3909. return -EINVAL;
  3910. }
  3911. }
  3912. int Process::sys$sync()
  3913. {
  3914. REQUIRE_PROMISE(stdio);
  3915. VFS::the().sync();
  3916. return 0;
  3917. }
  3918. int Process::sys$yield()
  3919. {
  3920. REQUIRE_PROMISE(stdio);
  3921. Thread::current->yield_without_holding_big_lock();
  3922. return 0;
  3923. }
  3924. int Process::sys$beep()
  3925. {
  3926. PCSpeaker::tone_on(440);
  3927. u64 wakeup_time = Thread::current->sleep(100);
  3928. PCSpeaker::tone_off();
  3929. if (wakeup_time > g_uptime)
  3930. return -EINTR;
  3931. return 0;
  3932. }
  3933. int Process::sys$module_load(const char* user_path, size_t path_length)
  3934. {
  3935. if (!is_superuser())
  3936. return -EPERM;
  3937. REQUIRE_NO_PROMISES;
  3938. auto path = get_syscall_path_argument(user_path, path_length);
  3939. if (path.is_error())
  3940. return path.error();
  3941. auto description_or_error = VFS::the().open(path.value(), O_RDONLY, 0, current_directory());
  3942. if (description_or_error.is_error())
  3943. return description_or_error.error();
  3944. auto& description = description_or_error.value();
  3945. auto payload_or_error = description->read_entire_file();
  3946. if (payload_or_error.is_error())
  3947. return payload_or_error.error();
  3948. auto payload = payload_or_error.value();
  3949. auto storage = KBuffer::create_with_size(payload.size());
  3950. memcpy(storage.data(), payload.data(), payload.size());
  3951. payload.clear();
  3952. auto elf_image = make<ELF::Image>(storage.data(), storage.size());
  3953. if (!elf_image->parse())
  3954. return -ENOEXEC;
  3955. HashMap<String, u8*> section_storage_by_name;
  3956. auto module = make<Module>();
  3957. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  3958. if (!section.size())
  3959. return IterationDecision::Continue;
  3960. auto section_storage = KBuffer::copy(section.raw_data(), section.size(), Region::Access::Read | Region::Access::Write | Region::Access::Execute);
  3961. section_storage_by_name.set(section.name(), section_storage.data());
  3962. module->sections.append(move(section_storage));
  3963. return IterationDecision::Continue;
  3964. });
  3965. bool missing_symbols = false;
  3966. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  3967. if (!section.size())
  3968. return IterationDecision::Continue;
  3969. auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
  3970. ASSERT(section_storage);
  3971. section.relocations().for_each_relocation([&](const ELF::Image::Relocation& relocation) {
  3972. auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
  3973. switch (relocation.type()) {
  3974. case R_386_PC32: {
  3975. // PC-relative relocation
  3976. dbg() << "PC-relative relocation: " << relocation.symbol().name();
  3977. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  3978. if (symbol_address == 0)
  3979. missing_symbols = true;
  3980. dbg() << " Symbol address: " << (void*)symbol_address;
  3981. ptrdiff_t relative_offset = (char*)symbol_address - ((char*)&patch_ptr + 4);
  3982. patch_ptr = relative_offset;
  3983. break;
  3984. }
  3985. case R_386_32: // Absolute relocation
  3986. dbg() << "Absolute relocation: '" << relocation.symbol().name() << "' value:" << relocation.symbol().value() << ", index:" << relocation.symbol_index();
  3987. if (relocation.symbol().bind() == STB_LOCAL) {
  3988. auto* section_storage_containing_symbol = section_storage_by_name.get(relocation.symbol().section().name()).value_or(nullptr);
  3989. ASSERT(section_storage_containing_symbol);
  3990. u32 symbol_address = (ptrdiff_t)(section_storage_containing_symbol + relocation.symbol().value());
  3991. if (symbol_address == 0)
  3992. missing_symbols = true;
  3993. dbg() << " Symbol address: " << (void*)symbol_address;
  3994. patch_ptr += symbol_address;
  3995. } else if (relocation.symbol().bind() == STB_GLOBAL) {
  3996. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  3997. if (symbol_address == 0)
  3998. missing_symbols = true;
  3999. dbg() << " Symbol address: " << (void*)symbol_address;
  4000. patch_ptr += symbol_address;
  4001. } else {
  4002. ASSERT_NOT_REACHED();
  4003. }
  4004. break;
  4005. }
  4006. return IterationDecision::Continue;
  4007. });
  4008. return IterationDecision::Continue;
  4009. });
  4010. if (missing_symbols)
  4011. return -EINVAL;
  4012. auto* text_base = section_storage_by_name.get(".text").value_or(nullptr);
  4013. if (!text_base) {
  4014. dbg() << "No .text section found in module!";
  4015. return -EINVAL;
  4016. }
  4017. elf_image->for_each_symbol([&](const ELF::Image::Symbol& symbol) {
  4018. dbg() << " - " << symbol.type() << " '" << symbol.name() << "' @ " << (void*)symbol.value() << ", size=" << symbol.size();
  4019. if (symbol.name() == "module_init") {
  4020. module->module_init = (ModuleInitPtr)(text_base + symbol.value());
  4021. } else if (symbol.name() == "module_fini") {
  4022. module->module_fini = (ModuleFiniPtr)(text_base + symbol.value());
  4023. } else if (symbol.name() == "module_name") {
  4024. const u8* storage = section_storage_by_name.get(symbol.section().name()).value_or(nullptr);
  4025. if (storage)
  4026. module->name = String((const char*)(storage + symbol.value()));
  4027. }
  4028. return IterationDecision::Continue;
  4029. });
  4030. if (!module->module_init)
  4031. return -EINVAL;
  4032. if (g_modules->contains(module->name)) {
  4033. dbg() << "a module with the name " << module->name << " is already loaded; please unload it first";
  4034. return -EEXIST;
  4035. }
  4036. module->module_init();
  4037. auto name = module->name;
  4038. g_modules->set(name, move(module));
  4039. return 0;
  4040. }
  4041. int Process::sys$module_unload(const char* user_name, size_t name_length)
  4042. {
  4043. if (!is_superuser())
  4044. return -EPERM;
  4045. REQUIRE_NO_PROMISES;
  4046. auto module_name = validate_and_copy_string_from_user(user_name, name_length);
  4047. if (module_name.is_null())
  4048. return -EFAULT;
  4049. auto it = g_modules->find(module_name);
  4050. if (it == g_modules->end())
  4051. return -ENOENT;
  4052. if (it->value->module_fini)
  4053. it->value->module_fini();
  4054. g_modules->remove(it);
  4055. return 0;
  4056. }
  4057. int Process::sys$profiling_enable(pid_t pid)
  4058. {
  4059. REQUIRE_NO_PROMISES;
  4060. InterruptDisabler disabler;
  4061. auto* process = Process::from_pid(pid);
  4062. if (!process)
  4063. return -ESRCH;
  4064. if (process->is_dead())
  4065. return -ESRCH;
  4066. if (!is_superuser() && process->uid() != m_uid)
  4067. return -EPERM;
  4068. Profiling::start(*process);
  4069. process->set_profiling(true);
  4070. return 0;
  4071. }
  4072. int Process::sys$profiling_disable(pid_t pid)
  4073. {
  4074. InterruptDisabler disabler;
  4075. auto* process = Process::from_pid(pid);
  4076. if (!process)
  4077. return -ESRCH;
  4078. if (!is_superuser() && process->uid() != m_uid)
  4079. return -EPERM;
  4080. process->set_profiling(false);
  4081. Profiling::stop();
  4082. return 0;
  4083. }
  4084. WaitQueue& Process::futex_queue(i32* userspace_address)
  4085. {
  4086. auto& queue = m_futex_queues.ensure((FlatPtr)userspace_address);
  4087. if (!queue)
  4088. queue = make<WaitQueue>();
  4089. return *queue;
  4090. }
  4091. int Process::sys$futex(const Syscall::SC_futex_params* user_params)
  4092. {
  4093. REQUIRE_PROMISE(thread);
  4094. Syscall::SC_futex_params params;
  4095. if (!validate_read_and_copy_typed(&params, user_params))
  4096. return -EFAULT;
  4097. i32* userspace_address = params.userspace_address;
  4098. int futex_op = params.futex_op;
  4099. i32 value = params.val;
  4100. const timespec* user_timeout = params.timeout;
  4101. if (!validate_read_typed(userspace_address))
  4102. return -EFAULT;
  4103. if (user_timeout && !validate_read_typed(user_timeout))
  4104. return -EFAULT;
  4105. switch (futex_op) {
  4106. case FUTEX_WAIT: {
  4107. i32 user_value;
  4108. copy_from_user(&user_value, userspace_address);
  4109. if (user_value != value)
  4110. return -EAGAIN;
  4111. timespec ts_abstimeout { 0, 0 };
  4112. if (user_timeout && !validate_read_and_copy_typed(&ts_abstimeout, user_timeout))
  4113. return -EFAULT;
  4114. WaitQueue& wait_queue = futex_queue(userspace_address);
  4115. timeval* optional_timeout = nullptr;
  4116. timeval relative_timeout { 0, 0 };
  4117. if (user_timeout) {
  4118. compute_relative_timeout_from_absolute(ts_abstimeout, relative_timeout);
  4119. optional_timeout = &relative_timeout;
  4120. }
  4121. // FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
  4122. Thread::BlockResult result = Thread::current->wait_on(wait_queue, optional_timeout);
  4123. if (result == Thread::BlockResult::InterruptedByTimeout) {
  4124. return -ETIMEDOUT;
  4125. }
  4126. break;
  4127. }
  4128. case FUTEX_WAKE:
  4129. if (value == 0)
  4130. return 0;
  4131. if (value == 1) {
  4132. futex_queue(userspace_address).wake_one();
  4133. } else {
  4134. futex_queue(userspace_address).wake_n(value);
  4135. }
  4136. break;
  4137. }
  4138. return 0;
  4139. }
  4140. int Process::sys$set_thread_boost(int tid, int amount)
  4141. {
  4142. REQUIRE_PROMISE(proc);
  4143. if (amount < 0 || amount > 20)
  4144. return -EINVAL;
  4145. InterruptDisabler disabler;
  4146. auto* thread = Thread::from_tid(tid);
  4147. if (!thread)
  4148. return -ESRCH;
  4149. if (thread->state() == Thread::State::Dead || thread->state() == Thread::State::Dying)
  4150. return -ESRCH;
  4151. if (!is_superuser() && thread->process().uid() != euid())
  4152. return -EPERM;
  4153. thread->set_priority_boost(amount);
  4154. return 0;
  4155. }
  4156. int Process::sys$set_process_boost(pid_t pid, int amount)
  4157. {
  4158. REQUIRE_PROMISE(proc);
  4159. if (amount < 0 || amount > 20)
  4160. return -EINVAL;
  4161. InterruptDisabler disabler;
  4162. auto* process = Process::from_pid(pid);
  4163. if (!process || process->is_dead())
  4164. return -ESRCH;
  4165. if (!is_superuser() && process->uid() != euid())
  4166. return -EPERM;
  4167. process->m_priority_boost = amount;
  4168. return 0;
  4169. }
  4170. int Process::sys$chroot(const char* user_path, size_t path_length, int mount_flags)
  4171. {
  4172. if (!is_superuser())
  4173. return -EPERM;
  4174. REQUIRE_PROMISE(chroot);
  4175. auto path = get_syscall_path_argument(user_path, path_length);
  4176. if (path.is_error())
  4177. return path.error();
  4178. auto directory_or_error = VFS::the().open_directory(path.value(), current_directory());
  4179. if (directory_or_error.is_error())
  4180. return directory_or_error.error();
  4181. auto directory = directory_or_error.value();
  4182. m_root_directory_relative_to_global_root = directory;
  4183. int chroot_mount_flags = mount_flags == -1 ? directory->mount_flags() : mount_flags;
  4184. set_root_directory(Custody::create(nullptr, "", directory->inode(), chroot_mount_flags));
  4185. return 0;
  4186. }
  4187. Custody& Process::root_directory()
  4188. {
  4189. if (!m_root_directory)
  4190. m_root_directory = VFS::the().root_custody();
  4191. return *m_root_directory;
  4192. }
  4193. Custody& Process::root_directory_relative_to_global_root()
  4194. {
  4195. if (!m_root_directory_relative_to_global_root)
  4196. m_root_directory_relative_to_global_root = root_directory();
  4197. return *m_root_directory_relative_to_global_root;
  4198. }
  4199. void Process::set_root_directory(const Custody& root)
  4200. {
  4201. m_root_directory = root;
  4202. }
  4203. int Process::sys$pledge(const Syscall::SC_pledge_params* user_params)
  4204. {
  4205. Syscall::SC_pledge_params params;
  4206. if (!validate_read_and_copy_typed(&params, user_params))
  4207. return -EFAULT;
  4208. if (params.promises.length > 1024 || params.execpromises.length > 1024)
  4209. return -E2BIG;
  4210. String promises;
  4211. if (params.promises.characters) {
  4212. promises = validate_and_copy_string_from_user(params.promises);
  4213. if (promises.is_null())
  4214. return -EFAULT;
  4215. }
  4216. String execpromises;
  4217. if (params.execpromises.characters) {
  4218. execpromises = validate_and_copy_string_from_user(params.execpromises);
  4219. if (execpromises.is_null())
  4220. return -EFAULT;
  4221. }
  4222. auto parse_pledge = [&](auto& pledge_spec, u32& mask) {
  4223. auto parts = pledge_spec.split_view(' ');
  4224. for (auto& part : parts) {
  4225. #define __ENUMERATE_PLEDGE_PROMISE(x) \
  4226. if (part == #x) { \
  4227. mask |= (1u << (u32)Pledge::x); \
  4228. continue; \
  4229. }
  4230. ENUMERATE_PLEDGE_PROMISES
  4231. #undef __ENUMERATE_PLEDGE_PROMISE
  4232. if (part == "dns") {
  4233. // "dns" is an alias for "unix" since DNS queries go via LookupServer
  4234. mask |= (1u << (u32)Pledge::unix);
  4235. continue;
  4236. }
  4237. return false;
  4238. }
  4239. return true;
  4240. };
  4241. u32 new_promises;
  4242. u32 new_execpromises;
  4243. if (!promises.is_null()) {
  4244. new_promises = 0;
  4245. if (!parse_pledge(promises, new_promises))
  4246. return -EINVAL;
  4247. if (m_promises && (!new_promises || new_promises & ~m_promises))
  4248. return -EPERM;
  4249. } else {
  4250. new_promises = m_promises;
  4251. }
  4252. if (!execpromises.is_null()) {
  4253. new_execpromises = 0;
  4254. if (!parse_pledge(execpromises, new_execpromises))
  4255. return -EINVAL;
  4256. if (m_execpromises && (!new_execpromises || new_execpromises & ~m_execpromises))
  4257. return -EPERM;
  4258. } else {
  4259. new_execpromises = m_execpromises;
  4260. }
  4261. m_promises = new_promises;
  4262. m_execpromises = new_execpromises;
  4263. return 0;
  4264. }
  4265. Region& Process::add_region(NonnullOwnPtr<Region> region)
  4266. {
  4267. auto* ptr = region.ptr();
  4268. m_regions.append(move(region));
  4269. return *ptr;
  4270. }
  4271. int Process::sys$unveil(const Syscall::SC_unveil_params* user_params)
  4272. {
  4273. Syscall::SC_unveil_params params;
  4274. if (!validate_read_and_copy_typed(&params, user_params))
  4275. return -EFAULT;
  4276. if (!params.path.characters && !params.permissions.characters) {
  4277. m_veil_state = VeilState::Locked;
  4278. return 0;
  4279. }
  4280. if (m_veil_state == VeilState::Locked)
  4281. return -EPERM;
  4282. if (!params.path.characters || !params.permissions.characters)
  4283. return -EINVAL;
  4284. if (params.permissions.length > 4)
  4285. return -EINVAL;
  4286. auto path = get_syscall_path_argument(params.path);
  4287. if (path.is_error())
  4288. return path.error();
  4289. if (path.value().is_empty() || path.value().characters()[0] != '/')
  4290. return -EINVAL;
  4291. auto custody_or_error = VFS::the().resolve_path_without_veil(path.value(), root_directory());
  4292. if (custody_or_error.is_error())
  4293. // FIXME Should this be EINVAL?
  4294. return custody_or_error.error();
  4295. auto& custody = custody_or_error.value();
  4296. auto new_unveiled_path = custody->absolute_path();
  4297. auto permissions = validate_and_copy_string_from_user(params.permissions);
  4298. if (permissions.is_null())
  4299. return -EFAULT;
  4300. unsigned new_permissions = 0;
  4301. for (size_t i = 0; i < permissions.length(); ++i) {
  4302. switch (permissions[i]) {
  4303. case 'r':
  4304. new_permissions |= UnveiledPath::Access::Read;
  4305. break;
  4306. case 'w':
  4307. new_permissions |= UnveiledPath::Access::Write;
  4308. break;
  4309. case 'x':
  4310. new_permissions |= UnveiledPath::Access::Execute;
  4311. break;
  4312. case 'c':
  4313. new_permissions |= UnveiledPath::Access::CreateOrRemove;
  4314. break;
  4315. default:
  4316. return -EINVAL;
  4317. }
  4318. }
  4319. for (size_t i = 0; i < m_unveiled_paths.size(); ++i) {
  4320. auto& unveiled_path = m_unveiled_paths[i];
  4321. if (unveiled_path.path == new_unveiled_path) {
  4322. if (new_permissions & ~unveiled_path.permissions)
  4323. return -EPERM;
  4324. unveiled_path.permissions = new_permissions;
  4325. return 0;
  4326. }
  4327. }
  4328. m_unveiled_paths.append({ new_unveiled_path, new_permissions });
  4329. ASSERT(m_veil_state != VeilState::Locked);
  4330. m_veil_state = VeilState::Dropped;
  4331. return 0;
  4332. }
  4333. int Process::sys$perf_event(int type, FlatPtr arg1, FlatPtr arg2)
  4334. {
  4335. if (!m_perf_event_buffer)
  4336. m_perf_event_buffer = make<PerformanceEventBuffer>();
  4337. return m_perf_event_buffer->append(type, arg1, arg2);
  4338. }
  4339. void Process::set_tty(TTY* tty)
  4340. {
  4341. m_tty = tty;
  4342. }
  4343. OwnPtr<Process::ELFBundle> Process::elf_bundle() const
  4344. {
  4345. if (!m_executable)
  4346. return nullptr;
  4347. auto bundle = make<ELFBundle>();
  4348. if (!m_executable->inode().shared_vmobject()) {
  4349. return nullptr;
  4350. }
  4351. ASSERT(m_executable->inode().shared_vmobject());
  4352. auto& vmobject = *m_executable->inode().shared_vmobject();
  4353. bundle->region = MM.allocate_kernel_region_with_vmobject(const_cast<SharedInodeVMObject&>(vmobject), vmobject.size(), "ELF bundle", Region::Access::Read);
  4354. if (!bundle->region)
  4355. return nullptr;
  4356. bundle->elf_loader = ELF::Loader::create(bundle->region->vaddr().as_ptr(), bundle->region->size());
  4357. return bundle;
  4358. }
  4359. int Process::sys$get_stack_bounds(FlatPtr* user_stack_base, size_t* user_stack_size)
  4360. {
  4361. if (!validate_write_typed(user_stack_base))
  4362. return -EFAULT;
  4363. if (!validate_write_typed(user_stack_size))
  4364. return -EFAULT;
  4365. FlatPtr stack_pointer = Thread::current->get_register_dump_from_stack().userspace_esp;
  4366. auto* stack_region = MM.region_from_vaddr(*this, VirtualAddress(stack_pointer));
  4367. if (!stack_region) {
  4368. ASSERT_NOT_REACHED();
  4369. return -EINVAL;
  4370. }
  4371. FlatPtr stack_base = stack_region->range().base().get();
  4372. size_t stack_size = stack_region->size();
  4373. copy_to_user(user_stack_base, &stack_base);
  4374. copy_to_user(user_stack_size, &stack_size);
  4375. return 0;
  4376. }
  4377. int Process::sys$ptrace(const Syscall::SC_ptrace_params* user_params)
  4378. {
  4379. REQUIRE_PROMISE(proc);
  4380. Syscall::SC_ptrace_params params;
  4381. if (!validate_read_and_copy_typed(&params, user_params))
  4382. return -EFAULT;
  4383. auto result = Ptrace::handle_syscall(params, *this);
  4384. return result.is_error() ? result.error() : result.value();
  4385. }
  4386. bool Process::has_tracee_thread(int tracer_pid) const
  4387. {
  4388. bool has_tracee = false;
  4389. for_each_thread([&](Thread& t) {
  4390. if (t.tracer() && t.tracer()->tracer_pid() == tracer_pid) {
  4391. has_tracee = true;
  4392. return IterationDecision::Break;
  4393. }
  4394. return IterationDecision::Continue;
  4395. });
  4396. return has_tracee;
  4397. }
  4398. KResultOr<u32> Process::peek_user_data(u32* address)
  4399. {
  4400. if (!MM.validate_user_read(*this, VirtualAddress(address), sizeof(u32))) {
  4401. dbg() << "Invalid address for peek_user_data: " << address;
  4402. return KResult(-EFAULT);
  4403. }
  4404. uint32_t result;
  4405. // This function can be called from the context of another
  4406. // process that called PT_PEEK
  4407. ProcessPagingScope scope(*this);
  4408. copy_from_user(&result, address);
  4409. return result;
  4410. }
  4411. KResult Process::poke_user_data(u32* address, u32 data)
  4412. {
  4413. // We validate for read (rather than write) because PT_POKE can write to readonly pages.
  4414. // So we effectively only care that the poke operation is trying to write to user pages.
  4415. if (!MM.validate_user_read(*this, VirtualAddress(address), sizeof(u32))) {
  4416. dbg() << "Invalid address for poke_user_data: " << address;
  4417. return KResult(-EFAULT);
  4418. }
  4419. ProcessPagingScope scope(*this);
  4420. Range range = { VirtualAddress(address), sizeof(u32) };
  4421. auto* region = region_containing(range);
  4422. ASSERT(region != nullptr);
  4423. if (region->is_shared()) {
  4424. // If the region is shared, we change its vmobject to a PrivateInodeVMObject
  4425. // to prevent the write operation from chaning any shared inode data
  4426. ASSERT(region->vmobject().is_shared_inode());
  4427. region->set_vmobject(PrivateInodeVMObject::create_with_inode(static_cast<SharedInodeVMObject&>(region->vmobject()).inode()));
  4428. region->set_shared(false);
  4429. }
  4430. const bool was_writable = region->is_writable();
  4431. if (!was_writable) //TODO refactor into scopeguard
  4432. {
  4433. region->set_writable(true);
  4434. region->remap();
  4435. }
  4436. copy_to_user(address, &data);
  4437. if (!was_writable) {
  4438. region->set_writable(false);
  4439. region->remap();
  4440. }
  4441. return KResult(KSuccess);
  4442. }
  4443. int Process::sys$sendfd(int sockfd, int fd)
  4444. {
  4445. REQUIRE_PROMISE(sendfd);
  4446. auto socket_description = file_description(sockfd);
  4447. if (!socket_description)
  4448. return -EBADF;
  4449. if (!socket_description->is_socket())
  4450. return -ENOTSOCK;
  4451. auto& socket = *socket_description->socket();
  4452. if (!socket.is_local())
  4453. return -EAFNOSUPPORT;
  4454. if (!socket.is_connected())
  4455. return -ENOTCONN;
  4456. auto passing_descriptor = file_description(fd);
  4457. if (!passing_descriptor)
  4458. return -EBADF;
  4459. auto& local_socket = static_cast<LocalSocket&>(socket);
  4460. return local_socket.sendfd(*socket_description, *passing_descriptor);
  4461. }
  4462. int Process::sys$recvfd(int sockfd)
  4463. {
  4464. REQUIRE_PROMISE(recvfd);
  4465. auto socket_description = file_description(sockfd);
  4466. if (!socket_description)
  4467. return -EBADF;
  4468. if (!socket_description->is_socket())
  4469. return -ENOTSOCK;
  4470. auto& socket = *socket_description->socket();
  4471. if (!socket.is_local())
  4472. return -EAFNOSUPPORT;
  4473. int new_fd = alloc_fd();
  4474. if (new_fd < 0)
  4475. return new_fd;
  4476. auto& local_socket = static_cast<LocalSocket&>(socket);
  4477. auto received_descriptor_or_error = local_socket.recvfd(*socket_description);
  4478. if (received_descriptor_or_error.is_error())
  4479. return received_descriptor_or_error.error();
  4480. m_fds[new_fd].set(*received_descriptor_or_error.value(), 0);
  4481. return new_fd;
  4482. }
  4483. }