archive_read_support_format_tar.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646
  1. /*-
  2. * Copyright (c) 2003-2023 Tim Kientzle
  3. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  4. * Copyright (c) 2016 Martin Matuska
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. #include <stddef.h>
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_acl_private.h" /* For ACL parsing routines. */
  40. #include "archive_entry.h"
  41. #include "archive_entry_locale.h"
  42. #include "archive_private.h"
  43. #include "archive_read_private.h"
  44. #define tar_min(a,b) ((a) < (b) ? (a) : (b))
  45. /*
  46. * Layout of POSIX 'ustar' tar header.
  47. */
  48. struct archive_entry_header_ustar {
  49. char name[100];
  50. char mode[8];
  51. char uid[8];
  52. char gid[8];
  53. char size[12];
  54. char mtime[12];
  55. char checksum[8];
  56. char typeflag[1];
  57. char linkname[100]; /* "old format" header ends here */
  58. char magic[6]; /* For POSIX: "ustar\0" */
  59. char version[2]; /* For POSIX: "00" */
  60. char uname[32];
  61. char gname[32];
  62. char rdevmajor[8];
  63. char rdevminor[8];
  64. char prefix[155];
  65. };
  66. /*
  67. * Structure of GNU tar header
  68. */
  69. struct gnu_sparse {
  70. char offset[12];
  71. char numbytes[12];
  72. };
  73. struct archive_entry_header_gnutar {
  74. char name[100];
  75. char mode[8];
  76. char uid[8];
  77. char gid[8];
  78. char size[12];
  79. char mtime[12];
  80. char checksum[8];
  81. char typeflag[1];
  82. char linkname[100];
  83. char magic[8]; /* "ustar \0" (note blank/blank/null at end) */
  84. char uname[32];
  85. char gname[32];
  86. char rdevmajor[8];
  87. char rdevminor[8];
  88. char atime[12];
  89. char ctime[12];
  90. char offset[12];
  91. char longnames[4];
  92. char unused[1];
  93. struct gnu_sparse sparse[4];
  94. char isextended[1];
  95. char realsize[12];
  96. /*
  97. * Old GNU format doesn't use POSIX 'prefix' field; they use
  98. * the 'L' (longname) entry instead.
  99. */
  100. };
  101. /*
  102. * Data specific to this format.
  103. */
  104. struct sparse_block {
  105. struct sparse_block *next;
  106. int64_t offset;
  107. int64_t remaining;
  108. int hole;
  109. };
  110. struct tar {
  111. struct archive_string entry_pathname;
  112. /* For "GNU.sparse.name" and other similar path extensions. */
  113. struct archive_string entry_pathname_override;
  114. struct archive_string entry_uname;
  115. struct archive_string entry_gname;
  116. struct archive_string entry_linkpath;
  117. struct archive_string line;
  118. int pax_hdrcharset_utf8;
  119. int64_t entry_bytes_remaining;
  120. int64_t entry_offset;
  121. int64_t entry_padding;
  122. int64_t entry_bytes_unconsumed;
  123. int64_t disk_size;
  124. int64_t GNU_sparse_realsize;
  125. int64_t GNU_sparse_size;
  126. int64_t SCHILY_sparse_realsize;
  127. int64_t pax_size;
  128. struct sparse_block *sparse_list;
  129. struct sparse_block *sparse_last;
  130. int64_t sparse_offset;
  131. int64_t sparse_numbytes;
  132. int sparse_gnu_major;
  133. int sparse_gnu_minor;
  134. char sparse_gnu_attributes_seen;
  135. char filetype;
  136. char size_fields; /* Bits defined below */
  137. struct archive_string localname;
  138. struct archive_string_conv *opt_sconv;
  139. struct archive_string_conv *sconv;
  140. struct archive_string_conv *sconv_acl;
  141. struct archive_string_conv *sconv_default;
  142. int init_default_conversion;
  143. int compat_2x;
  144. int process_mac_extensions;
  145. int read_concatenated_archives;
  146. };
  147. /* Track which size fields were present in the headers */
  148. #define TAR_SIZE_PAX_SIZE 1
  149. #define TAR_SIZE_GNU_SPARSE_REALSIZE 2
  150. #define TAR_SIZE_GNU_SPARSE_SIZE 4
  151. #define TAR_SIZE_SCHILY_SPARSE_REALSIZE 8
  152. static int archive_block_is_null(const char *p);
  153. static char *base64_decode(const char *, size_t, size_t *);
  154. static int gnu_add_sparse_entry(struct archive_read *, struct tar *,
  155. int64_t offset, int64_t remaining);
  156. static void gnu_clear_sparse_list(struct tar *);
  157. static int gnu_sparse_old_read(struct archive_read *, struct tar *,
  158. const struct archive_entry_header_gnutar *header, size_t *);
  159. static int gnu_sparse_old_parse(struct archive_read *, struct tar *,
  160. const struct gnu_sparse *sparse, int length);
  161. static int gnu_sparse_01_parse(struct archive_read *, struct tar *,
  162. const char *, size_t);
  163. static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *,
  164. size_t *);
  165. static int header_Solaris_ACL(struct archive_read *, struct tar *,
  166. struct archive_entry *, const void *, size_t *);
  167. static int header_common(struct archive_read *, struct tar *,
  168. struct archive_entry *, const void *);
  169. static int header_old_tar(struct archive_read *, struct tar *,
  170. struct archive_entry *, const void *);
  171. static int header_pax_extension(struct archive_read *, struct tar *,
  172. struct archive_entry *, const void *, size_t *);
  173. static int header_pax_global(struct archive_read *, struct tar *,
  174. struct archive_entry *, const void *h, size_t *);
  175. static int header_gnu_longlink(struct archive_read *, struct tar *,
  176. struct archive_entry *, const void *h, size_t *);
  177. static int header_gnu_longname(struct archive_read *, struct tar *,
  178. struct archive_entry *, const void *h, size_t *);
  179. static int is_mac_metadata_entry(struct archive_entry *entry);
  180. static int read_mac_metadata_blob(struct archive_read *,
  181. struct archive_entry *, size_t *);
  182. static int header_volume(struct archive_read *, struct tar *,
  183. struct archive_entry *, const void *h, size_t *);
  184. static int header_ustar(struct archive_read *, struct tar *,
  185. struct archive_entry *, const void *h);
  186. static int header_gnutar(struct archive_read *, struct tar *,
  187. struct archive_entry *, const void *h, size_t *);
  188. static int archive_read_format_tar_bid(struct archive_read *, int);
  189. static int archive_read_format_tar_options(struct archive_read *,
  190. const char *, const char *);
  191. static int archive_read_format_tar_cleanup(struct archive_read *);
  192. static int archive_read_format_tar_read_data(struct archive_read *a,
  193. const void **buff, size_t *size, int64_t *offset);
  194. static int archive_read_format_tar_skip(struct archive_read *a);
  195. static int archive_read_format_tar_read_header(struct archive_read *,
  196. struct archive_entry *);
  197. static int checksum(struct archive_read *, const void *);
  198. static int pax_attribute(struct archive_read *, struct tar *,
  199. struct archive_entry *, const char *key, size_t key_length,
  200. size_t value_length, size_t *unconsumed);
  201. static int pax_attribute_LIBARCHIVE_xattr(struct archive_entry *,
  202. const char *, size_t, const char *, size_t);
  203. static int pax_attribute_SCHILY_acl(struct archive_read *, struct tar *,
  204. struct archive_entry *, size_t, int);
  205. static int pax_attribute_SUN_holesdata(struct archive_read *, struct tar *,
  206. struct archive_entry *, const char *, size_t);
  207. static void pax_time(const char *, size_t, int64_t *sec, long *nanos);
  208. static ssize_t readline(struct archive_read *, struct tar *, const char **,
  209. ssize_t limit, size_t *);
  210. static int read_body_to_string(struct archive_read *, struct tar *,
  211. struct archive_string *, const void *h, size_t *);
  212. static int read_bytes_to_string(struct archive_read *,
  213. struct archive_string *, size_t, size_t *);
  214. static int64_t tar_atol(const char *, size_t);
  215. static int64_t tar_atol10(const char *, size_t);
  216. static int64_t tar_atol256(const char *, size_t);
  217. static int64_t tar_atol8(const char *, size_t);
  218. static int tar_read_header(struct archive_read *, struct tar *,
  219. struct archive_entry *, size_t *);
  220. static int tohex(int c);
  221. static char *url_decode(const char *, size_t);
  222. static void tar_flush_unconsumed(struct archive_read *, size_t *);
  223. /* Sanity limits: These numbers should be low enough to
  224. * prevent a maliciously-crafted archive from forcing us to
  225. * allocate extreme amounts of memory. But of course, they
  226. * need to be high enough for any correct value. These
  227. * will likely need some adjustment as we get more experience. */
  228. static const size_t guname_limit = 65536; /* Longest uname or gname: 64kiB */
  229. static const size_t pathname_limit = 1048576; /* Longest path name: 1MiB */
  230. static const size_t sparse_map_limit = 8 * 1048576; /* Longest sparse map: 8MiB */
  231. static const size_t xattr_limit = 16 * 1048576; /* Longest xattr: 16MiB */
  232. static const size_t fflags_limit = 512; /* Longest fflags */
  233. static const size_t acl_limit = 131072; /* Longest textual ACL: 128kiB */
  234. static const int64_t entry_limit = 0xfffffffffffffffLL; /* 2^60 bytes = 1 ExbiByte */
  235. int
  236. archive_read_support_format_gnutar(struct archive *a)
  237. {
  238. archive_check_magic(a, ARCHIVE_READ_MAGIC,
  239. ARCHIVE_STATE_NEW, "archive_read_support_format_gnutar");
  240. return (archive_read_support_format_tar(a));
  241. }
  242. int
  243. archive_read_support_format_tar(struct archive *_a)
  244. {
  245. struct archive_read *a = (struct archive_read *)_a;
  246. struct tar *tar;
  247. int r;
  248. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  249. ARCHIVE_STATE_NEW, "archive_read_support_format_tar");
  250. tar = calloc(1, sizeof(*tar));
  251. if (tar == NULL) {
  252. archive_set_error(&a->archive, ENOMEM,
  253. "Can't allocate tar data");
  254. return (ARCHIVE_FATAL);
  255. }
  256. #ifdef HAVE_COPYFILE_H
  257. /* Set this by default on Mac OS. */
  258. tar->process_mac_extensions = 1;
  259. #endif
  260. r = __archive_read_register_format(a, tar, "tar",
  261. archive_read_format_tar_bid,
  262. archive_read_format_tar_options,
  263. archive_read_format_tar_read_header,
  264. archive_read_format_tar_read_data,
  265. archive_read_format_tar_skip,
  266. NULL,
  267. archive_read_format_tar_cleanup,
  268. NULL,
  269. NULL);
  270. if (r != ARCHIVE_OK)
  271. free(tar);
  272. return (ARCHIVE_OK);
  273. }
  274. static int
  275. archive_read_format_tar_cleanup(struct archive_read *a)
  276. {
  277. struct tar *tar;
  278. tar = (struct tar *)(a->format->data);
  279. gnu_clear_sparse_list(tar);
  280. archive_string_free(&tar->entry_pathname);
  281. archive_string_free(&tar->entry_pathname_override);
  282. archive_string_free(&tar->entry_uname);
  283. archive_string_free(&tar->entry_gname);
  284. archive_string_free(&tar->entry_linkpath);
  285. archive_string_free(&tar->line);
  286. archive_string_free(&tar->localname);
  287. free(tar);
  288. (a->format->data) = NULL;
  289. return (ARCHIVE_OK);
  290. }
  291. /*
  292. * Validate number field
  293. *
  294. * This has to be pretty lenient in order to accommodate the enormous
  295. * variety of tar writers in the world:
  296. * = POSIX (IEEE Std 1003.1-1988) ustar requires octal values with leading
  297. * zeros and allows fields to be terminated with space or null characters
  298. * = Many writers use different termination (in particular, libarchive
  299. * omits terminator bytes to squeeze one or two more digits)
  300. * = Many writers pad with space and omit leading zeros
  301. * = GNU tar and star write base-256 values if numbers are too
  302. * big to be represented in octal
  303. *
  304. * Examples of specific tar headers that we should support:
  305. * = Perl Archive::Tar terminates uid, gid, devminor and devmajor with two
  306. * null bytes, pads size with spaces and other numeric fields with zeroes
  307. * = plexus-archiver prior to 2.6.3 (before switching to commons-compress)
  308. * may have uid and gid fields filled with spaces without any octal digits
  309. * at all and pads all numeric fields with spaces
  310. *
  311. * This should tolerate all variants in use. It will reject a field
  312. * where the writer just left garbage after a trailing NUL.
  313. */
  314. static int
  315. validate_number_field(const char* p_field, size_t i_size)
  316. {
  317. unsigned char marker = (unsigned char)p_field[0];
  318. if (marker == 128 || marker == 255 || marker == 0) {
  319. /* Base-256 marker, there's nothing we can check. */
  320. return 1;
  321. } else {
  322. /* Must be octal */
  323. size_t i = 0;
  324. /* Skip any leading spaces */
  325. while (i < i_size && p_field[i] == ' ') {
  326. ++i;
  327. }
  328. /* Skip octal digits. */
  329. while (i < i_size && p_field[i] >= '0' && p_field[i] <= '7') {
  330. ++i;
  331. }
  332. /* Any remaining characters must be space or NUL padding. */
  333. while (i < i_size) {
  334. if (p_field[i] != ' ' && p_field[i] != 0) {
  335. return 0;
  336. }
  337. ++i;
  338. }
  339. return 1;
  340. }
  341. }
  342. static int
  343. archive_read_format_tar_bid(struct archive_read *a, int best_bid)
  344. {
  345. int bid;
  346. const char *h;
  347. const struct archive_entry_header_ustar *header;
  348. (void)best_bid; /* UNUSED */
  349. bid = 0;
  350. /* Now let's look at the actual header and see if it matches. */
  351. h = __archive_read_ahead(a, 512, NULL);
  352. if (h == NULL)
  353. return (-1);
  354. /* If it's an end-of-archive mark, we can handle it. */
  355. if (h[0] == 0 && archive_block_is_null(h)) {
  356. /*
  357. * Usually, I bid the number of bits verified, but
  358. * in this case, 4096 seems excessive so I picked 10 as
  359. * an arbitrary but reasonable-seeming value.
  360. */
  361. return (10);
  362. }
  363. /* If it's not an end-of-archive mark, it must have a valid checksum.*/
  364. if (!checksum(a, h))
  365. return (0);
  366. bid += 48; /* Checksum is usually 6 octal digits. */
  367. header = (const struct archive_entry_header_ustar *)h;
  368. /* Recognize POSIX formats. */
  369. if ((memcmp(header->magic, "ustar\0", 6) == 0)
  370. && (memcmp(header->version, "00", 2) == 0))
  371. bid += 56;
  372. /* Recognize GNU tar format. */
  373. if ((memcmp(header->magic, "ustar ", 6) == 0)
  374. && (memcmp(header->version, " \0", 2) == 0))
  375. bid += 56;
  376. /* Type flag must be null, digit or A-Z, a-z. */
  377. if (header->typeflag[0] != 0 &&
  378. !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') &&
  379. !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') &&
  380. !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') )
  381. return (0);
  382. bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */
  383. /*
  384. * Check format of mode/uid/gid/mtime/size/rdevmajor/rdevminor fields.
  385. */
  386. if (validate_number_field(header->mode, sizeof(header->mode)) == 0
  387. || validate_number_field(header->uid, sizeof(header->uid)) == 0
  388. || validate_number_field(header->gid, sizeof(header->gid)) == 0
  389. || validate_number_field(header->mtime, sizeof(header->mtime)) == 0
  390. || validate_number_field(header->size, sizeof(header->size)) == 0
  391. || validate_number_field(header->rdevmajor, sizeof(header->rdevmajor)) == 0
  392. || validate_number_field(header->rdevminor, sizeof(header->rdevminor)) == 0) {
  393. bid = 0;
  394. }
  395. return (bid);
  396. }
  397. static int
  398. archive_read_format_tar_options(struct archive_read *a,
  399. const char *key, const char *val)
  400. {
  401. struct tar *tar;
  402. int ret = ARCHIVE_FAILED;
  403. tar = (struct tar *)(a->format->data);
  404. if (strcmp(key, "compat-2x") == 0) {
  405. /* Handle UTF-8 filenames as libarchive 2.x */
  406. tar->compat_2x = (val != NULL && val[0] != 0);
  407. tar->init_default_conversion = tar->compat_2x;
  408. return (ARCHIVE_OK);
  409. } else if (strcmp(key, "hdrcharset") == 0) {
  410. if (val == NULL || val[0] == 0)
  411. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  412. "tar: hdrcharset option needs a character-set name");
  413. else {
  414. tar->opt_sconv =
  415. archive_string_conversion_from_charset(
  416. &a->archive, val, 0);
  417. if (tar->opt_sconv != NULL)
  418. ret = ARCHIVE_OK;
  419. else
  420. ret = ARCHIVE_FATAL;
  421. }
  422. return (ret);
  423. } else if (strcmp(key, "mac-ext") == 0) {
  424. tar->process_mac_extensions = (val != NULL && val[0] != 0);
  425. return (ARCHIVE_OK);
  426. } else if (strcmp(key, "read_concatenated_archives") == 0) {
  427. tar->read_concatenated_archives = (val != NULL && val[0] != 0);
  428. return (ARCHIVE_OK);
  429. }
  430. /* Note: The "warn" return is just to inform the options
  431. * supervisor that we didn't handle it. It will generate
  432. * a suitable error if no one used this option. */
  433. return (ARCHIVE_WARN);
  434. }
  435. /* utility function- this exists to centralize the logic of tracking
  436. * how much unconsumed data we have floating around, and to consume
  437. * anything outstanding since we're going to do read_aheads
  438. */
  439. static void
  440. tar_flush_unconsumed(struct archive_read *a, size_t *unconsumed)
  441. {
  442. if (*unconsumed) {
  443. /*
  444. void *data = (void *)__archive_read_ahead(a, *unconsumed, NULL);
  445. * this block of code is to poison claimed unconsumed space, ensuring
  446. * things break if it is in use still.
  447. * currently it WILL break things, so enable it only for debugging this issue
  448. if (data) {
  449. memset(data, 0xff, *unconsumed);
  450. }
  451. */
  452. __archive_read_consume(a, *unconsumed);
  453. *unconsumed = 0;
  454. }
  455. }
  456. /*
  457. * The function invoked by archive_read_next_header(). This
  458. * just sets up a few things and then calls the internal
  459. * tar_read_header() function below.
  460. */
  461. static int
  462. archive_read_format_tar_read_header(struct archive_read *a,
  463. struct archive_entry *entry)
  464. {
  465. /*
  466. * When converting tar archives to cpio archives, it is
  467. * essential that each distinct file have a distinct inode
  468. * number. To simplify this, we keep a static count here to
  469. * assign fake dev/inode numbers to each tar entry. Note that
  470. * pax format archives may overwrite this with something more
  471. * useful.
  472. *
  473. * Ideally, we would track every file read from the archive so
  474. * that we could assign the same dev/ino pair to hardlinks,
  475. * but the memory required to store a complete lookup table is
  476. * probably not worthwhile just to support the relatively
  477. * obscure tar->cpio conversion case.
  478. */
  479. /* TODO: Move this into `struct tar` to avoid conflicts
  480. * when reading multiple archives */
  481. static int default_inode;
  482. static int default_dev;
  483. struct tar *tar;
  484. const char *p;
  485. const wchar_t *wp;
  486. int r;
  487. size_t l, unconsumed = 0;
  488. /* Assign default device/inode values. */
  489. archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */
  490. archive_entry_set_ino(entry, ++default_inode); /* Don't use zero. */
  491. /* Limit generated st_ino number to 16 bits. */
  492. if (default_inode >= 0xffff) {
  493. ++default_dev;
  494. default_inode = 0;
  495. }
  496. tar = (struct tar *)(a->format->data);
  497. tar->entry_offset = 0;
  498. gnu_clear_sparse_list(tar);
  499. tar->size_fields = 0; /* We don't have any size info yet */
  500. /* Setup default string conversion. */
  501. tar->sconv = tar->opt_sconv;
  502. if (tar->sconv == NULL) {
  503. if (!tar->init_default_conversion) {
  504. tar->sconv_default =
  505. archive_string_default_conversion_for_read(&(a->archive));
  506. tar->init_default_conversion = 1;
  507. }
  508. tar->sconv = tar->sconv_default;
  509. }
  510. r = tar_read_header(a, tar, entry, &unconsumed);
  511. tar_flush_unconsumed(a, &unconsumed);
  512. /*
  513. * "non-sparse" files are really just sparse files with
  514. * a single block.
  515. */
  516. if (tar->sparse_list == NULL) {
  517. if (gnu_add_sparse_entry(a, tar, 0, tar->entry_bytes_remaining)
  518. != ARCHIVE_OK)
  519. return (ARCHIVE_FATAL);
  520. } else {
  521. struct sparse_block *sb;
  522. for (sb = tar->sparse_list; sb != NULL; sb = sb->next) {
  523. if (!sb->hole)
  524. archive_entry_sparse_add_entry(entry,
  525. sb->offset, sb->remaining);
  526. }
  527. }
  528. if (r == ARCHIVE_OK && archive_entry_filetype(entry) == AE_IFREG) {
  529. /*
  530. * "Regular" entry with trailing '/' is really
  531. * directory: This is needed for certain old tar
  532. * variants and even for some broken newer ones.
  533. */
  534. if ((wp = archive_entry_pathname_w(entry)) != NULL) {
  535. l = wcslen(wp);
  536. if (l > 0 && wp[l - 1] == L'/') {
  537. archive_entry_set_filetype(entry, AE_IFDIR);
  538. tar->entry_bytes_remaining = 0;
  539. tar->entry_padding = 0;
  540. }
  541. } else if ((p = archive_entry_pathname(entry)) != NULL) {
  542. l = strlen(p);
  543. if (l > 0 && p[l - 1] == '/') {
  544. archive_entry_set_filetype(entry, AE_IFDIR);
  545. tar->entry_bytes_remaining = 0;
  546. tar->entry_padding = 0;
  547. }
  548. }
  549. }
  550. return (r);
  551. }
  552. static int
  553. archive_read_format_tar_read_data(struct archive_read *a,
  554. const void **buff, size_t *size, int64_t *offset)
  555. {
  556. ssize_t bytes_read;
  557. struct tar *tar;
  558. struct sparse_block *p;
  559. tar = (struct tar *)(a->format->data);
  560. for (;;) {
  561. /* Remove exhausted entries from sparse list. */
  562. while (tar->sparse_list != NULL &&
  563. tar->sparse_list->remaining == 0) {
  564. p = tar->sparse_list;
  565. tar->sparse_list = p->next;
  566. free(p);
  567. }
  568. if (tar->entry_bytes_unconsumed) {
  569. __archive_read_consume(a, tar->entry_bytes_unconsumed);
  570. tar->entry_bytes_unconsumed = 0;
  571. }
  572. /* If we're at end of file, return EOF. */
  573. if (tar->sparse_list == NULL ||
  574. tar->entry_bytes_remaining == 0) {
  575. if (__archive_read_consume(a, tar->entry_padding) < 0)
  576. return (ARCHIVE_FATAL);
  577. tar->entry_padding = 0;
  578. *buff = NULL;
  579. *size = 0;
  580. *offset = tar->disk_size;
  581. return (ARCHIVE_EOF);
  582. }
  583. *buff = __archive_read_ahead(a, 1, &bytes_read);
  584. if (*buff == NULL) {
  585. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  586. "Truncated tar archive"
  587. " detected while reading data");
  588. return (ARCHIVE_FATAL);
  589. }
  590. if (bytes_read > tar->entry_bytes_remaining)
  591. bytes_read = (ssize_t)tar->entry_bytes_remaining;
  592. /* Don't read more than is available in the
  593. * current sparse block. */
  594. if (tar->sparse_list->remaining < bytes_read)
  595. bytes_read = (ssize_t)tar->sparse_list->remaining;
  596. *size = bytes_read;
  597. *offset = tar->sparse_list->offset;
  598. tar->sparse_list->remaining -= bytes_read;
  599. tar->sparse_list->offset += bytes_read;
  600. tar->entry_bytes_remaining -= bytes_read;
  601. tar->entry_bytes_unconsumed = bytes_read;
  602. if (!tar->sparse_list->hole)
  603. return (ARCHIVE_OK);
  604. /* Current is hole data and skip this. */
  605. }
  606. }
  607. static int
  608. archive_read_format_tar_skip(struct archive_read *a)
  609. {
  610. int64_t bytes_skipped;
  611. int64_t request;
  612. struct sparse_block *p;
  613. struct tar* tar;
  614. tar = (struct tar *)(a->format->data);
  615. /* Do not consume the hole of a sparse file. */
  616. request = 0;
  617. for (p = tar->sparse_list; p != NULL; p = p->next) {
  618. if (!p->hole) {
  619. if (p->remaining >= INT64_MAX - request) {
  620. return ARCHIVE_FATAL;
  621. }
  622. request += p->remaining;
  623. }
  624. }
  625. if (request > tar->entry_bytes_remaining)
  626. request = tar->entry_bytes_remaining;
  627. request += tar->entry_padding + tar->entry_bytes_unconsumed;
  628. bytes_skipped = __archive_read_consume(a, request);
  629. if (bytes_skipped < 0)
  630. return (ARCHIVE_FATAL);
  631. tar->entry_bytes_remaining = 0;
  632. tar->entry_bytes_unconsumed = 0;
  633. tar->entry_padding = 0;
  634. /* Free the sparse list. */
  635. gnu_clear_sparse_list(tar);
  636. return (ARCHIVE_OK);
  637. }
  638. /*
  639. * This function reads and interprets all of the headers associated
  640. * with a single entry.
  641. */
  642. static int
  643. tar_read_header(struct archive_read *a, struct tar *tar,
  644. struct archive_entry *entry, size_t *unconsumed)
  645. {
  646. ssize_t bytes;
  647. int err = ARCHIVE_OK, err2;
  648. int eof_fatal = 0; /* EOF is okay at some points... */
  649. const char *h;
  650. const struct archive_entry_header_ustar *header;
  651. const struct archive_entry_header_gnutar *gnuheader;
  652. /* Bitmask of what header types we've seen. */
  653. int32_t seen_headers = 0;
  654. static const int32_t seen_A_header = 1;
  655. static const int32_t seen_g_header = 2;
  656. static const int32_t seen_K_header = 4;
  657. static const int32_t seen_L_header = 8;
  658. static const int32_t seen_V_header = 16;
  659. static const int32_t seen_x_header = 32; /* Also X */
  660. static const int32_t seen_mac_metadata = 512;
  661. tar->pax_hdrcharset_utf8 = 1;
  662. tar->sparse_gnu_attributes_seen = 0;
  663. archive_string_empty(&(tar->entry_gname));
  664. archive_string_empty(&(tar->entry_pathname));
  665. archive_string_empty(&(tar->entry_pathname_override));
  666. archive_string_empty(&(tar->entry_uname));
  667. archive_string_empty(&tar->entry_linkpath);
  668. /* Ensure format is set. */
  669. if (a->archive.archive_format_name == NULL) {
  670. a->archive.archive_format = ARCHIVE_FORMAT_TAR;
  671. a->archive.archive_format_name = "tar";
  672. }
  673. /*
  674. * TODO: Write global/default pax options into
  675. * 'entry' struct here before overwriting with
  676. * file-specific options.
  677. */
  678. /* Loop over all the headers needed for the next entry */
  679. for (;;) {
  680. /* Find the next valid header record. */
  681. while (1) {
  682. tar_flush_unconsumed(a, unconsumed);
  683. /* Read 512-byte header record */
  684. h = __archive_read_ahead(a, 512, &bytes);
  685. if (bytes == 0) { /* EOF at a block boundary. */
  686. if (eof_fatal) {
  687. /* We've read a special header already;
  688. * if there's no regular header, then this is
  689. * a premature EOF. */
  690. archive_set_error(&a->archive, EINVAL,
  691. "Damaged tar archive (end-of-archive within a sequence of headers)");
  692. return (ARCHIVE_FATAL);
  693. } else {
  694. return (ARCHIVE_EOF);
  695. }
  696. }
  697. if (h == NULL) { /* Short block at EOF; this is bad. */
  698. archive_set_error(&a->archive,
  699. ARCHIVE_ERRNO_FILE_FORMAT,
  700. "Truncated tar archive"
  701. " detected while reading next header");
  702. return (ARCHIVE_FATAL);
  703. }
  704. *unconsumed += 512;
  705. if (h[0] == 0 && archive_block_is_null(h)) {
  706. /* We found a NULL block which indicates end-of-archive */
  707. if (tar->read_concatenated_archives) {
  708. /* We're ignoring NULL blocks, so keep going. */
  709. continue;
  710. }
  711. /* Try to consume a second all-null record, as well. */
  712. /* If we can't, that's okay. */
  713. tar_flush_unconsumed(a, unconsumed);
  714. h = __archive_read_ahead(a, 512, NULL);
  715. if (h != NULL && h[0] == 0 && archive_block_is_null(h))
  716. __archive_read_consume(a, 512);
  717. archive_clear_error(&a->archive);
  718. return (ARCHIVE_EOF);
  719. }
  720. /* This is NOT a null block, so it must be a valid header. */
  721. if (!checksum(a, h)) {
  722. tar_flush_unconsumed(a, unconsumed);
  723. archive_set_error(&a->archive, EINVAL,
  724. "Damaged tar archive (bad header checksum)");
  725. /* If we've read some critical information (pax headers, etc)
  726. * and _then_ see a bad header, we can't really recover. */
  727. if (eof_fatal) {
  728. return (ARCHIVE_FATAL);
  729. } else {
  730. return (ARCHIVE_RETRY);
  731. }
  732. }
  733. break;
  734. }
  735. /* Determine the format variant. */
  736. header = (const struct archive_entry_header_ustar *)h;
  737. switch(header->typeflag[0]) {
  738. case 'A': /* Solaris tar ACL */
  739. if (seen_headers & seen_A_header) {
  740. return (ARCHIVE_FATAL);
  741. }
  742. seen_headers |= seen_A_header;
  743. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  744. a->archive.archive_format_name = "Solaris tar";
  745. err2 = header_Solaris_ACL(a, tar, entry, h, unconsumed);
  746. break;
  747. case 'g': /* POSIX-standard 'g' header. */
  748. if (seen_headers & seen_g_header) {
  749. return (ARCHIVE_FATAL);
  750. }
  751. seen_headers |= seen_g_header;
  752. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  753. a->archive.archive_format_name = "POSIX pax interchange format";
  754. err2 = header_pax_global(a, tar, entry, h, unconsumed);
  755. break;
  756. case 'K': /* Long link name (GNU tar, others) */
  757. if (seen_headers & seen_K_header) {
  758. return (ARCHIVE_FATAL);
  759. }
  760. seen_headers |= seen_K_header;
  761. err2 = header_gnu_longlink(a, tar, entry, h, unconsumed);
  762. break;
  763. case 'L': /* Long filename (GNU tar, others) */
  764. if (seen_headers & seen_L_header) {
  765. return (ARCHIVE_FATAL);
  766. }
  767. seen_headers |= seen_L_header;
  768. err2 = header_gnu_longname(a, tar, entry, h, unconsumed);
  769. break;
  770. case 'V': /* GNU volume header */
  771. if (seen_headers & seen_V_header) {
  772. return (ARCHIVE_FATAL);
  773. }
  774. seen_headers |= seen_V_header;
  775. err2 = header_volume(a, tar, entry, h, unconsumed);
  776. break;
  777. case 'X': /* Used by SUN tar; same as 'x'. */
  778. if (seen_headers & seen_x_header) {
  779. return (ARCHIVE_FATAL);
  780. }
  781. seen_headers |= seen_x_header;
  782. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  783. a->archive.archive_format_name =
  784. "POSIX pax interchange format (Sun variant)";
  785. err2 = header_pax_extension(a, tar, entry, h, unconsumed);
  786. break;
  787. case 'x': /* POSIX-standard 'x' header. */
  788. if (seen_headers & seen_x_header) {
  789. return (ARCHIVE_FATAL);
  790. }
  791. seen_headers |= seen_x_header;
  792. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  793. a->archive.archive_format_name = "POSIX pax interchange format";
  794. err2 = header_pax_extension(a, tar, entry, h, unconsumed);
  795. break;
  796. default: /* Regular header: Legacy tar, GNU tar, or ustar */
  797. gnuheader = (const struct archive_entry_header_gnutar *)h;
  798. if (memcmp(gnuheader->magic, "ustar \0", 8) == 0) {
  799. a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
  800. a->archive.archive_format_name = "GNU tar format";
  801. err2 = header_gnutar(a, tar, entry, h, unconsumed);
  802. } else if (memcmp(header->magic, "ustar", 5) == 0) {
  803. if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
  804. a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
  805. a->archive.archive_format_name = "POSIX ustar format";
  806. }
  807. err2 = header_ustar(a, tar, entry, h);
  808. } else {
  809. a->archive.archive_format = ARCHIVE_FORMAT_TAR;
  810. a->archive.archive_format_name = "tar (non-POSIX)";
  811. err2 = header_old_tar(a, tar, entry, h);
  812. }
  813. err = err_combine(err, err2);
  814. /* We return warnings or success as-is. Anything else is fatal. */
  815. if (err < ARCHIVE_WARN) {
  816. return (ARCHIVE_FATAL);
  817. }
  818. /* Filename of the form `._filename` is an AppleDouble
  819. * extension entry. The body is the macOS metadata blob;
  820. * this is followed by another entry with the actual
  821. * regular file data.
  822. * This design has two drawbacks:
  823. * = it's brittle; you might just have a file with such a name
  824. * = it duplicates any long pathname extensions
  825. *
  826. * TODO: This probably shouldn't be here at all. Consider
  827. * just returning the contents as a regular entry here and
  828. * then dealing with it when we write data to disk.
  829. */
  830. if (tar->process_mac_extensions
  831. && ((seen_headers & seen_mac_metadata) == 0)
  832. && is_mac_metadata_entry(entry)) {
  833. err2 = read_mac_metadata_blob(a, entry, unconsumed);
  834. if (err2 < ARCHIVE_WARN) {
  835. return (ARCHIVE_FATAL);
  836. }
  837. err = err_combine(err, err2);
  838. /* Note: Other headers can appear again. */
  839. seen_headers = seen_mac_metadata;
  840. break;
  841. }
  842. /* Reconcile GNU sparse attributes */
  843. if (tar->sparse_gnu_attributes_seen) {
  844. /* Only 'S' (GNU sparse) and ustar '0' regular files can be sparse */
  845. if (tar->filetype != 'S' && tar->filetype != '0') {
  846. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  847. "Non-regular file cannot be sparse");
  848. return (ARCHIVE_WARN);
  849. } else if (tar->sparse_gnu_major == 0 &&
  850. tar->sparse_gnu_minor == 0) {
  851. /* Sparse map already parsed from 'x' header */
  852. } else if (tar->sparse_gnu_major == 0 &&
  853. tar->sparse_gnu_minor == 1) {
  854. /* Sparse map already parsed from 'x' header */
  855. } else if (tar->sparse_gnu_major == 1 &&
  856. tar->sparse_gnu_minor == 0) {
  857. /* Sparse map is prepended to file contents */
  858. ssize_t bytes_read;
  859. bytes_read = gnu_sparse_10_read(a, tar, unconsumed);
  860. if (bytes_read < 0)
  861. return ((int)bytes_read);
  862. tar->entry_bytes_remaining -= bytes_read;
  863. } else {
  864. archive_set_error(&a->archive,
  865. ARCHIVE_ERRNO_MISC,
  866. "Unrecognized GNU sparse file format");
  867. return (ARCHIVE_WARN);
  868. }
  869. }
  870. return (err);
  871. }
  872. /* We're between headers ... */
  873. err = err_combine(err, err2);
  874. if (err == ARCHIVE_FATAL)
  875. return (err);
  876. /* The GNU volume header and the pax `g` global header
  877. * are both allowed to be the only header in an
  878. * archive. If we've seen any other header, a
  879. * following EOF is fatal. */
  880. if ((seen_headers & ~seen_V_header & ~seen_g_header) != 0) {
  881. eof_fatal = 1;
  882. }
  883. }
  884. }
  885. /*
  886. * Return true if block checksum is correct.
  887. */
  888. static int
  889. checksum(struct archive_read *a, const void *h)
  890. {
  891. const unsigned char *bytes;
  892. const struct archive_entry_header_ustar *header;
  893. int check, sum;
  894. size_t i;
  895. (void)a; /* UNUSED */
  896. bytes = (const unsigned char *)h;
  897. header = (const struct archive_entry_header_ustar *)h;
  898. /* Checksum field must hold an octal number */
  899. for (i = 0; i < sizeof(header->checksum); ++i) {
  900. char c = header->checksum[i];
  901. if (c != ' ' && c != '\0' && (c < '0' || c > '7'))
  902. return 0;
  903. }
  904. /*
  905. * Test the checksum. Note that POSIX specifies _unsigned_
  906. * bytes for this calculation.
  907. */
  908. sum = (int)tar_atol(header->checksum, sizeof(header->checksum));
  909. check = 0;
  910. for (i = 0; i < 148; i++)
  911. check += (unsigned char)bytes[i];
  912. for (; i < 156; i++)
  913. check += 32;
  914. for (; i < 512; i++)
  915. check += (unsigned char)bytes[i];
  916. if (sum == check)
  917. return (1);
  918. /*
  919. * Repeat test with _signed_ bytes, just in case this archive
  920. * was created by an old BSD, Solaris, or HP-UX tar with a
  921. * broken checksum calculation.
  922. */
  923. check = 0;
  924. for (i = 0; i < 148; i++)
  925. check += (signed char)bytes[i];
  926. for (; i < 156; i++)
  927. check += 32;
  928. for (; i < 512; i++)
  929. check += (signed char)bytes[i];
  930. if (sum == check)
  931. return (1);
  932. #if DONT_FAIL_ON_CRC_ERROR
  933. /* Speed up fuzzing by pretending the checksum is always right. */
  934. return (1);
  935. #else
  936. return (0);
  937. #endif
  938. }
  939. /*
  940. * Return true if this block contains only nulls.
  941. */
  942. static int
  943. archive_block_is_null(const char *p)
  944. {
  945. unsigned i;
  946. for (i = 0; i < 512; i++)
  947. if (*p++)
  948. return (0);
  949. return (1);
  950. }
  951. /*
  952. * Interpret 'A' Solaris ACL header
  953. */
  954. static int
  955. header_Solaris_ACL(struct archive_read *a, struct tar *tar,
  956. struct archive_entry *entry, const void *h, size_t *unconsumed)
  957. {
  958. const struct archive_entry_header_ustar *header;
  959. struct archive_string acl_text;
  960. size_t size;
  961. int err, acl_type;
  962. uint64_t type;
  963. char *acl, *p;
  964. header = (const struct archive_entry_header_ustar *)h;
  965. size = (size_t)tar_atol(header->size, sizeof(header->size));
  966. archive_string_init(&acl_text);
  967. err = read_body_to_string(a, tar, &acl_text, h, unconsumed);
  968. if (err != ARCHIVE_OK) {
  969. archive_string_free(&acl_text);
  970. return (err);
  971. }
  972. /* TODO: Examine the first characters to see if this
  973. * is an AIX ACL descriptor. We'll likely never support
  974. * them, but it would be polite to recognize and warn when
  975. * we do see them. */
  976. /* Leading octal number indicates ACL type and number of entries. */
  977. p = acl = acl_text.s;
  978. type = 0;
  979. while (*p != '\0' && p < acl + size) {
  980. if (*p < '0' || *p > '7') {
  981. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  982. "Malformed Solaris ACL attribute (invalid digit)");
  983. archive_string_free(&acl_text);
  984. return(ARCHIVE_WARN);
  985. }
  986. type <<= 3;
  987. type += *p - '0';
  988. if (type > 077777777) {
  989. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  990. "Malformed Solaris ACL attribute (count too large)");
  991. archive_string_free(&acl_text);
  992. return (ARCHIVE_WARN);
  993. }
  994. p++;
  995. }
  996. switch (type & ~0777777) {
  997. case 01000000:
  998. /* POSIX.1e ACL */
  999. acl_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
  1000. break;
  1001. case 03000000:
  1002. /* NFSv4 ACL */
  1003. acl_type = ARCHIVE_ENTRY_ACL_TYPE_NFS4;
  1004. break;
  1005. default:
  1006. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1007. "Malformed Solaris ACL attribute (unsupported type %"
  1008. PRIo64 ")", type);
  1009. archive_string_free(&acl_text);
  1010. return (ARCHIVE_WARN);
  1011. }
  1012. p++;
  1013. if (p >= acl + size) {
  1014. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1015. "Malformed Solaris ACL attribute (body overflow)");
  1016. archive_string_free(&acl_text);
  1017. return(ARCHIVE_WARN);
  1018. }
  1019. /* ACL text is null-terminated; find the end. */
  1020. size -= (p - acl);
  1021. acl = p;
  1022. while (*p != '\0' && p < acl + size)
  1023. p++;
  1024. if (tar->sconv_acl == NULL) {
  1025. tar->sconv_acl = archive_string_conversion_from_charset(
  1026. &(a->archive), "UTF-8", 1);
  1027. if (tar->sconv_acl == NULL) {
  1028. archive_string_free(&acl_text);
  1029. return (ARCHIVE_FATAL);
  1030. }
  1031. }
  1032. archive_strncpy(&(tar->localname), acl, p - acl);
  1033. err = archive_acl_from_text_l(archive_entry_acl(entry),
  1034. tar->localname.s, acl_type, tar->sconv_acl);
  1035. /* Workaround: Force perm_is_set() to be correct */
  1036. /* If this bit were stored in the ACL, this wouldn't be needed */
  1037. archive_entry_set_perm(entry, archive_entry_perm(entry));
  1038. if (err != ARCHIVE_OK) {
  1039. if (errno == ENOMEM) {
  1040. archive_set_error(&a->archive, ENOMEM,
  1041. "Can't allocate memory for ACL");
  1042. } else
  1043. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1044. "Malformed Solaris ACL attribute (unparsable)");
  1045. }
  1046. archive_string_free(&acl_text);
  1047. return (err);
  1048. }
  1049. /*
  1050. * Interpret 'K' long linkname header.
  1051. */
  1052. static int
  1053. header_gnu_longlink(struct archive_read *a, struct tar *tar,
  1054. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1055. {
  1056. int err;
  1057. struct archive_string linkpath;
  1058. archive_string_init(&linkpath);
  1059. err = read_body_to_string(a, tar, &linkpath, h, unconsumed);
  1060. if (err == ARCHIVE_OK) {
  1061. archive_entry_set_link(entry, linkpath.s);
  1062. }
  1063. archive_string_free(&linkpath);
  1064. return (err);
  1065. }
  1066. static int
  1067. set_conversion_failed_error(struct archive_read *a,
  1068. struct archive_string_conv *sconv, const char *name)
  1069. {
  1070. if (errno == ENOMEM) {
  1071. archive_set_error(&a->archive, ENOMEM,
  1072. "Can't allocate memory for %s", name);
  1073. return (ARCHIVE_FATAL);
  1074. }
  1075. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1076. "%s can't be converted from %s to current locale.",
  1077. name, archive_string_conversion_charset_name(sconv));
  1078. return (ARCHIVE_WARN);
  1079. }
  1080. /*
  1081. * Interpret 'L' long filename header.
  1082. */
  1083. static int
  1084. header_gnu_longname(struct archive_read *a, struct tar *tar,
  1085. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1086. {
  1087. int err;
  1088. struct archive_string longname;
  1089. archive_string_init(&longname);
  1090. err = read_body_to_string(a, tar, &longname, h, unconsumed);
  1091. if (err == ARCHIVE_OK) {
  1092. if (archive_entry_copy_pathname_l(entry, longname.s,
  1093. archive_strlen(&longname), tar->sconv) != 0)
  1094. err = set_conversion_failed_error(a, tar->sconv, "Pathname");
  1095. }
  1096. archive_string_free(&longname);
  1097. return (err);
  1098. }
  1099. /*
  1100. * Interpret 'V' GNU tar volume header.
  1101. */
  1102. static int
  1103. header_volume(struct archive_read *a, struct tar *tar,
  1104. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1105. {
  1106. const struct archive_entry_header_ustar *header;
  1107. int64_t size, to_consume;
  1108. (void)a; /* UNUSED */
  1109. (void)tar; /* UNUSED */
  1110. (void)entry; /* UNUSED */
  1111. header = (const struct archive_entry_header_ustar *)h;
  1112. size = tar_atol(header->size, sizeof(header->size));
  1113. if (size > (int64_t)pathname_limit) {
  1114. return (ARCHIVE_FATAL);
  1115. }
  1116. to_consume = ((size + 511) & ~511);
  1117. *unconsumed += to_consume;
  1118. return (ARCHIVE_OK);
  1119. }
  1120. /*
  1121. * Read the next `size` bytes into the provided string.
  1122. * Null-terminate the string.
  1123. */
  1124. static int
  1125. read_bytes_to_string(struct archive_read *a,
  1126. struct archive_string *as, size_t size,
  1127. size_t *unconsumed) {
  1128. const void *src;
  1129. /* Fail if we can't make our buffer big enough. */
  1130. if (archive_string_ensure(as, (size_t)size+1) == NULL) {
  1131. archive_set_error(&a->archive, ENOMEM,
  1132. "No memory");
  1133. return (ARCHIVE_FATAL);
  1134. }
  1135. tar_flush_unconsumed(a, unconsumed);
  1136. /* Read the body into the string. */
  1137. src = __archive_read_ahead(a, size, NULL);
  1138. if (src == NULL) {
  1139. archive_set_error(&a->archive, EINVAL,
  1140. "Truncated archive"
  1141. " detected while reading metadata");
  1142. *unconsumed = 0;
  1143. return (ARCHIVE_FATAL);
  1144. }
  1145. memcpy(as->s, src, (size_t)size);
  1146. as->s[size] = '\0';
  1147. as->length = (size_t)size;
  1148. *unconsumed += size;
  1149. return (ARCHIVE_OK);
  1150. }
  1151. /*
  1152. * Read body of an archive entry into an archive_string object.
  1153. */
  1154. static int
  1155. read_body_to_string(struct archive_read *a, struct tar *tar,
  1156. struct archive_string *as, const void *h, size_t *unconsumed)
  1157. {
  1158. int64_t size;
  1159. const struct archive_entry_header_ustar *header;
  1160. int r;
  1161. (void)tar; /* UNUSED */
  1162. header = (const struct archive_entry_header_ustar *)h;
  1163. size = tar_atol(header->size, sizeof(header->size));
  1164. if (size > entry_limit) {
  1165. return (ARCHIVE_FATAL);
  1166. }
  1167. if ((size > (int64_t)pathname_limit) || (size < 0)) {
  1168. archive_string_empty(as);
  1169. int64_t to_consume = ((size + 511) & ~511);
  1170. if (to_consume != __archive_read_consume(a, to_consume)) {
  1171. return (ARCHIVE_FATAL);
  1172. }
  1173. archive_set_error(&a->archive, EINVAL,
  1174. "Special header too large: %d > 1MiB",
  1175. (int)size);
  1176. return (ARCHIVE_WARN);
  1177. }
  1178. r = read_bytes_to_string(a, as, size, unconsumed);
  1179. *unconsumed += 0x1ff & (-size);
  1180. return(r);
  1181. }
  1182. /*
  1183. * Parse out common header elements.
  1184. *
  1185. * This would be the same as header_old_tar, except that the
  1186. * filename is handled slightly differently for old and POSIX
  1187. * entries (POSIX entries support a 'prefix'). This factoring
  1188. * allows header_old_tar and header_ustar
  1189. * to handle filenames differently, while still putting most of the
  1190. * common parsing into one place.
  1191. *
  1192. * This is called _after_ ustar, GNU tar, Schily, etc, special
  1193. * fields have already been parsed into the `tar` structure.
  1194. * So we can make final decisions here about how to reconcile
  1195. * size, mode, etc, information.
  1196. */
  1197. static int
  1198. header_common(struct archive_read *a, struct tar *tar,
  1199. struct archive_entry *entry, const void *h)
  1200. {
  1201. const struct archive_entry_header_ustar *header;
  1202. const char *existing_linkpath;
  1203. const wchar_t *existing_wcs_linkpath;
  1204. int err = ARCHIVE_OK;
  1205. header = (const struct archive_entry_header_ustar *)h;
  1206. /* Parse out the numeric fields (all are octal) */
  1207. /* Split mode handling: Set filetype always, perm only if not already set */
  1208. archive_entry_set_filetype(entry,
  1209. (mode_t)tar_atol(header->mode, sizeof(header->mode)));
  1210. if (!archive_entry_perm_is_set(entry)) {
  1211. archive_entry_set_perm(entry,
  1212. (mode_t)tar_atol(header->mode, sizeof(header->mode)));
  1213. }
  1214. /* Set uid, gid, mtime if not already set */
  1215. if (!archive_entry_uid_is_set(entry)) {
  1216. archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid)));
  1217. }
  1218. if (!archive_entry_gid_is_set(entry)) {
  1219. archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid)));
  1220. }
  1221. if (!archive_entry_mtime_is_set(entry)) {
  1222. archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0);
  1223. }
  1224. /* Reconcile the size info. */
  1225. /* First, how big is the file on disk? */
  1226. if ((tar->size_fields & TAR_SIZE_GNU_SPARSE_REALSIZE) != 0) {
  1227. /* GNU sparse format 1.0 uses `GNU.sparse.realsize`
  1228. * to hold the size of the file on disk. */
  1229. tar->disk_size = tar->GNU_sparse_realsize;
  1230. } else if ((tar->size_fields & TAR_SIZE_GNU_SPARSE_SIZE) != 0
  1231. && (tar->sparse_gnu_major == 0)) {
  1232. /* GNU sparse format 0.0 and 0.1 use `GNU.sparse.size`
  1233. * to hold the size of the file on disk. */
  1234. tar->disk_size = tar->GNU_sparse_size;
  1235. } else if ((tar->size_fields & TAR_SIZE_SCHILY_SPARSE_REALSIZE) != 0) {
  1236. tar->disk_size = tar->SCHILY_sparse_realsize;
  1237. } else if ((tar->size_fields & TAR_SIZE_PAX_SIZE) != 0) {
  1238. tar->disk_size = tar->pax_size;
  1239. } else {
  1240. /* There wasn't a suitable pax header, so use the ustar info */
  1241. tar->disk_size = tar_atol(header->size, sizeof(header->size));
  1242. }
  1243. if (tar->disk_size < 0) {
  1244. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1245. "Tar entry has negative file size");
  1246. return (ARCHIVE_FATAL);
  1247. } else if (tar->disk_size > entry_limit) {
  1248. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1249. "Tar entry size overflow");
  1250. return (ARCHIVE_FATAL);
  1251. } else {
  1252. archive_entry_set_size(entry, tar->disk_size);
  1253. }
  1254. /* Second, how big is the data in the archive? */
  1255. if ((tar->size_fields & TAR_SIZE_GNU_SPARSE_SIZE) != 0
  1256. && (tar->sparse_gnu_major == 1)) {
  1257. /* GNU sparse format 1.0 uses `GNU.sparse.size`
  1258. * to hold the size of the data in the archive. */
  1259. tar->entry_bytes_remaining = tar->GNU_sparse_size;
  1260. } else if ((tar->size_fields & TAR_SIZE_PAX_SIZE) != 0) {
  1261. tar->entry_bytes_remaining = tar->pax_size;
  1262. } else {
  1263. tar->entry_bytes_remaining
  1264. = tar_atol(header->size, sizeof(header->size));
  1265. }
  1266. if (tar->entry_bytes_remaining < 0) {
  1267. tar->entry_bytes_remaining = 0;
  1268. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1269. "Tar entry has negative size");
  1270. return (ARCHIVE_FATAL);
  1271. } else if (tar->entry_bytes_remaining > entry_limit) {
  1272. tar->entry_bytes_remaining = 0;
  1273. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1274. "Tar entry size overflow");
  1275. return (ARCHIVE_FATAL);
  1276. }
  1277. /* Handle the tar type flag appropriately. */
  1278. tar->filetype = header->typeflag[0];
  1279. /*
  1280. * TODO: If the linkpath came from Pax extension header, then
  1281. * we should obey the hdrcharset_utf8 flag when converting these.
  1282. */
  1283. switch (tar->filetype) {
  1284. case '1': /* Hard link */
  1285. archive_entry_set_link_to_hardlink(entry);
  1286. existing_wcs_linkpath = archive_entry_hardlink_w(entry);
  1287. existing_linkpath = archive_entry_hardlink(entry);
  1288. if ((existing_linkpath == NULL || existing_linkpath[0] == '\0')
  1289. && (existing_wcs_linkpath == NULL || existing_wcs_linkpath[0] == '\0')) {
  1290. struct archive_string linkpath;
  1291. archive_string_init(&linkpath);
  1292. archive_strncpy(&linkpath,
  1293. header->linkname, sizeof(header->linkname));
  1294. if (archive_entry_copy_hardlink_l(entry, linkpath.s,
  1295. archive_strlen(&linkpath), tar->sconv) != 0) {
  1296. err = set_conversion_failed_error(a, tar->sconv,
  1297. "Linkname");
  1298. if (err == ARCHIVE_FATAL) {
  1299. archive_string_free(&linkpath);
  1300. return (err);
  1301. }
  1302. }
  1303. archive_string_free(&linkpath);
  1304. }
  1305. /*
  1306. * The following may seem odd, but: Technically, tar
  1307. * does not store the file type for a "hard link"
  1308. * entry, only the fact that it is a hard link. So, I
  1309. * leave the type zero normally. But, pax interchange
  1310. * format allows hard links to have data, which
  1311. * implies that the underlying entry is a regular
  1312. * file.
  1313. */
  1314. if (archive_entry_size(entry) > 0)
  1315. archive_entry_set_filetype(entry, AE_IFREG);
  1316. /*
  1317. * A tricky point: Traditionally, tar readers have
  1318. * ignored the size field when reading hardlink
  1319. * entries, and some writers put non-zero sizes even
  1320. * though the body is empty. POSIX blessed this
  1321. * convention in the 1988 standard, but broke with
  1322. * this tradition in 2001 by permitting hardlink
  1323. * entries to store valid bodies in pax interchange
  1324. * format, but not in ustar format. Since there is no
  1325. * hard and fast way to distinguish pax interchange
  1326. * from earlier archives (the 'x' and 'g' entries are
  1327. * optional, after all), we need a heuristic.
  1328. */
  1329. if (archive_entry_size(entry) == 0) {
  1330. /* If the size is already zero, we're done. */
  1331. } else if (a->archive.archive_format
  1332. == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
  1333. /* Definitely pax extended; must obey hardlink size. */
  1334. } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR
  1335. || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR)
  1336. {
  1337. /* Old-style or GNU tar: we must ignore the size. */
  1338. archive_entry_set_size(entry, 0);
  1339. tar->entry_bytes_remaining = 0;
  1340. } else if (archive_read_format_tar_bid(a, 50) > 50) {
  1341. /*
  1342. * We don't know if it's pax: If the bid
  1343. * function sees a valid ustar header
  1344. * immediately following, then let's ignore
  1345. * the hardlink size.
  1346. */
  1347. archive_entry_set_size(entry, 0);
  1348. tar->entry_bytes_remaining = 0;
  1349. }
  1350. /*
  1351. * TODO: There are still two cases I'd like to handle:
  1352. * = a ustar non-pax archive with a hardlink entry at
  1353. * end-of-archive. (Look for block of nulls following?)
  1354. * = a pax archive that has not seen any pax headers
  1355. * and has an entry which is a hardlink entry storing
  1356. * a body containing an uncompressed tar archive.
  1357. * The first is worth addressing; I don't see any reliable
  1358. * way to deal with the second possibility.
  1359. */
  1360. break;
  1361. case '2': /* Symlink */
  1362. archive_entry_set_link_to_symlink(entry);
  1363. existing_wcs_linkpath = archive_entry_symlink_w(entry);
  1364. existing_linkpath = archive_entry_symlink(entry);
  1365. if ((existing_linkpath == NULL || existing_linkpath[0] == '\0')
  1366. && (existing_wcs_linkpath == NULL || existing_wcs_linkpath[0] == '\0')) {
  1367. struct archive_string linkpath;
  1368. archive_string_init(&linkpath);
  1369. archive_strncpy(&linkpath,
  1370. header->linkname, sizeof(header->linkname));
  1371. if (archive_entry_copy_symlink_l(entry, linkpath.s,
  1372. archive_strlen(&linkpath), tar->sconv) != 0) {
  1373. err = set_conversion_failed_error(a, tar->sconv,
  1374. "Linkname");
  1375. if (err == ARCHIVE_FATAL) {
  1376. archive_string_free(&linkpath);
  1377. return (err);
  1378. }
  1379. }
  1380. archive_string_free(&linkpath);
  1381. }
  1382. archive_entry_set_filetype(entry, AE_IFLNK);
  1383. archive_entry_set_size(entry, 0);
  1384. tar->entry_bytes_remaining = 0;
  1385. break;
  1386. case '3': /* Character device */
  1387. archive_entry_set_filetype(entry, AE_IFCHR);
  1388. archive_entry_set_size(entry, 0);
  1389. tar->entry_bytes_remaining = 0;
  1390. break;
  1391. case '4': /* Block device */
  1392. archive_entry_set_filetype(entry, AE_IFBLK);
  1393. archive_entry_set_size(entry, 0);
  1394. tar->entry_bytes_remaining = 0;
  1395. break;
  1396. case '5': /* Dir */
  1397. archive_entry_set_filetype(entry, AE_IFDIR);
  1398. archive_entry_set_size(entry, 0);
  1399. tar->entry_bytes_remaining = 0;
  1400. break;
  1401. case '6': /* FIFO device */
  1402. archive_entry_set_filetype(entry, AE_IFIFO);
  1403. archive_entry_set_size(entry, 0);
  1404. tar->entry_bytes_remaining = 0;
  1405. break;
  1406. case 'D': /* GNU incremental directory type */
  1407. /*
  1408. * No special handling is actually required here.
  1409. * It might be nice someday to preprocess the file list and
  1410. * provide it to the client, though.
  1411. */
  1412. archive_entry_set_filetype(entry, AE_IFDIR);
  1413. break;
  1414. case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
  1415. /*
  1416. * As far as I can tell, this is just like a regular file
  1417. * entry, except that the contents should be _appended_ to
  1418. * the indicated file at the indicated offset. This may
  1419. * require some API work to fully support.
  1420. */
  1421. break;
  1422. case 'N': /* Old GNU "long filename" entry. */
  1423. /* The body of this entry is a script for renaming
  1424. * previously-extracted entries. Ugh. It will never
  1425. * be supported by libarchive. */
  1426. archive_entry_set_filetype(entry, AE_IFREG);
  1427. break;
  1428. case 'S': /* GNU sparse files */
  1429. /*
  1430. * Sparse files are really just regular files with
  1431. * sparse information in the extended area.
  1432. */
  1433. /* FALLTHROUGH */
  1434. case '0': /* ustar "regular" file */
  1435. /* FALLTHROUGH */
  1436. default: /* Non-standard file types */
  1437. /*
  1438. * Per POSIX: non-recognized types should always be
  1439. * treated as regular files.
  1440. */
  1441. archive_entry_set_filetype(entry, AE_IFREG);
  1442. break;
  1443. }
  1444. return (err);
  1445. }
  1446. /*
  1447. * Parse out header elements for "old-style" tar archives.
  1448. */
  1449. static int
  1450. header_old_tar(struct archive_read *a, struct tar *tar,
  1451. struct archive_entry *entry, const void *h)
  1452. {
  1453. const struct archive_entry_header_ustar *header;
  1454. int err = ARCHIVE_OK, err2;
  1455. /*
  1456. * Copy filename over (to ensure null termination).
  1457. * Skip if pathname was already set e.g. by header_gnu_longname()
  1458. */
  1459. header = (const struct archive_entry_header_ustar *)h;
  1460. const char *existing_pathname = archive_entry_pathname(entry);
  1461. const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry);
  1462. if ((existing_pathname == NULL || existing_pathname[0] == '\0')
  1463. && (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0') &&
  1464. archive_entry_copy_pathname_l(entry,
  1465. header->name, sizeof(header->name), tar->sconv) != 0) {
  1466. err = set_conversion_failed_error(a, tar->sconv, "Pathname");
  1467. if (err == ARCHIVE_FATAL)
  1468. return (err);
  1469. }
  1470. /* Grab rest of common fields */
  1471. err2 = header_common(a, tar, entry, h);
  1472. if (err > err2)
  1473. err = err2;
  1474. tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
  1475. return (err);
  1476. }
  1477. /*
  1478. * Is this likely an AppleDouble extension?
  1479. */
  1480. static int
  1481. is_mac_metadata_entry(struct archive_entry *entry) {
  1482. const char *p, *name;
  1483. const wchar_t *wp, *wname;
  1484. wname = wp = archive_entry_pathname_w(entry);
  1485. if (wp != NULL) {
  1486. /* Find the last path element. */
  1487. for (; *wp != L'\0'; ++wp) {
  1488. if (wp[0] == '/' && wp[1] != L'\0')
  1489. wname = wp + 1;
  1490. }
  1491. /*
  1492. * If last path element starts with "._", then
  1493. * this is a Mac extension.
  1494. */
  1495. if (wname[0] == L'.' && wname[1] == L'_' && wname[2] != L'\0')
  1496. return 1;
  1497. } else {
  1498. /* Find the last path element. */
  1499. name = p = archive_entry_pathname(entry);
  1500. if (p == NULL)
  1501. return (ARCHIVE_FAILED);
  1502. for (; *p != '\0'; ++p) {
  1503. if (p[0] == '/' && p[1] != '\0')
  1504. name = p + 1;
  1505. }
  1506. /*
  1507. * If last path element starts with "._", then
  1508. * this is a Mac extension.
  1509. */
  1510. if (name[0] == '.' && name[1] == '_' && name[2] != '\0')
  1511. return 1;
  1512. }
  1513. /* Not a mac extension */
  1514. return 0;
  1515. }
  1516. /*
  1517. * Read a Mac AppleDouble-encoded blob of file metadata,
  1518. * if there is one.
  1519. *
  1520. * TODO: In Libarchive 4, we should consider ripping this
  1521. * out -- instead, return a file starting with `._` as
  1522. * a regular file and let the client (or archive_write logic)
  1523. * handle it.
  1524. */
  1525. static int
  1526. read_mac_metadata_blob(struct archive_read *a,
  1527. struct archive_entry *entry, size_t *unconsumed)
  1528. {
  1529. int64_t size;
  1530. size_t msize;
  1531. const void *data;
  1532. /* Read the body as a Mac OS metadata blob. */
  1533. size = archive_entry_size(entry);
  1534. msize = (size_t)size;
  1535. if (size < 0 || (uintmax_t)msize != (uintmax_t)size) {
  1536. *unconsumed = 0;
  1537. return (ARCHIVE_FATAL);
  1538. }
  1539. /* TODO: Should this merely skip the overlarge entry and
  1540. * WARN? Or is xattr_limit sufficiently large that we can
  1541. * safely assume anything larger is malicious? */
  1542. if (size > (int64_t)xattr_limit) {
  1543. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1544. "Oversized AppleDouble extension has size %llu > %llu",
  1545. (unsigned long long)size,
  1546. (unsigned long long)xattr_limit);
  1547. return (ARCHIVE_FATAL);
  1548. }
  1549. /*
  1550. * TODO: Look beyond the body here to peek at the next header.
  1551. * If it's a regular header (not an extension header)
  1552. * that has the wrong name, just return the current
  1553. * entry as-is, without consuming the body here.
  1554. * That would reduce the risk of us mis-identifying
  1555. * an ordinary file that just happened to have
  1556. * a name starting with "._".
  1557. *
  1558. * Q: Is the above idea really possible? Even
  1559. * when there are GNU or pax extension entries?
  1560. */
  1561. tar_flush_unconsumed(a, unconsumed);
  1562. data = __archive_read_ahead(a, msize, NULL);
  1563. if (data == NULL) {
  1564. archive_set_error(&a->archive, EINVAL,
  1565. "Truncated archive"
  1566. " detected while reading macOS metadata");
  1567. *unconsumed = 0;
  1568. return (ARCHIVE_FATAL);
  1569. }
  1570. archive_entry_clear(entry);
  1571. archive_entry_copy_mac_metadata(entry, data, msize);
  1572. *unconsumed = (msize + 511) & ~ 511;
  1573. return (ARCHIVE_OK);
  1574. }
  1575. /*
  1576. * Parse a file header for a pax extended archive entry.
  1577. */
  1578. static int
  1579. header_pax_global(struct archive_read *a, struct tar *tar,
  1580. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1581. {
  1582. const struct archive_entry_header_ustar *header;
  1583. int64_t size, to_consume;
  1584. (void)a; /* UNUSED */
  1585. (void)tar; /* UNUSED */
  1586. (void)entry; /* UNUSED */
  1587. header = (const struct archive_entry_header_ustar *)h;
  1588. size = tar_atol(header->size, sizeof(header->size));
  1589. if (size > entry_limit) {
  1590. return (ARCHIVE_FATAL);
  1591. }
  1592. to_consume = ((size + 511) & ~511);
  1593. *unconsumed += to_consume;
  1594. return (ARCHIVE_OK);
  1595. }
  1596. /*
  1597. * Parse a file header for a Posix "ustar" archive entry. This also
  1598. * handles "pax" or "extended ustar" entries.
  1599. *
  1600. * In order to correctly handle pax attributes (which precede this),
  1601. * we have to skip parsing any field for which the entry already has
  1602. * contents.
  1603. */
  1604. static int
  1605. header_ustar(struct archive_read *a, struct tar *tar,
  1606. struct archive_entry *entry, const void *h)
  1607. {
  1608. const struct archive_entry_header_ustar *header;
  1609. struct archive_string as;
  1610. int err = ARCHIVE_OK, r;
  1611. header = (const struct archive_entry_header_ustar *)h;
  1612. /* Copy name into an internal buffer to ensure null-termination. */
  1613. const char *existing_pathname = archive_entry_pathname(entry);
  1614. const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry);
  1615. if ((existing_pathname == NULL || existing_pathname[0] == '\0')
  1616. && (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0')) {
  1617. archive_string_init(&as);
  1618. if (header->prefix[0]) {
  1619. archive_strncpy(&as, header->prefix, sizeof(header->prefix));
  1620. if (as.s[archive_strlen(&as) - 1] != '/')
  1621. archive_strappend_char(&as, '/');
  1622. archive_strncat(&as, header->name, sizeof(header->name));
  1623. } else {
  1624. archive_strncpy(&as, header->name, sizeof(header->name));
  1625. }
  1626. if (archive_entry_copy_pathname_l(entry, as.s, archive_strlen(&as),
  1627. tar->sconv) != 0) {
  1628. err = set_conversion_failed_error(a, tar->sconv, "Pathname");
  1629. if (err == ARCHIVE_FATAL)
  1630. return (err);
  1631. }
  1632. archive_string_free(&as);
  1633. }
  1634. /* Handle rest of common fields. */
  1635. r = header_common(a, tar, entry, h);
  1636. if (r == ARCHIVE_FATAL)
  1637. return (r);
  1638. if (r < err)
  1639. err = r;
  1640. /* Handle POSIX ustar fields. */
  1641. const char *existing_uname = archive_entry_uname(entry);
  1642. if (existing_uname == NULL || existing_uname[0] == '\0') {
  1643. if (archive_entry_copy_uname_l(entry,
  1644. header->uname, sizeof(header->uname), tar->sconv) != 0) {
  1645. err = set_conversion_failed_error(a, tar->sconv, "Uname");
  1646. if (err == ARCHIVE_FATAL)
  1647. return (err);
  1648. }
  1649. }
  1650. const char *existing_gname = archive_entry_gname(entry);
  1651. if (existing_gname == NULL || existing_gname[0] == '\0') {
  1652. if (archive_entry_copy_gname_l(entry,
  1653. header->gname, sizeof(header->gname), tar->sconv) != 0) {
  1654. err = set_conversion_failed_error(a, tar->sconv, "Gname");
  1655. if (err == ARCHIVE_FATAL)
  1656. return (err);
  1657. }
  1658. }
  1659. /* Parse out device numbers only for char and block specials. */
  1660. if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
  1661. if (!archive_entry_rdev_is_set(entry)) {
  1662. archive_entry_set_rdevmajor(entry, (dev_t)
  1663. tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
  1664. archive_entry_set_rdevminor(entry, (dev_t)
  1665. tar_atol(header->rdevminor, sizeof(header->rdevminor)));
  1666. }
  1667. } else {
  1668. archive_entry_set_rdev(entry, 0);
  1669. }
  1670. tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
  1671. return (err);
  1672. }
  1673. static int
  1674. header_pax_extension(struct archive_read *a, struct tar *tar,
  1675. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1676. {
  1677. /* Sanity checks: The largest `x` body I've ever heard of was
  1678. * a little over 4MB. So I doubt there has ever been a
  1679. * well-formed archive with an `x` body over 1GiB. Similarly,
  1680. * it seems plausible that no single attribute has ever been
  1681. * larger than 100MB. So if we see a larger value here, it's
  1682. * almost certainly a sign of a corrupted/malicious archive. */
  1683. /* Maximum sane size for extension body: 1 GiB */
  1684. /* This cannot be raised to larger than 8GiB without
  1685. * exceeding the maximum size for a standard ustar
  1686. * entry. */
  1687. const int64_t ext_size_limit = 1024 * 1024 * (int64_t)1024;
  1688. /* Maximum size for a single line/attr: 100 million characters */
  1689. /* This cannot be raised to more than 2GiB without exceeding
  1690. * a `size_t` on 32-bit platforms. */
  1691. const size_t max_parsed_line_length = 99999999ULL;
  1692. /* Largest attribute prolog: size + name. */
  1693. const size_t max_size_name = 512;
  1694. /* Size and padding of the full extension body */
  1695. int64_t ext_size, ext_padding;
  1696. size_t line_length, value_length, name_length;
  1697. ssize_t to_read, did_read;
  1698. const struct archive_entry_header_ustar *header;
  1699. const char *p, *attr_start, *name_start;
  1700. struct archive_string_conv *sconv;
  1701. struct archive_string *pas = NULL;
  1702. struct archive_string attr_name;
  1703. int err = ARCHIVE_OK, r;
  1704. header = (const struct archive_entry_header_ustar *)h;
  1705. ext_size = tar_atol(header->size, sizeof(header->size));
  1706. if (ext_size > entry_limit) {
  1707. return (ARCHIVE_FATAL);
  1708. }
  1709. if (ext_size < 0) {
  1710. archive_set_error(&a->archive, EINVAL,
  1711. "pax extension header has invalid size: %lld",
  1712. (long long)ext_size);
  1713. return (ARCHIVE_FATAL);
  1714. }
  1715. ext_padding = 0x1ff & (-ext_size);
  1716. if (ext_size > ext_size_limit) {
  1717. /* Consume the pax extension body and return an error */
  1718. if (ext_size + ext_padding != __archive_read_consume(a, ext_size + ext_padding)) {
  1719. return (ARCHIVE_FATAL);
  1720. }
  1721. archive_set_error(&a->archive, EINVAL,
  1722. "Ignoring oversized pax extensions: %d > %d",
  1723. (int)ext_size, (int)ext_size_limit);
  1724. return (ARCHIVE_WARN);
  1725. }
  1726. tar_flush_unconsumed(a, unconsumed);
  1727. /* Parse the size/name of each pax attribute in the body */
  1728. archive_string_init(&attr_name);
  1729. while (ext_size > 0) {
  1730. /* Read enough bytes to parse the size/name of the next attribute */
  1731. to_read = max_size_name;
  1732. if (to_read > ext_size) {
  1733. to_read = ext_size;
  1734. }
  1735. p = __archive_read_ahead(a, to_read, &did_read);
  1736. if (p == NULL) { /* EOF */
  1737. archive_set_error(&a->archive, EINVAL,
  1738. "Truncated tar archive"
  1739. " detected while reading pax attribute name");
  1740. return (ARCHIVE_FATAL);
  1741. }
  1742. if (did_read > ext_size) {
  1743. did_read = ext_size;
  1744. }
  1745. /* Parse size of attribute */
  1746. line_length = 0;
  1747. attr_start = p;
  1748. while (1) {
  1749. if (p >= attr_start + did_read) {
  1750. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1751. "Ignoring malformed pax attributes: overlarge attribute size field");
  1752. *unconsumed += ext_size + ext_padding;
  1753. return (ARCHIVE_WARN);
  1754. }
  1755. if (*p == ' ') {
  1756. p++;
  1757. break;
  1758. }
  1759. if (*p < '0' || *p > '9') {
  1760. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1761. "Ignoring malformed pax attributes: malformed attribute size field");
  1762. *unconsumed += ext_size + ext_padding;
  1763. return (ARCHIVE_WARN);
  1764. }
  1765. line_length *= 10;
  1766. line_length += *p - '0';
  1767. if (line_length > max_parsed_line_length) {
  1768. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1769. "Ignoring malformed pax attribute: size > %lld",
  1770. (long long)max_parsed_line_length);
  1771. *unconsumed += ext_size + ext_padding;
  1772. return (ARCHIVE_WARN);
  1773. }
  1774. p++;
  1775. }
  1776. if ((int64_t)line_length > ext_size) {
  1777. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1778. "Ignoring malformed pax attribute: %lld > %lld",
  1779. (long long)line_length, (long long)ext_size);
  1780. *unconsumed += ext_size + ext_padding;
  1781. return (ARCHIVE_WARN);
  1782. }
  1783. /* Parse name of attribute */
  1784. if (p >= attr_start + did_read
  1785. || p >= attr_start + line_length
  1786. || *p == '=') {
  1787. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1788. "Ignoring malformed pax attributes: empty name found");
  1789. *unconsumed += ext_size + ext_padding;
  1790. return (ARCHIVE_WARN);
  1791. }
  1792. name_start = p;
  1793. while (1) {
  1794. if (p >= attr_start + did_read || p >= attr_start + line_length) {
  1795. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1796. "Ignoring malformed pax attributes: overlarge attribute name");
  1797. *unconsumed += ext_size + ext_padding;
  1798. return (ARCHIVE_WARN);
  1799. }
  1800. if (*p == '=') {
  1801. break;
  1802. }
  1803. p++;
  1804. }
  1805. name_length = p - name_start;
  1806. p++; // Skip '='
  1807. // Save the name before we consume it
  1808. archive_strncpy(&attr_name, name_start, name_length);
  1809. ext_size -= p - attr_start;
  1810. value_length = line_length - (p - attr_start);
  1811. /* Consume size, name, and `=` */
  1812. *unconsumed += p - attr_start;
  1813. tar_flush_unconsumed(a, unconsumed);
  1814. /* pax_attribute will consume value_length - 1 */
  1815. r = pax_attribute(a, tar, entry, attr_name.s, archive_strlen(&attr_name), value_length - 1, unconsumed);
  1816. ext_size -= value_length - 1;
  1817. // Release the allocated attr_name (either here or before every return in this function)
  1818. archive_string_free(&attr_name);
  1819. if (r < ARCHIVE_WARN) {
  1820. *unconsumed += ext_size + ext_padding;
  1821. return (r);
  1822. }
  1823. err = err_combine(err, r);
  1824. /* Consume the `\n` that follows the pax attribute value. */
  1825. tar_flush_unconsumed(a, unconsumed);
  1826. p = __archive_read_ahead(a, 1, &did_read);
  1827. if (p == NULL) {
  1828. archive_set_error(&a->archive, EINVAL,
  1829. "Truncated tar archive"
  1830. " detected while completing pax attribute");
  1831. return (ARCHIVE_FATAL);
  1832. }
  1833. if (p[0] != '\n') {
  1834. archive_set_error(&a->archive, EINVAL,
  1835. "Malformed pax attributes");
  1836. *unconsumed += ext_size + ext_padding;
  1837. return (ARCHIVE_WARN);
  1838. }
  1839. ext_size -= 1;
  1840. *unconsumed += 1;
  1841. tar_flush_unconsumed(a, unconsumed);
  1842. }
  1843. archive_string_free(&attr_name);
  1844. *unconsumed += ext_size + ext_padding;
  1845. /*
  1846. * Some PAX values -- pathname, linkpath, uname, gname --
  1847. * can't be copied into the entry until we know the character
  1848. * set to use:
  1849. */
  1850. if (!tar->pax_hdrcharset_utf8)
  1851. /* PAX specified "BINARY", so use the default charset */
  1852. sconv = tar->opt_sconv;
  1853. else {
  1854. /* PAX default UTF-8 */
  1855. sconv = archive_string_conversion_from_charset(
  1856. &(a->archive), "UTF-8", 1);
  1857. if (sconv == NULL)
  1858. return (ARCHIVE_FATAL);
  1859. if (tar->compat_2x)
  1860. archive_string_conversion_set_opt(sconv,
  1861. SCONV_SET_OPT_UTF8_LIBARCHIVE2X);
  1862. }
  1863. /* Pathname */
  1864. pas = NULL;
  1865. if (archive_strlen(&(tar->entry_pathname_override)) > 0) {
  1866. /* Prefer GNU.sparse.name attribute if present */
  1867. /* GNU sparse files store a fake name under the standard
  1868. * "pathname" key. */
  1869. pas = &(tar->entry_pathname_override);
  1870. } else if (archive_strlen(&(tar->entry_pathname)) > 0) {
  1871. /* Use standard "pathname" PAX extension */
  1872. pas = &(tar->entry_pathname);
  1873. }
  1874. if (pas != NULL) {
  1875. if (archive_entry_copy_pathname_l(entry, pas->s,
  1876. archive_strlen(pas), sconv) != 0) {
  1877. err = set_conversion_failed_error(a, sconv, "Pathname");
  1878. if (err == ARCHIVE_FATAL)
  1879. return (err);
  1880. /* Use raw name without conversion */
  1881. archive_entry_copy_pathname(entry, pas->s);
  1882. }
  1883. }
  1884. /* Uname */
  1885. if (archive_strlen(&(tar->entry_uname)) > 0) {
  1886. if (archive_entry_copy_uname_l(entry, tar->entry_uname.s,
  1887. archive_strlen(&(tar->entry_uname)), sconv) != 0) {
  1888. err = set_conversion_failed_error(a, sconv, "Uname");
  1889. if (err == ARCHIVE_FATAL)
  1890. return (err);
  1891. /* Use raw name without conversion */
  1892. archive_entry_copy_uname(entry, tar->entry_uname.s);
  1893. }
  1894. }
  1895. /* Gname */
  1896. if (archive_strlen(&(tar->entry_gname)) > 0) {
  1897. if (archive_entry_copy_gname_l(entry, tar->entry_gname.s,
  1898. archive_strlen(&(tar->entry_gname)), sconv) != 0) {
  1899. err = set_conversion_failed_error(a, sconv, "Gname");
  1900. if (err == ARCHIVE_FATAL)
  1901. return (err);
  1902. /* Use raw name without conversion */
  1903. archive_entry_copy_gname(entry, tar->entry_gname.s);
  1904. }
  1905. }
  1906. /* Linkpath */
  1907. if (archive_strlen(&(tar->entry_linkpath)) > 0) {
  1908. if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s,
  1909. archive_strlen(&(tar->entry_linkpath)), sconv) != 0) {
  1910. err = set_conversion_failed_error(a, sconv, "Linkpath");
  1911. if (err == ARCHIVE_FATAL)
  1912. return (err);
  1913. /* Use raw name without conversion */
  1914. archive_entry_copy_link(entry, tar->entry_linkpath.s);
  1915. }
  1916. }
  1917. /* Extension may have given us a corrected `entry_bytes_remaining` for
  1918. * the main entry; update the padding appropriately. */
  1919. tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
  1920. return (err);
  1921. }
  1922. static int
  1923. pax_attribute_LIBARCHIVE_xattr(struct archive_entry *entry,
  1924. const char *name, size_t name_length, const char *value, size_t value_length)
  1925. {
  1926. char *name_decoded;
  1927. void *value_decoded;
  1928. size_t value_len;
  1929. if (name_length < 1)
  1930. return 3;
  1931. /* URL-decode name */
  1932. name_decoded = url_decode(name, name_length);
  1933. if (name_decoded == NULL)
  1934. return 2;
  1935. /* Base-64 decode value */
  1936. value_decoded = base64_decode(value, value_length, &value_len);
  1937. if (value_decoded == NULL) {
  1938. free(name_decoded);
  1939. return 1;
  1940. }
  1941. archive_entry_xattr_add_entry(entry, name_decoded,
  1942. value_decoded, value_len);
  1943. free(name_decoded);
  1944. free(value_decoded);
  1945. return 0;
  1946. }
  1947. static int
  1948. pax_attribute_SCHILY_xattr(struct archive_entry *entry,
  1949. const char *name, size_t name_length, const char *value, size_t value_length)
  1950. {
  1951. if (name_length < 1 || name_length > 128) {
  1952. return 1;
  1953. }
  1954. char * null_terminated_name = malloc(name_length + 1);
  1955. if (null_terminated_name != NULL) {
  1956. memcpy(null_terminated_name, name, name_length);
  1957. null_terminated_name[name_length] = '\0';
  1958. archive_entry_xattr_add_entry(entry, null_terminated_name, value, value_length);
  1959. free(null_terminated_name);
  1960. }
  1961. return 0;
  1962. }
  1963. static int
  1964. pax_attribute_RHT_security_selinux(struct archive_entry *entry,
  1965. const char *value, size_t value_length)
  1966. {
  1967. archive_entry_xattr_add_entry(entry, "security.selinux",
  1968. value, value_length);
  1969. return 0;
  1970. }
  1971. static int
  1972. pax_attribute_SCHILY_acl(struct archive_read *a, struct tar *tar,
  1973. struct archive_entry *entry, size_t value_length, int type)
  1974. {
  1975. int r;
  1976. const char *p;
  1977. const char* errstr;
  1978. switch (type) {
  1979. case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
  1980. errstr = "SCHILY.acl.access";
  1981. break;
  1982. case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
  1983. errstr = "SCHILY.acl.default";
  1984. break;
  1985. case ARCHIVE_ENTRY_ACL_TYPE_NFS4:
  1986. errstr = "SCHILY.acl.ace";
  1987. break;
  1988. default:
  1989. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1990. "Unknown ACL type: %d", type);
  1991. return(ARCHIVE_FATAL);
  1992. }
  1993. if (tar->sconv_acl == NULL) {
  1994. tar->sconv_acl =
  1995. archive_string_conversion_from_charset(
  1996. &(a->archive), "UTF-8", 1);
  1997. if (tar->sconv_acl == NULL)
  1998. return (ARCHIVE_FATAL);
  1999. }
  2000. if (value_length > acl_limit) {
  2001. __archive_read_consume(a, value_length);
  2002. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2003. "Unreasonably large ACL: %d > %d",
  2004. (int)value_length, (int)acl_limit);
  2005. return (ARCHIVE_WARN);
  2006. }
  2007. p = __archive_read_ahead(a, value_length, NULL);
  2008. if (p == NULL) {
  2009. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2010. "Truncated tar archive "
  2011. "detected while reading ACL data");
  2012. return (ARCHIVE_FATAL);
  2013. }
  2014. r = archive_acl_from_text_nl(archive_entry_acl(entry), p, value_length,
  2015. type, tar->sconv_acl);
  2016. __archive_read_consume(a, value_length);
  2017. /* Workaround: Force perm_is_set() to be correct */
  2018. /* If this bit were stored in the ACL, this wouldn't be needed */
  2019. archive_entry_set_perm(entry, archive_entry_perm(entry));
  2020. if (r != ARCHIVE_OK) {
  2021. if (r == ARCHIVE_FATAL) {
  2022. archive_set_error(&a->archive, ENOMEM,
  2023. "%s %s", "Can't allocate memory for ",
  2024. errstr);
  2025. return (r);
  2026. }
  2027. archive_set_error(&a->archive,
  2028. ARCHIVE_ERRNO_MISC, "%s %s", "Parse error: ", errstr);
  2029. }
  2030. return (r);
  2031. }
  2032. static int
  2033. pax_attribute_read_time(struct archive_read *a, size_t value_length, int64_t *ps, long *pn, size_t *unconsumed) {
  2034. struct archive_string as;
  2035. int r;
  2036. if (value_length > 128) {
  2037. __archive_read_consume(a, value_length);
  2038. *ps = 0;
  2039. *pn = 0;
  2040. return (ARCHIVE_FATAL);
  2041. }
  2042. archive_string_init(&as);
  2043. r = read_bytes_to_string(a, &as, value_length, unconsumed);
  2044. if (r < ARCHIVE_OK) {
  2045. archive_string_free(&as);
  2046. return (r);
  2047. }
  2048. pax_time(as.s, archive_strlen(&as), ps, pn);
  2049. archive_string_free(&as);
  2050. if (*ps < 0 || *ps == INT64_MAX) {
  2051. return (ARCHIVE_WARN);
  2052. }
  2053. return (ARCHIVE_OK);
  2054. }
  2055. static int
  2056. pax_attribute_read_number(struct archive_read *a, size_t value_length, int64_t *result) {
  2057. struct archive_string as;
  2058. size_t unconsumed = 0;
  2059. int r;
  2060. if (value_length > 64) {
  2061. __archive_read_consume(a, value_length);
  2062. *result = 0;
  2063. return (ARCHIVE_FATAL);
  2064. }
  2065. archive_string_init(&as);
  2066. r = read_bytes_to_string(a, &as, value_length, &unconsumed);
  2067. tar_flush_unconsumed(a, &unconsumed);
  2068. if (r < ARCHIVE_OK) {
  2069. archive_string_free(&as);
  2070. return (r);
  2071. }
  2072. *result = tar_atol10(as.s, archive_strlen(&as));
  2073. archive_string_free(&as);
  2074. if (*result < 0 || *result == INT64_MAX) {
  2075. *result = INT64_MAX;
  2076. return (ARCHIVE_WARN);
  2077. }
  2078. return (ARCHIVE_OK);
  2079. }
  2080. /*
  2081. * Parse a single key=value attribute.
  2082. *
  2083. * POSIX reserves all-lowercase keywords. Vendor-specific extensions
  2084. * should always have keywords of the form "VENDOR.attribute" In
  2085. * particular, it's quite feasible to support many different vendor
  2086. * extensions here. I'm using "LIBARCHIVE" for extensions unique to
  2087. * this library.
  2088. *
  2089. * TODO: Investigate other vendor-specific extensions and see if
  2090. * any of them look useful.
  2091. */
  2092. static int
  2093. pax_attribute(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
  2094. const char *key, size_t key_length, size_t value_length, size_t *unconsumed)
  2095. {
  2096. int64_t t;
  2097. long n;
  2098. const char *p;
  2099. ssize_t bytes_read;
  2100. int err = ARCHIVE_OK;
  2101. switch (key[0]) {
  2102. case 'G':
  2103. /* GNU.* extensions */
  2104. if (key_length > 4 && memcmp(key, "GNU.", 4) == 0) {
  2105. key += 4;
  2106. key_length -= 4;
  2107. /* GNU.sparse marks the existence of GNU sparse information */
  2108. if (key_length == 6 && memcmp(key, "sparse", 6) == 0) {
  2109. tar->sparse_gnu_attributes_seen = 1;
  2110. }
  2111. /* GNU.sparse.* extensions */
  2112. else if (key_length > 7 && memcmp(key, "sparse.", 7) == 0) {
  2113. tar->sparse_gnu_attributes_seen = 1;
  2114. key += 7;
  2115. key_length -= 7;
  2116. /* GNU "0.0" sparse pax format. */
  2117. if (key_length == 9 && memcmp(key, "numblocks", 9) == 0) {
  2118. /* GNU.sparse.numblocks */
  2119. tar->sparse_offset = -1;
  2120. tar->sparse_numbytes = -1;
  2121. tar->sparse_gnu_major = 0;
  2122. tar->sparse_gnu_minor = 0;
  2123. }
  2124. else if (key_length == 6 && memcmp(key, "offset", 6) == 0) {
  2125. /* GNU.sparse.offset */
  2126. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2127. tar->sparse_offset = t;
  2128. if (tar->sparse_numbytes != -1) {
  2129. if (gnu_add_sparse_entry(a, tar,
  2130. tar->sparse_offset, tar->sparse_numbytes)
  2131. != ARCHIVE_OK)
  2132. return (ARCHIVE_FATAL);
  2133. tar->sparse_offset = -1;
  2134. tar->sparse_numbytes = -1;
  2135. }
  2136. }
  2137. return (err);
  2138. }
  2139. else if (key_length == 8 && memcmp(key, "numbytes", 8) == 0) {
  2140. /* GNU.sparse.numbytes */
  2141. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2142. tar->sparse_numbytes = t;
  2143. if (tar->sparse_offset != -1) {
  2144. if (gnu_add_sparse_entry(a, tar,
  2145. tar->sparse_offset, tar->sparse_numbytes)
  2146. != ARCHIVE_OK)
  2147. return (ARCHIVE_FATAL);
  2148. tar->sparse_offset = -1;
  2149. tar->sparse_numbytes = -1;
  2150. }
  2151. }
  2152. return (err);
  2153. }
  2154. else if (key_length == 4 && memcmp(key, "size", 4) == 0) {
  2155. /* GNU.sparse.size */
  2156. /* This is either the size of stored entry OR the size of data on disk,
  2157. * depending on which GNU sparse format version is in use.
  2158. * Since pax attributes can be in any order, we may not actually
  2159. * know at this point how to interpret this. */
  2160. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2161. tar->GNU_sparse_size = t;
  2162. tar->size_fields |= TAR_SIZE_GNU_SPARSE_SIZE;
  2163. }
  2164. return (err);
  2165. }
  2166. /* GNU "0.1" sparse pax format. */
  2167. else if (key_length == 3 && memcmp(key, "map", 3) == 0) {
  2168. /* GNU.sparse.map */
  2169. tar->sparse_gnu_major = 0;
  2170. tar->sparse_gnu_minor = 1;
  2171. if (value_length > sparse_map_limit) {
  2172. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2173. "Unreasonably large sparse map: %d > %d",
  2174. (int)value_length, (int)sparse_map_limit);
  2175. err = ARCHIVE_FAILED;
  2176. } else {
  2177. p = __archive_read_ahead(a, value_length, &bytes_read);
  2178. if (p == NULL) {
  2179. archive_set_error(&a->archive, EINVAL,
  2180. "Truncated archive"
  2181. " detected while reading GNU sparse data");
  2182. return (ARCHIVE_FATAL);
  2183. }
  2184. if (gnu_sparse_01_parse(a, tar, p, value_length) != ARCHIVE_OK) {
  2185. err = ARCHIVE_WARN;
  2186. }
  2187. }
  2188. __archive_read_consume(a, value_length);
  2189. return (err);
  2190. }
  2191. /* GNU "1.0" sparse pax format */
  2192. else if (key_length == 5 && memcmp(key, "major", 5) == 0) {
  2193. /* GNU.sparse.major */
  2194. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK
  2195. && t >= 0
  2196. && t <= 10) {
  2197. tar->sparse_gnu_major = (int)t;
  2198. }
  2199. return (err);
  2200. }
  2201. else if (key_length == 5 && memcmp(key, "minor", 5) == 0) {
  2202. /* GNU.sparse.minor */
  2203. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK
  2204. && t >= 0
  2205. && t <= 10) {
  2206. tar->sparse_gnu_minor = (int)t;
  2207. }
  2208. return (err);
  2209. }
  2210. else if (key_length == 4 && memcmp(key, "name", 4) == 0) {
  2211. /* GNU.sparse.name */
  2212. /*
  2213. * The real filename; when storing sparse
  2214. * files, GNU tar puts a synthesized name into
  2215. * the regular 'path' attribute in an attempt
  2216. * to limit confusion. ;-)
  2217. */
  2218. if (value_length > pathname_limit) {
  2219. *unconsumed += value_length;
  2220. err = ARCHIVE_WARN;
  2221. } else {
  2222. err = read_bytes_to_string(a, &(tar->entry_pathname_override),
  2223. value_length, unconsumed);
  2224. }
  2225. return (err);
  2226. }
  2227. else if (key_length == 8 && memcmp(key, "realsize", 8) == 0) {
  2228. /* GNU.sparse.realsize = size of file on disk */
  2229. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2230. tar->GNU_sparse_realsize = t;
  2231. tar->size_fields |= TAR_SIZE_GNU_SPARSE_REALSIZE;
  2232. }
  2233. return (err);
  2234. }
  2235. }
  2236. }
  2237. break;
  2238. case 'L':
  2239. /* LIBARCHIVE extensions */
  2240. if (key_length > 11 && memcmp(key, "LIBARCHIVE.", 11) == 0) {
  2241. key_length -= 11;
  2242. key += 11;
  2243. /* TODO: Handle arbitrary extended attributes... */
  2244. /*
  2245. if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0)
  2246. archive_entry_set_xxxxxx(entry, value);
  2247. */
  2248. if (key_length == 12 && memcmp(key, "creationtime", 12) == 0) {
  2249. /* LIBARCHIVE.creationtime */
  2250. if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) {
  2251. archive_entry_set_birthtime(entry, t, n);
  2252. }
  2253. return (err);
  2254. }
  2255. else if (key_length == 11 && memcmp(key, "symlinktype", 11) == 0) {
  2256. /* LIBARCHIVE.symlinktype */
  2257. if (value_length < 16) {
  2258. p = __archive_read_ahead(a, value_length, &bytes_read);
  2259. if (p == NULL) {
  2260. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2261. "Truncated tar archive "
  2262. "detected while reading `symlinktype` attribute");
  2263. return (ARCHIVE_FATAL);
  2264. }
  2265. if (value_length == 4 && memcmp(p, "file", 4) == 0) {
  2266. archive_entry_set_symlink_type(entry,
  2267. AE_SYMLINK_TYPE_FILE);
  2268. } else if (value_length == 3 && memcmp(p, "dir", 3) == 0) {
  2269. archive_entry_set_symlink_type(entry,
  2270. AE_SYMLINK_TYPE_DIRECTORY);
  2271. } else {
  2272. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2273. "Unrecognized symlink type");
  2274. err = ARCHIVE_WARN;
  2275. }
  2276. } else {
  2277. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2278. "symlink type is very long"
  2279. "(longest recognized value is 4 bytes, this is %d)",
  2280. (int)value_length);
  2281. err = ARCHIVE_WARN;
  2282. }
  2283. __archive_read_consume(a, value_length);
  2284. return (err);
  2285. }
  2286. else if (key_length > 6 && memcmp(key, "xattr.", 6) == 0) {
  2287. key_length -= 6;
  2288. key += 6;
  2289. if (value_length > xattr_limit) {
  2290. err = ARCHIVE_WARN;
  2291. } else {
  2292. p = __archive_read_ahead(a, value_length, &bytes_read);
  2293. if (p == NULL) {
  2294. archive_set_error(&a->archive, EINVAL,
  2295. "Truncated archive"
  2296. " detected while reading xattr information");
  2297. return (ARCHIVE_FATAL);
  2298. }
  2299. if (pax_attribute_LIBARCHIVE_xattr(entry, key, key_length, p, value_length)) {
  2300. /* TODO: Unable to parse xattr */
  2301. err = ARCHIVE_WARN;
  2302. }
  2303. }
  2304. __archive_read_consume(a, value_length);
  2305. return (err);
  2306. }
  2307. }
  2308. break;
  2309. case 'R':
  2310. /* GNU tar uses RHT.security header to store SELinux xattrs
  2311. * SCHILY.xattr.security.selinux == RHT.security.selinux */
  2312. if (key_length == 20 && memcmp(key, "RHT.security.selinux", 20) == 0) {
  2313. if (value_length > xattr_limit) {
  2314. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2315. "Ignoring unreasonably large security.selinux attribute:"
  2316. " %d > %d",
  2317. (int)value_length, (int)xattr_limit);
  2318. /* TODO: Should this be FAILED instead? */
  2319. err = ARCHIVE_WARN;
  2320. } else {
  2321. p = __archive_read_ahead(a, value_length, &bytes_read);
  2322. if (p == NULL) {
  2323. archive_set_error(&a->archive, EINVAL,
  2324. "Truncated archive"
  2325. " detected while reading selinux data");
  2326. return (ARCHIVE_FATAL);
  2327. }
  2328. if (pax_attribute_RHT_security_selinux(entry, p, value_length)) {
  2329. /* TODO: Unable to parse xattr */
  2330. err = ARCHIVE_WARN;
  2331. }
  2332. }
  2333. __archive_read_consume(a, value_length);
  2334. return (err);
  2335. }
  2336. break;
  2337. case 'S':
  2338. /* SCHILY.* extensions used by "star" archiver */
  2339. if (key_length > 7 && memcmp(key, "SCHILY.", 7) == 0) {
  2340. key_length -= 7;
  2341. key += 7;
  2342. if (key_length == 10 && memcmp(key, "acl.access", 10) == 0) {
  2343. err = pax_attribute_SCHILY_acl(a, tar, entry, value_length,
  2344. ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
  2345. // TODO: Mark mode as set
  2346. return (err);
  2347. }
  2348. else if (key_length == 11 && memcmp(key, "acl.default", 11) == 0) {
  2349. err = pax_attribute_SCHILY_acl(a, tar, entry, value_length,
  2350. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
  2351. return (err);
  2352. }
  2353. else if (key_length == 7 && memcmp(key, "acl.ace", 7) == 0) {
  2354. err = pax_attribute_SCHILY_acl(a, tar, entry, value_length,
  2355. ARCHIVE_ENTRY_ACL_TYPE_NFS4);
  2356. // TODO: Mark mode as set
  2357. return (err);
  2358. }
  2359. else if (key_length == 8 && memcmp(key, "devmajor", 8) == 0) {
  2360. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2361. archive_entry_set_rdevmajor(entry, (dev_t)t);
  2362. }
  2363. return (err);
  2364. }
  2365. else if (key_length == 8 && memcmp(key, "devminor", 8) == 0) {
  2366. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2367. archive_entry_set_rdevminor(entry, (dev_t)t);
  2368. }
  2369. return (err);
  2370. }
  2371. else if (key_length == 6 && memcmp(key, "fflags", 6) == 0) {
  2372. if (value_length < fflags_limit) {
  2373. p = __archive_read_ahead(a, value_length, &bytes_read);
  2374. if (p == NULL) {
  2375. /* Truncated archive */
  2376. archive_set_error(&a->archive, EINVAL,
  2377. "Truncated archive"
  2378. " detected while reading SCHILY.fflags");
  2379. return (ARCHIVE_FATAL);
  2380. }
  2381. archive_entry_copy_fflags_text_len(entry, p, value_length);
  2382. err = ARCHIVE_OK;
  2383. } else {
  2384. /* Overlong fflags field */
  2385. err = ARCHIVE_WARN;
  2386. }
  2387. __archive_read_consume(a, value_length);
  2388. return (err);
  2389. }
  2390. else if (key_length == 3 && memcmp(key, "dev", 3) == 0) {
  2391. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2392. archive_entry_set_dev(entry, (dev_t)t);
  2393. }
  2394. return (err);
  2395. }
  2396. else if (key_length == 3 && memcmp(key, "ino", 3) == 0) {
  2397. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2398. archive_entry_set_ino(entry, t);
  2399. }
  2400. return (err);
  2401. }
  2402. else if (key_length == 5 && memcmp(key, "nlink", 5) == 0) {
  2403. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2404. archive_entry_set_nlink(entry, (unsigned int)t);
  2405. }
  2406. return (err);
  2407. }
  2408. else if (key_length == 8 && memcmp(key, "realsize", 8) == 0) {
  2409. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2410. tar->SCHILY_sparse_realsize = t;
  2411. tar->size_fields |= TAR_SIZE_SCHILY_SPARSE_REALSIZE;
  2412. }
  2413. return (err);
  2414. }
  2415. /* TODO: Is there a SCHILY.sparse.size similar to GNU.sparse.size ? */
  2416. else if (key_length > 6 && memcmp(key, "xattr.", 6) == 0) {
  2417. key_length -= 6;
  2418. key += 6;
  2419. if (value_length < xattr_limit) {
  2420. p = __archive_read_ahead(a, value_length, &bytes_read);
  2421. if (p == NULL) {
  2422. archive_set_error(&a->archive, EINVAL,
  2423. "Truncated archive"
  2424. " detected while reading SCHILY.xattr");
  2425. return (ARCHIVE_FATAL);
  2426. }
  2427. if (pax_attribute_SCHILY_xattr(entry, key, key_length, p, value_length)) {
  2428. /* TODO: Unable to parse xattr */
  2429. err = ARCHIVE_WARN;
  2430. }
  2431. } else {
  2432. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2433. "Unreasonably large xattr: %d > %d",
  2434. (int)value_length, (int)xattr_limit);
  2435. err = ARCHIVE_WARN;
  2436. }
  2437. __archive_read_consume(a, value_length);
  2438. return (err);
  2439. }
  2440. }
  2441. /* SUN.* extensions from Solaris tar */
  2442. if (key_length > 4 && memcmp(key, "SUN.", 4) == 0) {
  2443. key_length -= 4;
  2444. key += 4;
  2445. if (key_length == 9 && memcmp(key, "holesdata", 9) == 0) {
  2446. /* SUN.holesdata */
  2447. if (value_length < sparse_map_limit) {
  2448. p = __archive_read_ahead(a, value_length, &bytes_read);
  2449. if (p == NULL) {
  2450. archive_set_error(&a->archive, EINVAL,
  2451. "Truncated archive"
  2452. " detected while reading SUN.holesdata");
  2453. return (ARCHIVE_FATAL);
  2454. }
  2455. err = pax_attribute_SUN_holesdata(a, tar, entry, p, value_length);
  2456. if (err < ARCHIVE_OK) {
  2457. archive_set_error(&a->archive,
  2458. ARCHIVE_ERRNO_MISC,
  2459. "Parse error: SUN.holesdata");
  2460. }
  2461. } else {
  2462. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2463. "Unreasonably large sparse map: %d > %d",
  2464. (int)value_length, (int)sparse_map_limit);
  2465. err = ARCHIVE_FAILED;
  2466. }
  2467. __archive_read_consume(a, value_length);
  2468. return (err);
  2469. }
  2470. }
  2471. break;
  2472. case 'a':
  2473. if (key_length == 5 && memcmp(key, "atime", 5) == 0) {
  2474. if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) {
  2475. archive_entry_set_atime(entry, t, n);
  2476. }
  2477. return (err);
  2478. }
  2479. break;
  2480. case 'c':
  2481. if (key_length == 5 && memcmp(key, "ctime", 5) == 0) {
  2482. if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) {
  2483. archive_entry_set_ctime(entry, t, n);
  2484. }
  2485. return (err);
  2486. } else if (key_length == 7 && memcmp(key, "charset", 7) == 0) {
  2487. /* TODO: Publish charset information in entry. */
  2488. } else if (key_length == 7 && memcmp(key, "comment", 7) == 0) {
  2489. /* TODO: Publish comment in entry. */
  2490. }
  2491. break;
  2492. case 'g':
  2493. if (key_length == 3 && memcmp(key, "gid", 3) == 0) {
  2494. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2495. archive_entry_set_gid(entry, t);
  2496. }
  2497. return (err);
  2498. } else if (key_length == 5 && memcmp(key, "gname", 5) == 0) {
  2499. if (value_length > guname_limit) {
  2500. *unconsumed += value_length;
  2501. err = ARCHIVE_WARN;
  2502. } else {
  2503. err = read_bytes_to_string(a, &(tar->entry_gname), value_length, unconsumed);
  2504. }
  2505. return (err);
  2506. }
  2507. break;
  2508. case 'h':
  2509. if (key_length == 10 && memcmp(key, "hdrcharset", 10) == 0) {
  2510. if (value_length < 64) {
  2511. p = __archive_read_ahead(a, value_length, &bytes_read);
  2512. if (p == NULL) {
  2513. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2514. "Truncated tar archive "
  2515. "detected while reading hdrcharset attribute");
  2516. return (ARCHIVE_FATAL);
  2517. }
  2518. if (value_length == 6
  2519. && memcmp(p, "BINARY", 6) == 0) {
  2520. /* Binary mode. */
  2521. tar->pax_hdrcharset_utf8 = 0;
  2522. err = ARCHIVE_OK;
  2523. } else if (value_length == 23
  2524. && memcmp(p, "ISO-IR 10646 2000 UTF-8", 23) == 0) {
  2525. tar->pax_hdrcharset_utf8 = 1;
  2526. err = ARCHIVE_OK;
  2527. } else {
  2528. /* TODO: Unrecognized character set */
  2529. err = ARCHIVE_WARN;
  2530. }
  2531. } else {
  2532. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2533. "hdrcharset attribute is unreasonably large (%d bytes)",
  2534. (int)value_length);
  2535. err = ARCHIVE_WARN;
  2536. }
  2537. __archive_read_consume(a, value_length);
  2538. return (err);
  2539. }
  2540. break;
  2541. case 'l':
  2542. /* pax interchange doesn't distinguish hardlink vs. symlink. */
  2543. if (key_length == 8 && memcmp(key, "linkpath", 8) == 0) {
  2544. if (value_length > pathname_limit) {
  2545. *unconsumed += value_length;
  2546. err = ARCHIVE_WARN;
  2547. } else {
  2548. err = read_bytes_to_string(a, &tar->entry_linkpath, value_length, unconsumed);
  2549. }
  2550. return (err);
  2551. }
  2552. break;
  2553. case 'm':
  2554. if (key_length == 5 && memcmp(key, "mtime", 5) == 0) {
  2555. if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) {
  2556. archive_entry_set_mtime(entry, t, n);
  2557. }
  2558. return (err);
  2559. }
  2560. break;
  2561. case 'p':
  2562. if (key_length == 4 && memcmp(key, "path", 4) == 0) {
  2563. if (value_length > pathname_limit) {
  2564. *unconsumed += value_length;
  2565. err = ARCHIVE_WARN;
  2566. } else {
  2567. err = read_bytes_to_string(a, &(tar->entry_pathname), value_length, unconsumed);
  2568. }
  2569. return (err);
  2570. }
  2571. break;
  2572. case 'r':
  2573. /* POSIX has reserved 'realtime.*' */
  2574. break;
  2575. case 's':
  2576. /* POSIX has reserved 'security.*' */
  2577. /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */
  2578. if (key_length == 4 && memcmp(key, "size", 4) == 0) {
  2579. /* "size" is the size of the data in the entry. */
  2580. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2581. tar->pax_size = t;
  2582. tar->size_fields |= TAR_SIZE_PAX_SIZE;
  2583. }
  2584. else if (t == INT64_MAX) {
  2585. /* Note: pax_attr_read_number returns INT64_MAX on overflow or < 0 */
  2586. tar->entry_bytes_remaining = 0;
  2587. archive_set_error(&a->archive,
  2588. ARCHIVE_ERRNO_MISC,
  2589. "Tar size attribute overflow");
  2590. return (ARCHIVE_FATAL);
  2591. }
  2592. return (err);
  2593. }
  2594. break;
  2595. case 'u':
  2596. if (key_length == 3 && memcmp(key, "uid", 3) == 0) {
  2597. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2598. archive_entry_set_uid(entry, t);
  2599. }
  2600. return (err);
  2601. } else if (key_length == 5 && memcmp(key, "uname", 5) == 0) {
  2602. if (value_length > guname_limit) {
  2603. *unconsumed += value_length;
  2604. err = ARCHIVE_WARN;
  2605. } else {
  2606. err = read_bytes_to_string(a, &(tar->entry_uname), value_length, unconsumed);
  2607. }
  2608. return (err);
  2609. }
  2610. break;
  2611. }
  2612. /* Unrecognized key, just skip the entire value. */
  2613. __archive_read_consume(a, value_length);
  2614. return (err);
  2615. }
  2616. /*
  2617. * parse a decimal time value, which may include a fractional portion
  2618. */
  2619. static void
  2620. pax_time(const char *p, size_t length, int64_t *ps, long *pn)
  2621. {
  2622. char digit;
  2623. int64_t s;
  2624. unsigned long l;
  2625. int sign;
  2626. int64_t limit, last_digit_limit;
  2627. limit = INT64_MAX / 10;
  2628. last_digit_limit = INT64_MAX % 10;
  2629. if (length <= 0) {
  2630. *ps = 0;
  2631. return;
  2632. }
  2633. s = 0;
  2634. sign = 1;
  2635. if (*p == '-') {
  2636. sign = -1;
  2637. p++;
  2638. length--;
  2639. }
  2640. while (length > 0 && *p >= '0' && *p <= '9') {
  2641. digit = *p - '0';
  2642. if (s > limit ||
  2643. (s == limit && digit > last_digit_limit)) {
  2644. s = INT64_MAX;
  2645. break;
  2646. }
  2647. s = (s * 10) + digit;
  2648. ++p;
  2649. --length;
  2650. }
  2651. *ps = s * sign;
  2652. /* Calculate nanoseconds. */
  2653. *pn = 0;
  2654. if (length <= 0 || *p != '.')
  2655. return;
  2656. l = 100000000UL;
  2657. do {
  2658. ++p;
  2659. --length;
  2660. if (length > 0 && *p >= '0' && *p <= '9')
  2661. *pn += (*p - '0') * l;
  2662. else
  2663. break;
  2664. } while (l /= 10);
  2665. }
  2666. /*
  2667. * Parse GNU tar header
  2668. */
  2669. static int
  2670. header_gnutar(struct archive_read *a, struct tar *tar,
  2671. struct archive_entry *entry, const void *h, size_t *unconsumed)
  2672. {
  2673. const struct archive_entry_header_gnutar *header;
  2674. int64_t t;
  2675. int err = ARCHIVE_OK;
  2676. /*
  2677. * GNU header is like POSIX ustar, except 'prefix' is
  2678. * replaced with some other fields. This also means the
  2679. * filename is stored as in old-style archives.
  2680. */
  2681. /* Copy filename over (to ensure null termination). */
  2682. header = (const struct archive_entry_header_gnutar *)h;
  2683. const char *existing_pathname = archive_entry_pathname(entry);
  2684. if (existing_pathname == NULL || existing_pathname[0] == '\0') {
  2685. if (archive_entry_copy_pathname_l(entry,
  2686. header->name, sizeof(header->name), tar->sconv) != 0) {
  2687. err = set_conversion_failed_error(a, tar->sconv, "Pathname");
  2688. if (err == ARCHIVE_FATAL)
  2689. return (err);
  2690. }
  2691. }
  2692. /* Fields common to ustar and GNU */
  2693. /* XXX Can the following be factored out since it's common
  2694. * to ustar and gnu tar? Is it okay to move it down into
  2695. * header_common, perhaps? */
  2696. const char *existing_uname = archive_entry_uname(entry);
  2697. if (existing_uname == NULL || existing_uname[0] == '\0') {
  2698. if (archive_entry_copy_uname_l(entry,
  2699. header->uname, sizeof(header->uname), tar->sconv) != 0) {
  2700. err = set_conversion_failed_error(a, tar->sconv, "Uname");
  2701. if (err == ARCHIVE_FATAL)
  2702. return (err);
  2703. }
  2704. }
  2705. const char *existing_gname = archive_entry_gname(entry);
  2706. if (existing_gname == NULL || existing_gname[0] == '\0') {
  2707. if (archive_entry_copy_gname_l(entry,
  2708. header->gname, sizeof(header->gname), tar->sconv) != 0) {
  2709. err = set_conversion_failed_error(a, tar->sconv, "Gname");
  2710. if (err == ARCHIVE_FATAL)
  2711. return (err);
  2712. }
  2713. }
  2714. /* Parse out device numbers only for char and block specials */
  2715. if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
  2716. if (!archive_entry_rdev_is_set(entry)) {
  2717. archive_entry_set_rdevmajor(entry, (dev_t)
  2718. tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
  2719. archive_entry_set_rdevminor(entry, (dev_t)
  2720. tar_atol(header->rdevminor, sizeof(header->rdevminor)));
  2721. }
  2722. } else {
  2723. archive_entry_set_rdev(entry, 0);
  2724. }
  2725. /* Grab GNU-specific fields. */
  2726. if (!archive_entry_atime_is_set(entry)) {
  2727. t = tar_atol(header->atime, sizeof(header->atime));
  2728. if (t > 0)
  2729. archive_entry_set_atime(entry, t, 0);
  2730. }
  2731. if (!archive_entry_ctime_is_set(entry)) {
  2732. t = tar_atol(header->ctime, sizeof(header->ctime));
  2733. if (t > 0)
  2734. archive_entry_set_ctime(entry, t, 0);
  2735. }
  2736. if (header->realsize[0] != 0) {
  2737. /* Treat as a synonym for the pax GNU.sparse.realsize attr */
  2738. tar->GNU_sparse_realsize
  2739. = tar_atol(header->realsize, sizeof(header->realsize));
  2740. tar->size_fields |= TAR_SIZE_GNU_SPARSE_REALSIZE;
  2741. }
  2742. if (header->sparse[0].offset[0] != 0) {
  2743. if (gnu_sparse_old_read(a, tar, header, unconsumed)
  2744. != ARCHIVE_OK)
  2745. return (ARCHIVE_FATAL);
  2746. } else {
  2747. if (header->isextended[0] != 0) {
  2748. /* XXX WTF? XXX */
  2749. }
  2750. }
  2751. /* Grab fields common to all tar variants. */
  2752. err = header_common(a, tar, entry, h);
  2753. if (err == ARCHIVE_FATAL)
  2754. return (err);
  2755. tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
  2756. return (err);
  2757. }
  2758. static int
  2759. gnu_add_sparse_entry(struct archive_read *a, struct tar *tar,
  2760. int64_t offset, int64_t remaining)
  2761. {
  2762. struct sparse_block *p;
  2763. p = calloc(1, sizeof(*p));
  2764. if (p == NULL) {
  2765. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2766. return (ARCHIVE_FATAL);
  2767. }
  2768. if (tar->sparse_last != NULL)
  2769. tar->sparse_last->next = p;
  2770. else
  2771. tar->sparse_list = p;
  2772. tar->sparse_last = p;
  2773. if (remaining < 0 || offset < 0 || offset > INT64_MAX - remaining) {
  2774. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Malformed sparse map data");
  2775. return (ARCHIVE_FATAL);
  2776. }
  2777. p->offset = offset;
  2778. p->remaining = remaining;
  2779. return (ARCHIVE_OK);
  2780. }
  2781. static void
  2782. gnu_clear_sparse_list(struct tar *tar)
  2783. {
  2784. struct sparse_block *p;
  2785. while (tar->sparse_list != NULL) {
  2786. p = tar->sparse_list;
  2787. tar->sparse_list = p->next;
  2788. free(p);
  2789. }
  2790. tar->sparse_last = NULL;
  2791. }
  2792. /*
  2793. * GNU tar old-format sparse data.
  2794. *
  2795. * GNU old-format sparse data is stored in a fixed-field
  2796. * format. Offset/size values are 11-byte octal fields (same
  2797. * format as 'size' field in ustart header). These are
  2798. * stored in the header, allocating subsequent header blocks
  2799. * as needed. Extending the header in this way is a pretty
  2800. * severe POSIX violation; this design has earned GNU tar a
  2801. * lot of criticism.
  2802. */
  2803. static int
  2804. gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
  2805. const struct archive_entry_header_gnutar *header, size_t *unconsumed)
  2806. {
  2807. ssize_t bytes_read;
  2808. const void *data;
  2809. struct extended {
  2810. struct gnu_sparse sparse[21];
  2811. char isextended[1];
  2812. char padding[7];
  2813. };
  2814. const struct extended *ext;
  2815. if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK)
  2816. return (ARCHIVE_FATAL);
  2817. if (header->isextended[0] == 0)
  2818. return (ARCHIVE_OK);
  2819. do {
  2820. tar_flush_unconsumed(a, unconsumed);
  2821. data = __archive_read_ahead(a, 512, &bytes_read);
  2822. if (data == NULL) {
  2823. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2824. "Truncated tar archive "
  2825. "detected while reading sparse file data");
  2826. return (ARCHIVE_FATAL);
  2827. }
  2828. *unconsumed = 512;
  2829. ext = (const struct extended *)data;
  2830. if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK)
  2831. return (ARCHIVE_FATAL);
  2832. } while (ext->isextended[0] != 0);
  2833. if (tar->sparse_list != NULL)
  2834. tar->entry_offset = tar->sparse_list->offset;
  2835. return (ARCHIVE_OK);
  2836. }
  2837. static int
  2838. gnu_sparse_old_parse(struct archive_read *a, struct tar *tar,
  2839. const struct gnu_sparse *sparse, int length)
  2840. {
  2841. while (length > 0 && sparse->offset[0] != 0) {
  2842. if (gnu_add_sparse_entry(a, tar,
  2843. tar_atol(sparse->offset, sizeof(sparse->offset)),
  2844. tar_atol(sparse->numbytes, sizeof(sparse->numbytes)))
  2845. != ARCHIVE_OK)
  2846. return (ARCHIVE_FATAL);
  2847. sparse++;
  2848. length--;
  2849. }
  2850. return (ARCHIVE_OK);
  2851. }
  2852. /*
  2853. * GNU tar sparse format 0.0
  2854. *
  2855. * Beginning with GNU tar 1.15, sparse files are stored using
  2856. * information in the pax extended header. The GNU tar maintainers
  2857. * have gone through a number of variations in the process of working
  2858. * out this scheme; fortunately, they're all numbered.
  2859. *
  2860. * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the
  2861. * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to
  2862. * store offset/size for each block. The repeated instances of these
  2863. * latter fields violate the pax specification (which frowns on
  2864. * duplicate keys), so this format was quickly replaced.
  2865. */
  2866. /*
  2867. * GNU tar sparse format 0.1
  2868. *
  2869. * This version replaced the offset/numbytes attributes with
  2870. * a single "map" attribute that stored a list of integers. This
  2871. * format had two problems: First, the "map" attribute could be very
  2872. * long, which caused problems for some implementations. More
  2873. * importantly, the sparse data was lost when extracted by archivers
  2874. * that didn't recognize this extension.
  2875. */
  2876. static int
  2877. gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p, size_t length)
  2878. {
  2879. const char *e;
  2880. int64_t offset = -1, size = -1;
  2881. for (;;) {
  2882. e = p;
  2883. while (length > 0 && *e != ',') {
  2884. if (*e < '0' || *e > '9')
  2885. return (ARCHIVE_WARN);
  2886. e++;
  2887. length--;
  2888. }
  2889. if (offset < 0) {
  2890. offset = tar_atol10(p, e - p);
  2891. if (offset < 0)
  2892. return (ARCHIVE_WARN);
  2893. } else {
  2894. size = tar_atol10(p, e - p);
  2895. if (size < 0)
  2896. return (ARCHIVE_WARN);
  2897. if (gnu_add_sparse_entry(a, tar, offset, size)
  2898. != ARCHIVE_OK)
  2899. return (ARCHIVE_FATAL);
  2900. offset = -1;
  2901. }
  2902. if (length == 0)
  2903. return (ARCHIVE_OK);
  2904. p = e + 1;
  2905. length--;
  2906. }
  2907. }
  2908. /*
  2909. * GNU tar sparse format 1.0
  2910. *
  2911. * The idea: The offset/size data is stored as a series of base-10
  2912. * ASCII numbers prepended to the file data, so that dearchivers that
  2913. * don't support this format will extract the block map along with the
  2914. * data and a separate post-process can restore the sparseness.
  2915. *
  2916. * Unfortunately, GNU tar 1.16 had a bug that added unnecessary
  2917. * padding to the body of the file when using this format. GNU tar
  2918. * 1.17 corrected this bug without bumping the version number, so
  2919. * it's not possible to support both variants. This code supports
  2920. * the later variant at the expense of not supporting the former.
  2921. *
  2922. * This variant also introduced the GNU.sparse.major/GNU.sparse.minor attributes.
  2923. */
  2924. /*
  2925. * Read the next line from the input, and parse it as a decimal
  2926. * integer followed by '\n'. Returns positive integer value or
  2927. * negative on error.
  2928. */
  2929. static int64_t
  2930. gnu_sparse_10_atol(struct archive_read *a, struct tar *tar,
  2931. int64_t *remaining, size_t *unconsumed)
  2932. {
  2933. int64_t l, limit, last_digit_limit;
  2934. const char *p;
  2935. ssize_t bytes_read;
  2936. int base, digit;
  2937. base = 10;
  2938. limit = INT64_MAX / base;
  2939. last_digit_limit = INT64_MAX % base;
  2940. /*
  2941. * Skip any lines starting with '#'; GNU tar specs
  2942. * don't require this, but they should.
  2943. */
  2944. do {
  2945. bytes_read = readline(a, tar, &p,
  2946. (ssize_t)tar_min(*remaining, 100), unconsumed);
  2947. if (bytes_read <= 0)
  2948. return (ARCHIVE_FATAL);
  2949. *remaining -= bytes_read;
  2950. } while (p[0] == '#');
  2951. l = 0;
  2952. while (bytes_read > 0) {
  2953. if (*p == '\n')
  2954. return (l);
  2955. if (*p < '0' || *p >= '0' + base)
  2956. return (ARCHIVE_WARN);
  2957. digit = *p - '0';
  2958. if (l > limit || (l == limit && digit > last_digit_limit))
  2959. l = INT64_MAX; /* Truncate on overflow. */
  2960. else
  2961. l = (l * base) + digit;
  2962. p++;
  2963. bytes_read--;
  2964. }
  2965. /* TODO: Error message. */
  2966. return (ARCHIVE_WARN);
  2967. }
  2968. /*
  2969. * Returns length (in bytes) of the sparse data description
  2970. * that was read.
  2971. */
  2972. static ssize_t
  2973. gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed)
  2974. {
  2975. ssize_t bytes_read;
  2976. int entries;
  2977. int64_t offset, size, to_skip, remaining;
  2978. /* Clear out the existing sparse list. */
  2979. gnu_clear_sparse_list(tar);
  2980. remaining = tar->entry_bytes_remaining;
  2981. /* Parse entries. */
  2982. entries = (int)gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
  2983. if (entries < 0)
  2984. return (ARCHIVE_FATAL);
  2985. /* Parse the individual entries. */
  2986. while (entries-- > 0) {
  2987. /* Parse offset/size */
  2988. offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
  2989. if (offset < 0)
  2990. return (ARCHIVE_FATAL);
  2991. size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
  2992. if (size < 0)
  2993. return (ARCHIVE_FATAL);
  2994. /* Add a new sparse entry. */
  2995. if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK)
  2996. return (ARCHIVE_FATAL);
  2997. }
  2998. /* Skip rest of block... */
  2999. tar_flush_unconsumed(a, unconsumed);
  3000. bytes_read = (ssize_t)(tar->entry_bytes_remaining - remaining);
  3001. to_skip = 0x1ff & -bytes_read;
  3002. /* Fail if tar->entry_bytes_remaing would get negative */
  3003. if (to_skip > remaining)
  3004. return (ARCHIVE_FATAL);
  3005. if (to_skip != __archive_read_consume(a, to_skip))
  3006. return (ARCHIVE_FATAL);
  3007. return ((ssize_t)(bytes_read + to_skip));
  3008. }
  3009. /*
  3010. * Solaris pax extension for a sparse file. This is recorded with the
  3011. * data and hole pairs. The way recording sparse information by Solaris'
  3012. * pax simply indicates where data and sparse are, so the stored contents
  3013. * consist of both data and hole.
  3014. */
  3015. static int
  3016. pax_attribute_SUN_holesdata(struct archive_read *a, struct tar *tar,
  3017. struct archive_entry *entry, const char *p, size_t length)
  3018. {
  3019. const char *e;
  3020. int64_t start, end;
  3021. int hole = 1;
  3022. (void)entry; /* UNUSED */
  3023. end = 0;
  3024. if (length <= 0)
  3025. return (ARCHIVE_WARN);
  3026. if (*p == ' ') {
  3027. p++;
  3028. length--;
  3029. } else {
  3030. return (ARCHIVE_WARN);
  3031. }
  3032. for (;;) {
  3033. e = p;
  3034. while (length > 0 && *e != ' ') {
  3035. if (*e < '0' || *e > '9')
  3036. return (ARCHIVE_WARN);
  3037. e++;
  3038. length--;
  3039. }
  3040. start = end;
  3041. end = tar_atol10(p, e - p);
  3042. if (end < 0)
  3043. return (ARCHIVE_WARN);
  3044. if (start < end) {
  3045. if (gnu_add_sparse_entry(a, tar, start,
  3046. end - start) != ARCHIVE_OK)
  3047. return (ARCHIVE_FATAL);
  3048. tar->sparse_last->hole = hole;
  3049. }
  3050. if (length == 0 || *e == '\n') {
  3051. if (length == 0 && *e == '\n') {
  3052. return (ARCHIVE_OK);
  3053. } else {
  3054. return (ARCHIVE_WARN);
  3055. }
  3056. }
  3057. p = e + 1;
  3058. length--;
  3059. hole = hole == 0;
  3060. }
  3061. }
  3062. /*-
  3063. * Convert text->integer.
  3064. *
  3065. * Traditional tar formats (including POSIX) specify base-8 for
  3066. * all of the standard numeric fields. This is a significant limitation
  3067. * in practice:
  3068. * = file size is limited to 8GB
  3069. * = rdevmajor and rdevminor are limited to 21 bits
  3070. * = uid/gid are limited to 21 bits
  3071. *
  3072. * There are two workarounds for this:
  3073. * = pax extended headers, which use variable-length string fields
  3074. * = GNU tar and STAR both allow either base-8 or base-256 in
  3075. * most fields. The high bit is set to indicate base-256.
  3076. *
  3077. * On read, this implementation supports both extensions.
  3078. */
  3079. static int64_t
  3080. tar_atol(const char *p, size_t char_cnt)
  3081. {
  3082. /*
  3083. * Technically, GNU tar considers a field to be in base-256
  3084. * only if the first byte is 0xff or 0x80.
  3085. */
  3086. if (*p & 0x80)
  3087. return (tar_atol256(p, char_cnt));
  3088. return (tar_atol8(p, char_cnt));
  3089. }
  3090. /*
  3091. * Note that this implementation does not (and should not!) obey
  3092. * locale settings; you cannot simply substitute strtol here, since
  3093. * it does obey locale.
  3094. */
  3095. static int64_t
  3096. tar_atol_base_n(const char *p, size_t char_cnt, int base)
  3097. {
  3098. int64_t l, maxval, limit, last_digit_limit;
  3099. int digit, sign;
  3100. maxval = INT64_MAX;
  3101. limit = INT64_MAX / base;
  3102. last_digit_limit = INT64_MAX % base;
  3103. /* the pointer will not be dereferenced if char_cnt is zero
  3104. * due to the way the && operator is evaluated.
  3105. */
  3106. while (char_cnt != 0 && (*p == ' ' || *p == '\t')) {
  3107. p++;
  3108. char_cnt--;
  3109. }
  3110. sign = 1;
  3111. if (char_cnt != 0 && *p == '-') {
  3112. sign = -1;
  3113. p++;
  3114. char_cnt--;
  3115. maxval = INT64_MIN;
  3116. limit = -(INT64_MIN / base);
  3117. last_digit_limit = -(INT64_MIN % base);
  3118. }
  3119. l = 0;
  3120. if (char_cnt != 0) {
  3121. digit = *p - '0';
  3122. while (digit >= 0 && digit < base && char_cnt != 0) {
  3123. if (l>limit || (l == limit && digit >= last_digit_limit)) {
  3124. return maxval; /* Truncate on overflow. */
  3125. }
  3126. l = (l * base) + digit;
  3127. digit = *++p - '0';
  3128. char_cnt--;
  3129. }
  3130. }
  3131. return (sign < 0) ? -l : l;
  3132. }
  3133. static int64_t
  3134. tar_atol8(const char *p, size_t char_cnt)
  3135. {
  3136. return tar_atol_base_n(p, char_cnt, 8);
  3137. }
  3138. static int64_t
  3139. tar_atol10(const char *p, size_t char_cnt)
  3140. {
  3141. return tar_atol_base_n(p, char_cnt, 10);
  3142. }
  3143. /*
  3144. * Parse a base-256 integer. This is just a variable-length
  3145. * twos-complement signed binary value in big-endian order, except
  3146. * that the high-order bit is ignored. The values here can be up to
  3147. * 12 bytes, so we need to be careful about overflowing 64-bit
  3148. * (8-byte) integers.
  3149. *
  3150. * This code unashamedly assumes that the local machine uses 8-bit
  3151. * bytes and twos-complement arithmetic.
  3152. */
  3153. static int64_t
  3154. tar_atol256(const char *_p, size_t char_cnt)
  3155. {
  3156. uint64_t l;
  3157. const unsigned char *p = (const unsigned char *)_p;
  3158. unsigned char c, neg;
  3159. /* Extend 7-bit 2s-comp to 8-bit 2s-comp, decide sign. */
  3160. c = *p;
  3161. if (c & 0x40) {
  3162. neg = 0xff;
  3163. c |= 0x80;
  3164. l = ~ARCHIVE_LITERAL_ULL(0);
  3165. } else {
  3166. neg = 0;
  3167. c &= 0x7f;
  3168. l = 0;
  3169. }
  3170. /* If more than 8 bytes, check that we can ignore
  3171. * high-order bits without overflow. */
  3172. while (char_cnt > sizeof(int64_t)) {
  3173. --char_cnt;
  3174. if (c != neg)
  3175. return neg ? INT64_MIN : INT64_MAX;
  3176. c = *++p;
  3177. }
  3178. /* c is first byte that fits; if sign mismatch, return overflow */
  3179. if ((c ^ neg) & 0x80) {
  3180. return neg ? INT64_MIN : INT64_MAX;
  3181. }
  3182. /* Accumulate remaining bytes. */
  3183. while (--char_cnt > 0) {
  3184. l = (l << 8) | c;
  3185. c = *++p;
  3186. }
  3187. l = (l << 8) | c;
  3188. /* Return signed twos-complement value. */
  3189. return (int64_t)(l);
  3190. }
  3191. /*
  3192. * Returns length of line (including trailing newline)
  3193. * or negative on error. 'start' argument is updated to
  3194. * point to first character of line. This avoids copying
  3195. * when possible.
  3196. */
  3197. static ssize_t
  3198. readline(struct archive_read *a, struct tar *tar, const char **start,
  3199. ssize_t limit, size_t *unconsumed)
  3200. {
  3201. ssize_t bytes_read;
  3202. ssize_t total_size = 0;
  3203. const void *t;
  3204. const char *s;
  3205. void *p;
  3206. tar_flush_unconsumed(a, unconsumed);
  3207. t = __archive_read_ahead(a, 1, &bytes_read);
  3208. if (bytes_read <= 0 || t == NULL)
  3209. return (ARCHIVE_FATAL);
  3210. s = t; /* Start of line? */
  3211. p = memchr(t, '\n', bytes_read);
  3212. /* If we found '\n' in the read buffer, return pointer to that. */
  3213. if (p != NULL) {
  3214. bytes_read = 1 + ((const char *)p) - s;
  3215. if (bytes_read > limit) {
  3216. archive_set_error(&a->archive,
  3217. ARCHIVE_ERRNO_FILE_FORMAT,
  3218. "Line too long");
  3219. return (ARCHIVE_FATAL);
  3220. }
  3221. *unconsumed = bytes_read;
  3222. *start = s;
  3223. return (bytes_read);
  3224. }
  3225. *unconsumed = bytes_read;
  3226. /* Otherwise, we need to accumulate in a line buffer. */
  3227. for (;;) {
  3228. if (total_size + bytes_read > limit) {
  3229. archive_set_error(&a->archive,
  3230. ARCHIVE_ERRNO_FILE_FORMAT,
  3231. "Line too long");
  3232. return (ARCHIVE_FATAL);
  3233. }
  3234. if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) {
  3235. archive_set_error(&a->archive, ENOMEM,
  3236. "Can't allocate working buffer");
  3237. return (ARCHIVE_FATAL);
  3238. }
  3239. memcpy(tar->line.s + total_size, t, bytes_read);
  3240. tar_flush_unconsumed(a, unconsumed);
  3241. total_size += bytes_read;
  3242. /* If we found '\n', clean up and return. */
  3243. if (p != NULL) {
  3244. *start = tar->line.s;
  3245. return (total_size);
  3246. }
  3247. /* Read some more. */
  3248. t = __archive_read_ahead(a, 1, &bytes_read);
  3249. if (bytes_read <= 0 || t == NULL)
  3250. return (ARCHIVE_FATAL);
  3251. s = t; /* Start of line? */
  3252. p = memchr(t, '\n', bytes_read);
  3253. /* If we found '\n', trim the read. */
  3254. if (p != NULL) {
  3255. bytes_read = 1 + ((const char *)p) - s;
  3256. }
  3257. *unconsumed = bytes_read;
  3258. }
  3259. }
  3260. /*
  3261. * base64_decode - Base64 decode
  3262. *
  3263. * This accepts most variations of base-64 encoding, including:
  3264. * * with or without line breaks
  3265. * * with or without the final group padded with '=' or '_' characters
  3266. * (The most economical Base-64 variant does not pad the last group and
  3267. * omits line breaks; RFC1341 used for MIME requires both.)
  3268. */
  3269. static char *
  3270. base64_decode(const char *s, size_t len, size_t *out_len)
  3271. {
  3272. static const unsigned char digits[64] = {
  3273. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
  3274. 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
  3275. 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
  3276. 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
  3277. '4','5','6','7','8','9','+','/' };
  3278. static unsigned char decode_table[128];
  3279. char *out, *d;
  3280. const unsigned char *src = (const unsigned char *)s;
  3281. /* If the decode table is not yet initialized, prepare it. */
  3282. if (decode_table[digits[1]] != 1) {
  3283. unsigned i;
  3284. memset(decode_table, 0xff, sizeof(decode_table));
  3285. for (i = 0; i < sizeof(digits); i++)
  3286. decode_table[digits[i]] = i;
  3287. }
  3288. /* Allocate enough space to hold the entire output. */
  3289. /* Note that we may not use all of this... */
  3290. out = malloc(len - len / 4 + 1);
  3291. if (out == NULL) {
  3292. *out_len = 0;
  3293. return (NULL);
  3294. }
  3295. d = out;
  3296. while (len > 0) {
  3297. /* Collect the next group of (up to) four characters. */
  3298. int v = 0;
  3299. int group_size = 0;
  3300. while (group_size < 4 && len > 0) {
  3301. /* '=' or '_' padding indicates final group. */
  3302. if (*src == '=' || *src == '_') {
  3303. len = 0;
  3304. break;
  3305. }
  3306. /* Skip illegal characters (including line breaks) */
  3307. if (*src > 127 || *src < 32
  3308. || decode_table[*src] == 0xff) {
  3309. len--;
  3310. src++;
  3311. continue;
  3312. }
  3313. v <<= 6;
  3314. v |= decode_table[*src++];
  3315. len --;
  3316. group_size++;
  3317. }
  3318. /* Align a short group properly. */
  3319. v <<= 6 * (4 - group_size);
  3320. /* Unpack the group we just collected. */
  3321. switch (group_size) {
  3322. case 4: d[2] = v & 0xff;
  3323. /* FALLTHROUGH */
  3324. case 3: d[1] = (v >> 8) & 0xff;
  3325. /* FALLTHROUGH */
  3326. case 2: d[0] = (v >> 16) & 0xff;
  3327. break;
  3328. case 1: /* this is invalid! */
  3329. break;
  3330. }
  3331. d += group_size * 3 / 4;
  3332. }
  3333. *out_len = d - out;
  3334. return (out);
  3335. }
  3336. static char *
  3337. url_decode(const char *in, size_t length)
  3338. {
  3339. char *out, *d;
  3340. const char *s;
  3341. out = malloc(length + 1);
  3342. if (out == NULL)
  3343. return (NULL);
  3344. for (s = in, d = out; length > 0 && *s != '\0'; ) {
  3345. if (s[0] == '%' && length > 2) {
  3346. /* Try to convert % escape */
  3347. int digit1 = tohex(s[1]);
  3348. int digit2 = tohex(s[2]);
  3349. if (digit1 >= 0 && digit2 >= 0) {
  3350. /* Looks good, consume three chars */
  3351. s += 3;
  3352. length -= 3;
  3353. /* Convert output */
  3354. *d++ = ((digit1 << 4) | digit2);
  3355. continue;
  3356. }
  3357. /* Else fall through and treat '%' as normal char */
  3358. }
  3359. *d++ = *s++;
  3360. --length;
  3361. }
  3362. *d = '\0';
  3363. return (out);
  3364. }
  3365. static int
  3366. tohex(int c)
  3367. {
  3368. if (c >= '0' && c <= '9')
  3369. return (c - '0');
  3370. else if (c >= 'A' && c <= 'F')
  3371. return (c - 'A' + 10);
  3372. else if (c >= 'a' && c <= 'f')
  3373. return (c - 'a' + 10);
  3374. else
  3375. return (-1);
  3376. }