sess.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * SMB/CIFS session setup handling routines
  5. *
  6. * Copyright (c) International Business Machines Corp., 2006, 2009
  7. * Author(s): Steve French (sfrench@us.ibm.com)
  8. *
  9. */
  10. #include "cifspdu.h"
  11. #include "cifsglob.h"
  12. #include "cifsproto.h"
  13. #include "cifs_unicode.h"
  14. #include "cifs_debug.h"
  15. #include "ntlmssp.h"
  16. #include "nterr.h"
  17. #include <linux/utsname.h>
  18. #include <linux/slab.h>
  19. #include <linux/version.h>
  20. #include "cifsfs.h"
  21. #include "cifs_spnego.h"
  22. #include "smb2proto.h"
  23. #include "fs_context.h"
  24. static int
  25. cifs_ses_add_channel(struct cifs_ses *ses,
  26. struct cifs_server_iface *iface);
  27. bool
  28. is_server_using_iface(struct TCP_Server_Info *server,
  29. struct cifs_server_iface *iface)
  30. {
  31. struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
  32. struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
  33. struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
  34. struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
  35. if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
  36. return false;
  37. if (server->dstaddr.ss_family == AF_INET) {
  38. if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
  39. return false;
  40. } else if (server->dstaddr.ss_family == AF_INET6) {
  41. if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
  42. sizeof(i6->sin6_addr)) != 0)
  43. return false;
  44. } else {
  45. /* unknown family.. */
  46. return false;
  47. }
  48. return true;
  49. }
  50. bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
  51. {
  52. int i;
  53. spin_lock(&ses->chan_lock);
  54. for (i = 0; i < ses->chan_count; i++) {
  55. if (ses->chans[i].iface == iface) {
  56. spin_unlock(&ses->chan_lock);
  57. return true;
  58. }
  59. }
  60. spin_unlock(&ses->chan_lock);
  61. return false;
  62. }
  63. /* channel helper functions. assumed that chan_lock is held by caller. */
  64. int
  65. cifs_ses_get_chan_index(struct cifs_ses *ses,
  66. struct TCP_Server_Info *server)
  67. {
  68. unsigned int i;
  69. /* if the channel is waiting for termination */
  70. if (server && server->terminate)
  71. return CIFS_INVAL_CHAN_INDEX;
  72. for (i = 0; i < ses->chan_count; i++) {
  73. if (ses->chans[i].server == server)
  74. return i;
  75. }
  76. /* If we didn't find the channel, it is likely a bug */
  77. if (server)
  78. cifs_dbg(VFS, "unable to get chan index for server: 0x%llx",
  79. server->conn_id);
  80. return CIFS_INVAL_CHAN_INDEX;
  81. }
  82. void
  83. cifs_chan_set_in_reconnect(struct cifs_ses *ses,
  84. struct TCP_Server_Info *server)
  85. {
  86. int chan_index = cifs_ses_get_chan_index(ses, server);
  87. if (chan_index == CIFS_INVAL_CHAN_INDEX)
  88. return;
  89. ses->chans[chan_index].in_reconnect = true;
  90. }
  91. void
  92. cifs_chan_clear_in_reconnect(struct cifs_ses *ses,
  93. struct TCP_Server_Info *server)
  94. {
  95. unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
  96. if (chan_index == CIFS_INVAL_CHAN_INDEX)
  97. return;
  98. ses->chans[chan_index].in_reconnect = false;
  99. }
  100. void
  101. cifs_chan_set_need_reconnect(struct cifs_ses *ses,
  102. struct TCP_Server_Info *server)
  103. {
  104. unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
  105. if (chan_index == CIFS_INVAL_CHAN_INDEX)
  106. return;
  107. set_bit(chan_index, &ses->chans_need_reconnect);
  108. cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n",
  109. chan_index, ses->chans_need_reconnect);
  110. }
  111. void
  112. cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
  113. struct TCP_Server_Info *server)
  114. {
  115. unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
  116. if (chan_index == CIFS_INVAL_CHAN_INDEX)
  117. return;
  118. clear_bit(chan_index, &ses->chans_need_reconnect);
  119. cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n",
  120. chan_index, ses->chans_need_reconnect);
  121. }
  122. bool
  123. cifs_chan_needs_reconnect(struct cifs_ses *ses,
  124. struct TCP_Server_Info *server)
  125. {
  126. unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
  127. if (chan_index == CIFS_INVAL_CHAN_INDEX)
  128. return true; /* err on the safer side */
  129. return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index);
  130. }
  131. bool
  132. cifs_chan_is_iface_active(struct cifs_ses *ses,
  133. struct TCP_Server_Info *server)
  134. {
  135. unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
  136. if (chan_index == CIFS_INVAL_CHAN_INDEX)
  137. return true; /* err on the safer side */
  138. return ses->chans[chan_index].iface &&
  139. ses->chans[chan_index].iface->is_active;
  140. }
  141. /* returns number of channels added */
  142. int cifs_try_adding_channels(struct cifs_ses *ses)
  143. {
  144. struct TCP_Server_Info *server = ses->server;
  145. int old_chan_count, new_chan_count;
  146. int left;
  147. int rc = 0;
  148. int tries = 0;
  149. size_t iface_weight = 0, iface_min_speed = 0;
  150. struct cifs_server_iface *iface = NULL, *niface = NULL;
  151. struct cifs_server_iface *last_iface = NULL;
  152. spin_lock(&ses->chan_lock);
  153. new_chan_count = old_chan_count = ses->chan_count;
  154. left = ses->chan_max - ses->chan_count;
  155. if (left <= 0) {
  156. spin_unlock(&ses->chan_lock);
  157. cifs_dbg(FYI,
  158. "ses already at max_channels (%zu), nothing to open\n",
  159. ses->chan_max);
  160. return 0;
  161. }
  162. if (server->dialect < SMB30_PROT_ID) {
  163. spin_unlock(&ses->chan_lock);
  164. cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
  165. return 0;
  166. }
  167. if (!(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
  168. spin_unlock(&ses->chan_lock);
  169. cifs_server_dbg(VFS, "no multichannel support\n");
  170. return 0;
  171. }
  172. spin_unlock(&ses->chan_lock);
  173. while (left > 0) {
  174. tries++;
  175. if (tries > 3*ses->chan_max) {
  176. cifs_dbg(VFS, "too many channel open attempts (%d channels left to open)\n",
  177. left);
  178. break;
  179. }
  180. spin_lock(&ses->iface_lock);
  181. if (!ses->iface_count) {
  182. spin_unlock(&ses->iface_lock);
  183. cifs_dbg(ONCE, "server %s does not advertise interfaces\n",
  184. ses->server->hostname);
  185. break;
  186. }
  187. if (!iface)
  188. iface = list_first_entry(&ses->iface_list, struct cifs_server_iface,
  189. iface_head);
  190. last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
  191. iface_head);
  192. iface_min_speed = last_iface->speed;
  193. list_for_each_entry_safe_from(iface, niface, &ses->iface_list,
  194. iface_head) {
  195. /* do not mix rdma and non-rdma interfaces */
  196. if (iface->rdma_capable != ses->server->rdma)
  197. continue;
  198. /* skip ifaces that are unusable */
  199. if (!iface->is_active ||
  200. (is_ses_using_iface(ses, iface) &&
  201. !iface->rss_capable))
  202. continue;
  203. /* check if we already allocated enough channels */
  204. iface_weight = iface->speed / iface_min_speed;
  205. if (iface->weight_fulfilled >= iface_weight)
  206. continue;
  207. /* take ref before unlock */
  208. kref_get(&iface->refcount);
  209. spin_unlock(&ses->iface_lock);
  210. rc = cifs_ses_add_channel(ses, iface);
  211. spin_lock(&ses->iface_lock);
  212. if (rc) {
  213. cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n",
  214. &iface->sockaddr,
  215. rc);
  216. kref_put(&iface->refcount, release_iface);
  217. /* failure to add chan should increase weight */
  218. iface->weight_fulfilled++;
  219. continue;
  220. }
  221. iface->num_channels++;
  222. iface->weight_fulfilled++;
  223. cifs_dbg(VFS, "successfully opened new channel on iface:%pIS\n",
  224. &iface->sockaddr);
  225. break;
  226. }
  227. /* reached end of list. reset weight_fulfilled and start over */
  228. if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
  229. list_for_each_entry(iface, &ses->iface_list, iface_head)
  230. iface->weight_fulfilled = 0;
  231. spin_unlock(&ses->iface_lock);
  232. iface = NULL;
  233. continue;
  234. }
  235. spin_unlock(&ses->iface_lock);
  236. left--;
  237. new_chan_count++;
  238. }
  239. return new_chan_count - old_chan_count;
  240. }
  241. /*
  242. * called when multichannel is disabled by the server.
  243. * this always gets called from smb2_reconnect
  244. * and cannot get called in parallel threads.
  245. */
  246. void
  247. cifs_disable_secondary_channels(struct cifs_ses *ses)
  248. {
  249. int i, chan_count;
  250. struct TCP_Server_Info *server;
  251. struct cifs_server_iface *iface;
  252. spin_lock(&ses->chan_lock);
  253. chan_count = ses->chan_count;
  254. if (chan_count == 1)
  255. goto done;
  256. ses->chan_count = 1;
  257. /* for all secondary channels reset the need reconnect bit */
  258. ses->chans_need_reconnect &= 1;
  259. for (i = 1; i < chan_count; i++) {
  260. iface = ses->chans[i].iface;
  261. server = ses->chans[i].server;
  262. /*
  263. * remove these references first, since we need to unlock
  264. * the chan_lock here, since iface_lock is a higher lock
  265. */
  266. ses->chans[i].iface = NULL;
  267. ses->chans[i].server = NULL;
  268. spin_unlock(&ses->chan_lock);
  269. if (iface) {
  270. spin_lock(&ses->iface_lock);
  271. iface->num_channels--;
  272. if (iface->weight_fulfilled)
  273. iface->weight_fulfilled--;
  274. kref_put(&iface->refcount, release_iface);
  275. spin_unlock(&ses->iface_lock);
  276. }
  277. if (server) {
  278. if (!server->terminate) {
  279. server->terminate = true;
  280. cifs_signal_cifsd_for_reconnect(server, false);
  281. }
  282. cifs_put_tcp_session(server, false);
  283. }
  284. spin_lock(&ses->chan_lock);
  285. }
  286. done:
  287. spin_unlock(&ses->chan_lock);
  288. }
  289. /*
  290. * update the iface for the channel if necessary.
  291. * Must be called with chan_lock held.
  292. */
  293. void
  294. cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
  295. {
  296. unsigned int chan_index;
  297. size_t iface_weight = 0, iface_min_speed = 0;
  298. struct cifs_server_iface *iface = NULL;
  299. struct cifs_server_iface *old_iface = NULL;
  300. struct cifs_server_iface *last_iface = NULL;
  301. struct sockaddr_storage ss;
  302. int retry = 0;
  303. spin_lock(&ses->chan_lock);
  304. chan_index = cifs_ses_get_chan_index(ses, server);
  305. if (chan_index == CIFS_INVAL_CHAN_INDEX) {
  306. spin_unlock(&ses->chan_lock);
  307. return;
  308. }
  309. if (ses->chans[chan_index].iface) {
  310. old_iface = ses->chans[chan_index].iface;
  311. if (old_iface->is_active) {
  312. spin_unlock(&ses->chan_lock);
  313. return;
  314. }
  315. }
  316. spin_unlock(&ses->chan_lock);
  317. spin_lock(&server->srv_lock);
  318. ss = server->dstaddr;
  319. spin_unlock(&server->srv_lock);
  320. spin_lock(&ses->iface_lock);
  321. if (!ses->iface_count) {
  322. spin_unlock(&ses->iface_lock);
  323. cifs_dbg(ONCE, "server %s does not advertise interfaces\n", ses->server->hostname);
  324. return;
  325. }
  326. try_again:
  327. last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
  328. iface_head);
  329. iface_min_speed = last_iface->speed;
  330. /* then look for a new one */
  331. list_for_each_entry(iface, &ses->iface_list, iface_head) {
  332. if (!chan_index) {
  333. /* if we're trying to get the updated iface for primary channel */
  334. if (!cifs_match_ipaddr((struct sockaddr *) &ss,
  335. (struct sockaddr *) &iface->sockaddr))
  336. continue;
  337. kref_get(&iface->refcount);
  338. break;
  339. }
  340. /* do not mix rdma and non-rdma interfaces */
  341. if (iface->rdma_capable != server->rdma)
  342. continue;
  343. if (!iface->is_active ||
  344. (is_ses_using_iface(ses, iface) &&
  345. !iface->rss_capable)) {
  346. continue;
  347. }
  348. /* check if we already allocated enough channels */
  349. iface_weight = iface->speed / iface_min_speed;
  350. if (iface->weight_fulfilled >= iface_weight)
  351. continue;
  352. kref_get(&iface->refcount);
  353. break;
  354. }
  355. if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
  356. list_for_each_entry(iface, &ses->iface_list, iface_head)
  357. iface->weight_fulfilled = 0;
  358. /* see if it can be satisfied in second attempt */
  359. if (!retry++)
  360. goto try_again;
  361. iface = NULL;
  362. cifs_dbg(FYI, "unable to find a suitable iface\n");
  363. }
  364. if (!iface) {
  365. if (!chan_index)
  366. cifs_dbg(FYI, "unable to get the interface matching: %pIS\n",
  367. &ss);
  368. else {
  369. cifs_dbg(FYI, "unable to find another interface to replace: %pIS\n",
  370. &old_iface->sockaddr);
  371. }
  372. spin_unlock(&ses->iface_lock);
  373. return;
  374. }
  375. /* now drop the ref to the current iface */
  376. if (old_iface) {
  377. cifs_dbg(FYI, "replacing iface: %pIS with %pIS\n",
  378. &old_iface->sockaddr,
  379. &iface->sockaddr);
  380. old_iface->num_channels--;
  381. if (old_iface->weight_fulfilled)
  382. old_iface->weight_fulfilled--;
  383. iface->num_channels++;
  384. iface->weight_fulfilled++;
  385. kref_put(&old_iface->refcount, release_iface);
  386. } else if (!chan_index) {
  387. /* special case: update interface for primary channel */
  388. cifs_dbg(FYI, "referencing primary channel iface: %pIS\n",
  389. &iface->sockaddr);
  390. iface->num_channels++;
  391. iface->weight_fulfilled++;
  392. }
  393. spin_unlock(&ses->iface_lock);
  394. spin_lock(&ses->chan_lock);
  395. chan_index = cifs_ses_get_chan_index(ses, server);
  396. if (chan_index == CIFS_INVAL_CHAN_INDEX) {
  397. spin_unlock(&ses->chan_lock);
  398. return;
  399. }
  400. ses->chans[chan_index].iface = iface;
  401. spin_unlock(&ses->chan_lock);
  402. spin_lock(&server->srv_lock);
  403. memcpy(&server->dstaddr, &iface->sockaddr, sizeof(server->dstaddr));
  404. spin_unlock(&server->srv_lock);
  405. }
  406. static int
  407. cifs_ses_add_channel(struct cifs_ses *ses,
  408. struct cifs_server_iface *iface)
  409. {
  410. struct TCP_Server_Info *chan_server;
  411. struct cifs_chan *chan;
  412. struct smb3_fs_context *ctx;
  413. static const char unc_fmt[] = "\\%s\\foo";
  414. struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
  415. struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
  416. size_t len;
  417. int rc;
  418. unsigned int xid = get_xid();
  419. if (iface->sockaddr.ss_family == AF_INET)
  420. cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
  421. ses, iface->speed, iface->rdma_capable ? "yes" : "no",
  422. &ipv4->sin_addr);
  423. else
  424. cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
  425. ses, iface->speed, iface->rdma_capable ? "yes" : "no",
  426. &ipv6->sin6_addr);
  427. /*
  428. * Setup a ctx with mostly the same info as the existing
  429. * session and overwrite it with the requested iface data.
  430. *
  431. * We need to setup at least the fields used for negprot and
  432. * sesssetup.
  433. *
  434. * We only need the ctx here, so we can reuse memory from
  435. * the session and server without caring about memory
  436. * management.
  437. */
  438. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  439. if (!ctx) {
  440. rc = -ENOMEM;
  441. goto out_free_xid;
  442. }
  443. /* Always make new connection for now (TODO?) */
  444. ctx->nosharesock = true;
  445. /* Auth */
  446. ctx->domainauto = ses->domainAuto;
  447. ctx->domainname = ses->domainName;
  448. ctx->server_hostname = ses->server->hostname;
  449. ctx->username = ses->user_name;
  450. ctx->password = ses->password;
  451. ctx->sectype = ses->sectype;
  452. ctx->sign = ses->sign;
  453. /* UNC and paths */
  454. /* XXX: Use ses->server->hostname? */
  455. len = sizeof(unc_fmt) + SERVER_NAME_LEN_WITH_NULL;
  456. ctx->UNC = kzalloc(len, GFP_KERNEL);
  457. if (!ctx->UNC) {
  458. rc = -ENOMEM;
  459. goto out_free_ctx;
  460. }
  461. scnprintf(ctx->UNC, len, unc_fmt, ses->ip_addr);
  462. ctx->prepath = "";
  463. /* Reuse same version as master connection */
  464. ctx->vals = ses->server->vals;
  465. ctx->ops = ses->server->ops;
  466. ctx->noblocksnd = ses->server->noblocksnd;
  467. ctx->noautotune = ses->server->noautotune;
  468. ctx->sockopt_tcp_nodelay = ses->server->tcp_nodelay;
  469. ctx->echo_interval = ses->server->echo_interval / HZ;
  470. ctx->max_credits = ses->server->max_credits;
  471. ctx->min_offload = ses->server->min_offload;
  472. ctx->compress = ses->server->compression.requested;
  473. ctx->dfs_conn = ses->server->dfs_conn;
  474. ctx->ignore_signature = ses->server->ignore_signature;
  475. ctx->leaf_fullpath = ses->server->leaf_fullpath;
  476. ctx->rootfs = ses->server->noblockcnt;
  477. ctx->retrans = ses->server->retrans;
  478. /*
  479. * This will be used for encoding/decoding user/domain/pw
  480. * during sess setup auth.
  481. */
  482. ctx->local_nls = ses->local_nls;
  483. /* Use RDMA if possible */
  484. ctx->rdma = iface->rdma_capable;
  485. memcpy(&ctx->dstaddr, &iface->sockaddr, sizeof(ctx->dstaddr));
  486. /* reuse master con client guid */
  487. memcpy(&ctx->client_guid, ses->server->client_guid,
  488. sizeof(ctx->client_guid));
  489. ctx->use_client_guid = true;
  490. chan_server = cifs_get_tcp_session(ctx, ses->server);
  491. spin_lock(&ses->chan_lock);
  492. chan = &ses->chans[ses->chan_count];
  493. chan->server = chan_server;
  494. if (IS_ERR(chan->server)) {
  495. rc = PTR_ERR(chan->server);
  496. chan->server = NULL;
  497. spin_unlock(&ses->chan_lock);
  498. goto out;
  499. }
  500. chan->iface = iface;
  501. ses->chan_count++;
  502. atomic_set(&ses->chan_seq, 0);
  503. /* Mark this channel as needing connect/setup */
  504. cifs_chan_set_need_reconnect(ses, chan->server);
  505. spin_unlock(&ses->chan_lock);
  506. mutex_lock(&ses->session_mutex);
  507. /*
  508. * We need to allocate the server crypto now as we will need
  509. * to sign packets before we generate the channel signing key
  510. * (we sign with the session key)
  511. */
  512. rc = smb311_crypto_shash_allocate(chan->server);
  513. if (rc) {
  514. cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
  515. mutex_unlock(&ses->session_mutex);
  516. goto out;
  517. }
  518. rc = cifs_negotiate_protocol(xid, ses, chan->server);
  519. if (!rc)
  520. rc = cifs_setup_session(xid, ses, chan->server, ses->local_nls);
  521. mutex_unlock(&ses->session_mutex);
  522. out:
  523. if (rc && chan->server) {
  524. cifs_put_tcp_session(chan->server, 0);
  525. spin_lock(&ses->chan_lock);
  526. /* we rely on all bits beyond chan_count to be clear */
  527. cifs_chan_clear_need_reconnect(ses, chan->server);
  528. ses->chan_count--;
  529. /*
  530. * chan_count should never reach 0 as at least the primary
  531. * channel is always allocated
  532. */
  533. WARN_ON(ses->chan_count < 1);
  534. spin_unlock(&ses->chan_lock);
  535. }
  536. kfree(ctx->UNC);
  537. out_free_ctx:
  538. kfree(ctx);
  539. out_free_xid:
  540. free_xid(xid);
  541. return rc;
  542. }
  543. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  544. static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
  545. struct TCP_Server_Info *server,
  546. SESSION_SETUP_ANDX *pSMB)
  547. {
  548. __u32 capabilities = 0;
  549. /* init fields common to all four types of SessSetup */
  550. /* Note that offsets for first seven fields in req struct are same */
  551. /* in CIFS Specs so does not matter which of 3 forms of struct */
  552. /* that we use in next few lines */
  553. /* Note that header is initialized to zero in header_assemble */
  554. pSMB->req.AndXCommand = 0xFF;
  555. pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
  556. CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
  557. USHRT_MAX));
  558. pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
  559. pSMB->req.VcNumber = cpu_to_le16(1);
  560. pSMB->req.SessionKey = server->session_key_id;
  561. /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
  562. /* BB verify whether signing required on neg or just auth frame (and NTLM case) */
  563. capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
  564. CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
  565. if (server->sign)
  566. pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  567. if (ses->capabilities & CAP_UNICODE) {
  568. pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
  569. capabilities |= CAP_UNICODE;
  570. }
  571. if (ses->capabilities & CAP_STATUS32) {
  572. pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
  573. capabilities |= CAP_STATUS32;
  574. }
  575. if (ses->capabilities & CAP_DFS) {
  576. pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
  577. capabilities |= CAP_DFS;
  578. }
  579. if (ses->capabilities & CAP_UNIX)
  580. capabilities |= CAP_UNIX;
  581. return capabilities;
  582. }
  583. static void
  584. unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
  585. {
  586. char *bcc_ptr = *pbcc_area;
  587. int bytes_ret = 0;
  588. /* Copy OS version */
  589. bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
  590. nls_cp);
  591. bcc_ptr += 2 * bytes_ret;
  592. bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
  593. 32, nls_cp);
  594. bcc_ptr += 2 * bytes_ret;
  595. bcc_ptr += 2; /* trailing null */
  596. bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
  597. 32, nls_cp);
  598. bcc_ptr += 2 * bytes_ret;
  599. bcc_ptr += 2; /* trailing null */
  600. *pbcc_area = bcc_ptr;
  601. }
  602. static void
  603. ascii_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
  604. {
  605. char *bcc_ptr = *pbcc_area;
  606. strcpy(bcc_ptr, "Linux version ");
  607. bcc_ptr += strlen("Linux version ");
  608. strcpy(bcc_ptr, init_utsname()->release);
  609. bcc_ptr += strlen(init_utsname()->release) + 1;
  610. strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
  611. bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
  612. *pbcc_area = bcc_ptr;
  613. }
  614. static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
  615. const struct nls_table *nls_cp)
  616. {
  617. char *bcc_ptr = *pbcc_area;
  618. int bytes_ret = 0;
  619. /* copy domain */
  620. if (ses->domainName == NULL) {
  621. /*
  622. * Sending null domain better than using a bogus domain name (as
  623. * we did briefly in 2.6.18) since server will use its default
  624. */
  625. *bcc_ptr = 0;
  626. *(bcc_ptr+1) = 0;
  627. bytes_ret = 0;
  628. } else
  629. bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
  630. CIFS_MAX_DOMAINNAME_LEN, nls_cp);
  631. bcc_ptr += 2 * bytes_ret;
  632. bcc_ptr += 2; /* account for null terminator */
  633. *pbcc_area = bcc_ptr;
  634. }
  635. static void ascii_domain_string(char **pbcc_area, struct cifs_ses *ses,
  636. const struct nls_table *nls_cp)
  637. {
  638. char *bcc_ptr = *pbcc_area;
  639. int len;
  640. /* copy domain */
  641. if (ses->domainName != NULL) {
  642. len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
  643. if (WARN_ON_ONCE(len < 0))
  644. len = CIFS_MAX_DOMAINNAME_LEN - 1;
  645. bcc_ptr += len;
  646. } /* else we send a null domain name so server will default to its own domain */
  647. *bcc_ptr = 0;
  648. bcc_ptr++;
  649. *pbcc_area = bcc_ptr;
  650. }
  651. static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
  652. const struct nls_table *nls_cp)
  653. {
  654. char *bcc_ptr = *pbcc_area;
  655. int bytes_ret = 0;
  656. /* BB FIXME add check that strings less than 335 or will need to send as arrays */
  657. /* copy user */
  658. if (ses->user_name == NULL) {
  659. /* null user mount */
  660. *bcc_ptr = 0;
  661. *(bcc_ptr+1) = 0;
  662. } else {
  663. bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
  664. CIFS_MAX_USERNAME_LEN, nls_cp);
  665. }
  666. bcc_ptr += 2 * bytes_ret;
  667. bcc_ptr += 2; /* account for null termination */
  668. unicode_domain_string(&bcc_ptr, ses, nls_cp);
  669. unicode_oslm_strings(&bcc_ptr, nls_cp);
  670. *pbcc_area = bcc_ptr;
  671. }
  672. static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
  673. const struct nls_table *nls_cp)
  674. {
  675. char *bcc_ptr = *pbcc_area;
  676. int len;
  677. /* copy user */
  678. /* BB what about null user mounts - check that we do this BB */
  679. /* copy user */
  680. if (ses->user_name != NULL) {
  681. len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
  682. if (WARN_ON_ONCE(len < 0))
  683. len = CIFS_MAX_USERNAME_LEN - 1;
  684. bcc_ptr += len;
  685. }
  686. /* else null user mount */
  687. *bcc_ptr = 0;
  688. bcc_ptr++; /* account for null termination */
  689. /* BB check for overflow here */
  690. ascii_domain_string(&bcc_ptr, ses, nls_cp);
  691. ascii_oslm_strings(&bcc_ptr, nls_cp);
  692. *pbcc_area = bcc_ptr;
  693. }
  694. static void
  695. decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
  696. const struct nls_table *nls_cp)
  697. {
  698. int len;
  699. char *data = *pbcc_area;
  700. cifs_dbg(FYI, "bleft %d\n", bleft);
  701. kfree(ses->serverOS);
  702. ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
  703. cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
  704. len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
  705. data += len;
  706. bleft -= len;
  707. if (bleft <= 0)
  708. return;
  709. kfree(ses->serverNOS);
  710. ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
  711. cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
  712. len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
  713. data += len;
  714. bleft -= len;
  715. if (bleft <= 0)
  716. return;
  717. kfree(ses->serverDomain);
  718. ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
  719. cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
  720. return;
  721. }
  722. static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
  723. struct cifs_ses *ses,
  724. const struct nls_table *nls_cp)
  725. {
  726. int len;
  727. char *bcc_ptr = *pbcc_area;
  728. cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
  729. len = strnlen(bcc_ptr, bleft);
  730. if (len >= bleft)
  731. return;
  732. kfree(ses->serverOS);
  733. ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
  734. if (ses->serverOS) {
  735. memcpy(ses->serverOS, bcc_ptr, len);
  736. ses->serverOS[len] = 0;
  737. if (strncmp(ses->serverOS, "OS/2", 4) == 0)
  738. cifs_dbg(FYI, "OS/2 server\n");
  739. }
  740. bcc_ptr += len + 1;
  741. bleft -= len + 1;
  742. len = strnlen(bcc_ptr, bleft);
  743. if (len >= bleft)
  744. return;
  745. kfree(ses->serverNOS);
  746. ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
  747. if (ses->serverNOS) {
  748. memcpy(ses->serverNOS, bcc_ptr, len);
  749. ses->serverNOS[len] = 0;
  750. }
  751. bcc_ptr += len + 1;
  752. bleft -= len + 1;
  753. len = strnlen(bcc_ptr, bleft);
  754. if (len > bleft)
  755. return;
  756. /*
  757. * No domain field in LANMAN case. Domain is
  758. * returned by old servers in the SMB negprot response
  759. *
  760. * BB For newer servers which do not support Unicode,
  761. * but thus do return domain here, we could add parsing
  762. * for it later, but it is not very important
  763. */
  764. cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
  765. }
  766. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  767. int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
  768. struct cifs_ses *ses)
  769. {
  770. unsigned int tioffset; /* challenge message target info area */
  771. unsigned int tilen; /* challenge message target info area length */
  772. CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
  773. __u32 server_flags;
  774. if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
  775. cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
  776. return -EINVAL;
  777. }
  778. if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
  779. cifs_dbg(VFS, "blob signature incorrect %s\n",
  780. pblob->Signature);
  781. return -EINVAL;
  782. }
  783. if (pblob->MessageType != NtLmChallenge) {
  784. cifs_dbg(VFS, "Incorrect message type %d\n",
  785. pblob->MessageType);
  786. return -EINVAL;
  787. }
  788. server_flags = le32_to_cpu(pblob->NegotiateFlags);
  789. cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
  790. ses->ntlmssp->client_flags, server_flags);
  791. if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
  792. (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
  793. cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
  794. __func__);
  795. return -EINVAL;
  796. }
  797. if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
  798. cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
  799. return -EINVAL;
  800. }
  801. if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
  802. cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
  803. __func__);
  804. return -EOPNOTSUPP;
  805. }
  806. if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
  807. !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
  808. pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
  809. __func__);
  810. ses->ntlmssp->server_flags = server_flags;
  811. memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
  812. /*
  813. * In particular we can examine sign flags
  814. *
  815. * BB spec says that if AvId field of MsvAvTimestamp is populated then
  816. * we must set the MIC field of the AUTHENTICATE_MESSAGE
  817. */
  818. tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
  819. tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
  820. if (tioffset > blob_len || tioffset + tilen > blob_len) {
  821. cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
  822. tioffset, tilen);
  823. return -EINVAL;
  824. }
  825. if (tilen) {
  826. kfree_sensitive(ses->auth_key.response);
  827. ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
  828. GFP_KERNEL);
  829. if (!ses->auth_key.response) {
  830. cifs_dbg(VFS, "Challenge target info alloc failure\n");
  831. return -ENOMEM;
  832. }
  833. ses->auth_key.len = tilen;
  834. }
  835. return 0;
  836. }
  837. static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
  838. {
  839. int sz = base_size + ses->auth_key.len
  840. - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
  841. if (ses->domainName)
  842. sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
  843. else
  844. sz += sizeof(__le16);
  845. if (ses->user_name)
  846. sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
  847. else
  848. sz += sizeof(__le16);
  849. if (ses->workstation_name[0])
  850. sz += sizeof(__le16) * strnlen(ses->workstation_name,
  851. ntlmssp_workstation_name_size(ses));
  852. else
  853. sz += sizeof(__le16);
  854. return sz;
  855. }
  856. static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
  857. char *str_value,
  858. int str_length,
  859. unsigned char *pstart,
  860. unsigned char **pcur,
  861. const struct nls_table *nls_cp)
  862. {
  863. unsigned char *tmp = pstart;
  864. int len;
  865. if (!pbuf)
  866. return;
  867. if (!pcur)
  868. pcur = &tmp;
  869. if (!str_value) {
  870. pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
  871. pbuf->Length = 0;
  872. pbuf->MaximumLength = 0;
  873. *pcur += sizeof(__le16);
  874. } else {
  875. len = cifs_strtoUTF16((__le16 *)*pcur,
  876. str_value,
  877. str_length,
  878. nls_cp);
  879. len *= sizeof(__le16);
  880. pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
  881. pbuf->Length = cpu_to_le16(len);
  882. pbuf->MaximumLength = cpu_to_le16(len);
  883. *pcur += len;
  884. }
  885. }
  886. /* BB Move to ntlmssp.c eventually */
  887. int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
  888. u16 *buflen,
  889. struct cifs_ses *ses,
  890. struct TCP_Server_Info *server,
  891. const struct nls_table *nls_cp)
  892. {
  893. int rc = 0;
  894. NEGOTIATE_MESSAGE *sec_blob;
  895. __u32 flags;
  896. unsigned char *tmp;
  897. int len;
  898. len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
  899. *pbuffer = kmalloc(len, GFP_KERNEL);
  900. if (!*pbuffer) {
  901. rc = -ENOMEM;
  902. cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
  903. *buflen = 0;
  904. goto setup_ntlm_neg_ret;
  905. }
  906. sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
  907. memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
  908. memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
  909. sec_blob->MessageType = NtLmNegotiate;
  910. /* BB is NTLMV2 session security format easier to use here? */
  911. flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
  912. NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
  913. NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
  914. NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
  915. NTLMSSP_NEGOTIATE_SIGN;
  916. if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
  917. flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
  918. tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
  919. ses->ntlmssp->client_flags = flags;
  920. sec_blob->NegotiateFlags = cpu_to_le32(flags);
  921. /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
  922. cifs_security_buffer_from_str(&sec_blob->DomainName,
  923. NULL,
  924. CIFS_MAX_DOMAINNAME_LEN,
  925. *pbuffer, &tmp,
  926. nls_cp);
  927. cifs_security_buffer_from_str(&sec_blob->WorkstationName,
  928. NULL,
  929. CIFS_MAX_WORKSTATION_LEN,
  930. *pbuffer, &tmp,
  931. nls_cp);
  932. *buflen = tmp - *pbuffer;
  933. setup_ntlm_neg_ret:
  934. return rc;
  935. }
  936. /*
  937. * Build ntlmssp blob with additional fields, such as version,
  938. * supported by modern servers. For safety limit to SMB3 or later
  939. * See notes in MS-NLMP Section 2.2.2.1 e.g.
  940. */
  941. int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
  942. u16 *buflen,
  943. struct cifs_ses *ses,
  944. struct TCP_Server_Info *server,
  945. const struct nls_table *nls_cp)
  946. {
  947. int rc = 0;
  948. struct negotiate_message *sec_blob;
  949. __u32 flags;
  950. unsigned char *tmp;
  951. int len;
  952. len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
  953. *pbuffer = kmalloc(len, GFP_KERNEL);
  954. if (!*pbuffer) {
  955. rc = -ENOMEM;
  956. cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
  957. *buflen = 0;
  958. goto setup_ntlm_smb3_neg_ret;
  959. }
  960. sec_blob = (struct negotiate_message *)*pbuffer;
  961. memset(*pbuffer, 0, sizeof(struct negotiate_message));
  962. memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
  963. sec_blob->MessageType = NtLmNegotiate;
  964. /* BB is NTLMV2 session security format easier to use here? */
  965. flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
  966. NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
  967. NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
  968. NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
  969. NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
  970. if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
  971. flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
  972. sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
  973. sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
  974. sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
  975. sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
  976. tmp = *pbuffer + sizeof(struct negotiate_message);
  977. ses->ntlmssp->client_flags = flags;
  978. sec_blob->NegotiateFlags = cpu_to_le32(flags);
  979. /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
  980. cifs_security_buffer_from_str(&sec_blob->DomainName,
  981. NULL,
  982. CIFS_MAX_DOMAINNAME_LEN,
  983. *pbuffer, &tmp,
  984. nls_cp);
  985. cifs_security_buffer_from_str(&sec_blob->WorkstationName,
  986. NULL,
  987. CIFS_MAX_WORKSTATION_LEN,
  988. *pbuffer, &tmp,
  989. nls_cp);
  990. *buflen = tmp - *pbuffer;
  991. setup_ntlm_smb3_neg_ret:
  992. return rc;
  993. }
  994. /* See MS-NLMP 2.2.1.3 */
  995. int build_ntlmssp_auth_blob(unsigned char **pbuffer,
  996. u16 *buflen,
  997. struct cifs_ses *ses,
  998. struct TCP_Server_Info *server,
  999. const struct nls_table *nls_cp)
  1000. {
  1001. int rc;
  1002. AUTHENTICATE_MESSAGE *sec_blob;
  1003. __u32 flags;
  1004. unsigned char *tmp;
  1005. int len;
  1006. rc = setup_ntlmv2_rsp(ses, nls_cp);
  1007. if (rc) {
  1008. cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
  1009. *buflen = 0;
  1010. goto setup_ntlmv2_ret;
  1011. }
  1012. len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
  1013. *pbuffer = kmalloc(len, GFP_KERNEL);
  1014. if (!*pbuffer) {
  1015. rc = -ENOMEM;
  1016. cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
  1017. *buflen = 0;
  1018. goto setup_ntlmv2_ret;
  1019. }
  1020. sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
  1021. memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
  1022. sec_blob->MessageType = NtLmAuthenticate;
  1023. /* send version information in ntlmssp authenticate also */
  1024. flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
  1025. NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_VERSION |
  1026. NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
  1027. sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
  1028. sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
  1029. sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
  1030. sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
  1031. tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
  1032. sec_blob->NegotiateFlags = cpu_to_le32(flags);
  1033. sec_blob->LmChallengeResponse.BufferOffset =
  1034. cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
  1035. sec_blob->LmChallengeResponse.Length = 0;
  1036. sec_blob->LmChallengeResponse.MaximumLength = 0;
  1037. sec_blob->NtChallengeResponse.BufferOffset =
  1038. cpu_to_le32(tmp - *pbuffer);
  1039. if (ses->user_name != NULL) {
  1040. memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  1041. ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  1042. tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
  1043. sec_blob->NtChallengeResponse.Length =
  1044. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  1045. sec_blob->NtChallengeResponse.MaximumLength =
  1046. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  1047. } else {
  1048. /*
  1049. * don't send an NT Response for anonymous access
  1050. */
  1051. sec_blob->NtChallengeResponse.Length = 0;
  1052. sec_blob->NtChallengeResponse.MaximumLength = 0;
  1053. }
  1054. cifs_security_buffer_from_str(&sec_blob->DomainName,
  1055. ses->domainName,
  1056. CIFS_MAX_DOMAINNAME_LEN,
  1057. *pbuffer, &tmp,
  1058. nls_cp);
  1059. cifs_security_buffer_from_str(&sec_blob->UserName,
  1060. ses->user_name,
  1061. CIFS_MAX_USERNAME_LEN,
  1062. *pbuffer, &tmp,
  1063. nls_cp);
  1064. cifs_security_buffer_from_str(&sec_blob->WorkstationName,
  1065. ses->workstation_name,
  1066. ntlmssp_workstation_name_size(ses),
  1067. *pbuffer, &tmp,
  1068. nls_cp);
  1069. if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
  1070. (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
  1071. !calc_seckey(ses)) {
  1072. memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
  1073. sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  1074. sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
  1075. sec_blob->SessionKey.MaximumLength =
  1076. cpu_to_le16(CIFS_CPHTXT_SIZE);
  1077. tmp += CIFS_CPHTXT_SIZE;
  1078. } else {
  1079. sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
  1080. sec_blob->SessionKey.Length = 0;
  1081. sec_blob->SessionKey.MaximumLength = 0;
  1082. }
  1083. *buflen = tmp - *pbuffer;
  1084. setup_ntlmv2_ret:
  1085. return rc;
  1086. }
  1087. enum securityEnum
  1088. cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
  1089. {
  1090. switch (server->negflavor) {
  1091. case CIFS_NEGFLAVOR_EXTENDED:
  1092. switch (requested) {
  1093. case Kerberos:
  1094. case RawNTLMSSP:
  1095. case IAKerb:
  1096. return requested;
  1097. case Unspecified:
  1098. if (server->sec_ntlmssp &&
  1099. (global_secflags & CIFSSEC_MAY_NTLMSSP))
  1100. return RawNTLMSSP;
  1101. if ((server->sec_kerberos || server->sec_mskerberos || server->sec_iakerb) &&
  1102. (global_secflags & CIFSSEC_MAY_KRB5))
  1103. return Kerberos;
  1104. fallthrough;
  1105. default:
  1106. return Unspecified;
  1107. }
  1108. case CIFS_NEGFLAVOR_UNENCAP:
  1109. switch (requested) {
  1110. case NTLMv2:
  1111. return requested;
  1112. case Unspecified:
  1113. if (global_secflags & CIFSSEC_MAY_NTLMV2)
  1114. return NTLMv2;
  1115. break;
  1116. default:
  1117. break;
  1118. }
  1119. fallthrough;
  1120. default:
  1121. return Unspecified;
  1122. }
  1123. }
  1124. struct sess_data {
  1125. unsigned int xid;
  1126. struct cifs_ses *ses;
  1127. struct TCP_Server_Info *server;
  1128. struct nls_table *nls_cp;
  1129. void (*func)(struct sess_data *);
  1130. int result;
  1131. /* we will send the SMB in three pieces:
  1132. * a fixed length beginning part, an optional
  1133. * SPNEGO blob (which can be zero length), and a
  1134. * last part which will include the strings
  1135. * and rest of bcc area. This allows us to avoid
  1136. * a large buffer 17K allocation
  1137. */
  1138. int buf0_type;
  1139. struct kvec iov[3];
  1140. };
  1141. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1142. static int
  1143. sess_alloc_buffer(struct sess_data *sess_data, int wct)
  1144. {
  1145. int rc;
  1146. struct cifs_ses *ses = sess_data->ses;
  1147. struct smb_hdr *smb_buf;
  1148. rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
  1149. (void **)&smb_buf);
  1150. if (rc)
  1151. return rc;
  1152. sess_data->iov[0].iov_base = (char *)smb_buf;
  1153. sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
  1154. /*
  1155. * This variable will be used to clear the buffer
  1156. * allocated above in case of any error in the calling function.
  1157. */
  1158. sess_data->buf0_type = CIFS_SMALL_BUFFER;
  1159. /* 2000 big enough to fit max user, domain, NOS name etc. */
  1160. sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
  1161. if (!sess_data->iov[2].iov_base) {
  1162. rc = -ENOMEM;
  1163. goto out_free_smb_buf;
  1164. }
  1165. return 0;
  1166. out_free_smb_buf:
  1167. cifs_small_buf_release(smb_buf);
  1168. sess_data->iov[0].iov_base = NULL;
  1169. sess_data->iov[0].iov_len = 0;
  1170. sess_data->buf0_type = CIFS_NO_BUFFER;
  1171. return rc;
  1172. }
  1173. static void
  1174. sess_free_buffer(struct sess_data *sess_data)
  1175. {
  1176. struct kvec *iov = sess_data->iov;
  1177. /*
  1178. * Zero the session data before freeing, as it might contain sensitive info (keys, etc).
  1179. * Note that iov[1] is already freed by caller.
  1180. */
  1181. if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
  1182. memzero_explicit(iov[0].iov_base, iov[0].iov_len);
  1183. free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
  1184. sess_data->buf0_type = CIFS_NO_BUFFER;
  1185. kfree_sensitive(iov[2].iov_base);
  1186. }
  1187. static int
  1188. sess_establish_session(struct sess_data *sess_data)
  1189. {
  1190. struct cifs_ses *ses = sess_data->ses;
  1191. struct TCP_Server_Info *server = sess_data->server;
  1192. cifs_server_lock(server);
  1193. if (!server->session_estab) {
  1194. if (server->sign) {
  1195. server->session_key.response =
  1196. kmemdup(ses->auth_key.response,
  1197. ses->auth_key.len, GFP_KERNEL);
  1198. if (!server->session_key.response) {
  1199. cifs_server_unlock(server);
  1200. return -ENOMEM;
  1201. }
  1202. server->session_key.len =
  1203. ses->auth_key.len;
  1204. }
  1205. server->sequence_number = 0x2;
  1206. server->session_estab = true;
  1207. }
  1208. cifs_server_unlock(server);
  1209. cifs_dbg(FYI, "CIFS session established successfully\n");
  1210. return 0;
  1211. }
  1212. static int
  1213. sess_sendreceive(struct sess_data *sess_data)
  1214. {
  1215. int rc;
  1216. struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
  1217. __u16 count;
  1218. struct kvec rsp_iov = { NULL, 0 };
  1219. count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
  1220. be32_add_cpu(&smb_buf->smb_buf_length, count);
  1221. put_bcc(count, smb_buf);
  1222. rc = SendReceive2(sess_data->xid, sess_data->ses,
  1223. sess_data->iov, 3 /* num_iovecs */,
  1224. &sess_data->buf0_type,
  1225. CIFS_LOG_ERROR, &rsp_iov);
  1226. cifs_small_buf_release(sess_data->iov[0].iov_base);
  1227. memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
  1228. return rc;
  1229. }
  1230. static void
  1231. sess_auth_ntlmv2(struct sess_data *sess_data)
  1232. {
  1233. int rc = 0;
  1234. struct smb_hdr *smb_buf;
  1235. SESSION_SETUP_ANDX *pSMB;
  1236. char *bcc_ptr;
  1237. struct cifs_ses *ses = sess_data->ses;
  1238. struct TCP_Server_Info *server = sess_data->server;
  1239. __u32 capabilities;
  1240. __u16 bytes_remaining;
  1241. /* old style NTLM sessionsetup */
  1242. /* wct = 13 */
  1243. rc = sess_alloc_buffer(sess_data, 13);
  1244. if (rc)
  1245. goto out;
  1246. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1247. bcc_ptr = sess_data->iov[2].iov_base;
  1248. capabilities = cifs_ssetup_hdr(ses, server, pSMB);
  1249. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  1250. /* LM2 password would be here if we supported it */
  1251. pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
  1252. if (ses->user_name != NULL) {
  1253. /* calculate nlmv2 response and session key */
  1254. rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
  1255. if (rc) {
  1256. cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
  1257. goto out;
  1258. }
  1259. memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  1260. ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  1261. bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
  1262. /* set case sensitive password length after tilen may get
  1263. * assigned, tilen is 0 otherwise.
  1264. */
  1265. pSMB->req_no_secext.CaseSensitivePasswordLength =
  1266. cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
  1267. } else {
  1268. pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
  1269. }
  1270. if (ses->capabilities & CAP_UNICODE) {
  1271. if (!IS_ALIGNED(sess_data->iov[0].iov_len, 2)) {
  1272. *bcc_ptr = 0;
  1273. bcc_ptr++;
  1274. }
  1275. unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  1276. } else {
  1277. ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
  1278. }
  1279. sess_data->iov[2].iov_len = (long) bcc_ptr -
  1280. (long) sess_data->iov[2].iov_base;
  1281. rc = sess_sendreceive(sess_data);
  1282. if (rc)
  1283. goto out;
  1284. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1285. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1286. if (smb_buf->WordCount != 3) {
  1287. rc = -EIO;
  1288. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1289. goto out;
  1290. }
  1291. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  1292. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  1293. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  1294. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  1295. bytes_remaining = get_bcc(smb_buf);
  1296. bcc_ptr = pByteArea(smb_buf);
  1297. /* BB check if Unicode and decode strings */
  1298. if (bytes_remaining == 0) {
  1299. /* no string area to decode, do nothing */
  1300. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  1301. /* unicode string area must be word-aligned */
  1302. if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
  1303. ++bcc_ptr;
  1304. --bytes_remaining;
  1305. }
  1306. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  1307. sess_data->nls_cp);
  1308. } else {
  1309. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  1310. sess_data->nls_cp);
  1311. }
  1312. rc = sess_establish_session(sess_data);
  1313. out:
  1314. sess_data->result = rc;
  1315. sess_data->func = NULL;
  1316. sess_free_buffer(sess_data);
  1317. kfree_sensitive(ses->auth_key.response);
  1318. ses->auth_key.response = NULL;
  1319. }
  1320. #ifdef CONFIG_CIFS_UPCALL
  1321. static void
  1322. sess_auth_kerberos(struct sess_data *sess_data)
  1323. {
  1324. int rc = 0;
  1325. struct smb_hdr *smb_buf;
  1326. SESSION_SETUP_ANDX *pSMB;
  1327. char *bcc_ptr;
  1328. struct cifs_ses *ses = sess_data->ses;
  1329. struct TCP_Server_Info *server = sess_data->server;
  1330. __u32 capabilities;
  1331. __u16 bytes_remaining;
  1332. struct key *spnego_key = NULL;
  1333. struct cifs_spnego_msg *msg;
  1334. u16 blob_len;
  1335. /* extended security */
  1336. /* wct = 12 */
  1337. rc = sess_alloc_buffer(sess_data, 12);
  1338. if (rc)
  1339. goto out;
  1340. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1341. bcc_ptr = sess_data->iov[2].iov_base;
  1342. capabilities = cifs_ssetup_hdr(ses, server, pSMB);
  1343. spnego_key = cifs_get_spnego_key(ses, server);
  1344. if (IS_ERR(spnego_key)) {
  1345. rc = PTR_ERR(spnego_key);
  1346. spnego_key = NULL;
  1347. goto out;
  1348. }
  1349. msg = spnego_key->payload.data[0];
  1350. /*
  1351. * check version field to make sure that cifs.upcall is
  1352. * sending us a response in an expected form
  1353. */
  1354. if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
  1355. cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
  1356. CIFS_SPNEGO_UPCALL_VERSION, msg->version);
  1357. rc = -EKEYREJECTED;
  1358. goto out_put_spnego_key;
  1359. }
  1360. kfree_sensitive(ses->auth_key.response);
  1361. ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
  1362. GFP_KERNEL);
  1363. if (!ses->auth_key.response) {
  1364. cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
  1365. msg->sesskey_len);
  1366. rc = -ENOMEM;
  1367. goto out_put_spnego_key;
  1368. }
  1369. ses->auth_key.len = msg->sesskey_len;
  1370. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  1371. capabilities |= CAP_EXTENDED_SECURITY;
  1372. pSMB->req.Capabilities = cpu_to_le32(capabilities);
  1373. sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
  1374. sess_data->iov[1].iov_len = msg->secblob_len;
  1375. pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
  1376. if (pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) {
  1377. /* unicode strings must be word aligned */
  1378. if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
  1379. *bcc_ptr = 0;
  1380. bcc_ptr++;
  1381. }
  1382. unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  1383. unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
  1384. } else {
  1385. ascii_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  1386. ascii_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
  1387. }
  1388. sess_data->iov[2].iov_len = (long) bcc_ptr -
  1389. (long) sess_data->iov[2].iov_base;
  1390. rc = sess_sendreceive(sess_data);
  1391. if (rc)
  1392. goto out_put_spnego_key;
  1393. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1394. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1395. if (smb_buf->WordCount != 4) {
  1396. rc = -EIO;
  1397. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1398. goto out_put_spnego_key;
  1399. }
  1400. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  1401. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  1402. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  1403. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  1404. bytes_remaining = get_bcc(smb_buf);
  1405. bcc_ptr = pByteArea(smb_buf);
  1406. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1407. if (blob_len > bytes_remaining) {
  1408. cifs_dbg(VFS, "bad security blob length %d\n",
  1409. blob_len);
  1410. rc = -EINVAL;
  1411. goto out_put_spnego_key;
  1412. }
  1413. bcc_ptr += blob_len;
  1414. bytes_remaining -= blob_len;
  1415. /* BB check if Unicode and decode strings */
  1416. if (bytes_remaining == 0) {
  1417. /* no string area to decode, do nothing */
  1418. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  1419. /* unicode string area must be word-aligned */
  1420. if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
  1421. ++bcc_ptr;
  1422. --bytes_remaining;
  1423. }
  1424. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  1425. sess_data->nls_cp);
  1426. } else {
  1427. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  1428. sess_data->nls_cp);
  1429. }
  1430. rc = sess_establish_session(sess_data);
  1431. out_put_spnego_key:
  1432. key_invalidate(spnego_key);
  1433. key_put(spnego_key);
  1434. out:
  1435. sess_data->result = rc;
  1436. sess_data->func = NULL;
  1437. sess_free_buffer(sess_data);
  1438. kfree_sensitive(ses->auth_key.response);
  1439. ses->auth_key.response = NULL;
  1440. }
  1441. #endif /* ! CONFIG_CIFS_UPCALL */
  1442. /*
  1443. * The required kvec buffers have to be allocated before calling this
  1444. * function.
  1445. */
  1446. static int
  1447. _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
  1448. {
  1449. SESSION_SETUP_ANDX *pSMB;
  1450. struct cifs_ses *ses = sess_data->ses;
  1451. struct TCP_Server_Info *server = sess_data->server;
  1452. __u32 capabilities;
  1453. char *bcc_ptr;
  1454. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1455. capabilities = cifs_ssetup_hdr(ses, server, pSMB);
  1456. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  1457. capabilities |= CAP_EXTENDED_SECURITY;
  1458. pSMB->req.Capabilities |= cpu_to_le32(capabilities);
  1459. bcc_ptr = sess_data->iov[2].iov_base;
  1460. if (pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) {
  1461. /* unicode strings must be word aligned */
  1462. if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
  1463. *bcc_ptr = 0;
  1464. bcc_ptr++;
  1465. }
  1466. unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  1467. } else {
  1468. ascii_oslm_strings(&bcc_ptr, sess_data->nls_cp);
  1469. }
  1470. sess_data->iov[2].iov_len = (long) bcc_ptr -
  1471. (long) sess_data->iov[2].iov_base;
  1472. return 0;
  1473. }
  1474. static void
  1475. sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
  1476. static void
  1477. sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
  1478. {
  1479. int rc;
  1480. struct smb_hdr *smb_buf;
  1481. SESSION_SETUP_ANDX *pSMB;
  1482. struct cifs_ses *ses = sess_data->ses;
  1483. struct TCP_Server_Info *server = sess_data->server;
  1484. __u16 bytes_remaining;
  1485. char *bcc_ptr;
  1486. unsigned char *ntlmsspblob = NULL;
  1487. u16 blob_len;
  1488. cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
  1489. /*
  1490. * if memory allocation is successful, caller of this function
  1491. * frees it.
  1492. */
  1493. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  1494. if (!ses->ntlmssp) {
  1495. rc = -ENOMEM;
  1496. goto out;
  1497. }
  1498. ses->ntlmssp->sesskey_per_smbsess = false;
  1499. /* wct = 12 */
  1500. rc = sess_alloc_buffer(sess_data, 12);
  1501. if (rc)
  1502. goto out;
  1503. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1504. /* Build security blob before we assemble the request */
  1505. rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
  1506. &blob_len, ses, server,
  1507. sess_data->nls_cp);
  1508. if (rc)
  1509. goto out_free_ntlmsspblob;
  1510. sess_data->iov[1].iov_len = blob_len;
  1511. sess_data->iov[1].iov_base = ntlmsspblob;
  1512. pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
  1513. rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
  1514. if (rc)
  1515. goto out_free_ntlmsspblob;
  1516. rc = sess_sendreceive(sess_data);
  1517. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1518. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1519. /* If true, rc here is expected and not an error */
  1520. if (sess_data->buf0_type != CIFS_NO_BUFFER &&
  1521. smb_buf->Status.CifsError ==
  1522. cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
  1523. rc = 0;
  1524. if (rc)
  1525. goto out_free_ntlmsspblob;
  1526. cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
  1527. if (smb_buf->WordCount != 4) {
  1528. rc = -EIO;
  1529. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1530. goto out_free_ntlmsspblob;
  1531. }
  1532. ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
  1533. cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
  1534. bytes_remaining = get_bcc(smb_buf);
  1535. bcc_ptr = pByteArea(smb_buf);
  1536. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1537. if (blob_len > bytes_remaining) {
  1538. cifs_dbg(VFS, "bad security blob length %d\n",
  1539. blob_len);
  1540. rc = -EINVAL;
  1541. goto out_free_ntlmsspblob;
  1542. }
  1543. rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
  1544. out_free_ntlmsspblob:
  1545. kfree_sensitive(ntlmsspblob);
  1546. out:
  1547. sess_free_buffer(sess_data);
  1548. if (!rc) {
  1549. sess_data->func = sess_auth_rawntlmssp_authenticate;
  1550. return;
  1551. }
  1552. /* Else error. Cleanup */
  1553. kfree_sensitive(ses->auth_key.response);
  1554. ses->auth_key.response = NULL;
  1555. kfree_sensitive(ses->ntlmssp);
  1556. ses->ntlmssp = NULL;
  1557. sess_data->func = NULL;
  1558. sess_data->result = rc;
  1559. }
  1560. static void
  1561. sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
  1562. {
  1563. int rc;
  1564. struct smb_hdr *smb_buf;
  1565. SESSION_SETUP_ANDX *pSMB;
  1566. struct cifs_ses *ses = sess_data->ses;
  1567. struct TCP_Server_Info *server = sess_data->server;
  1568. __u16 bytes_remaining;
  1569. char *bcc_ptr;
  1570. unsigned char *ntlmsspblob = NULL;
  1571. u16 blob_len;
  1572. cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
  1573. /* wct = 12 */
  1574. rc = sess_alloc_buffer(sess_data, 12);
  1575. if (rc)
  1576. goto out;
  1577. /* Build security blob before we assemble the request */
  1578. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1579. smb_buf = (struct smb_hdr *)pSMB;
  1580. rc = build_ntlmssp_auth_blob(&ntlmsspblob,
  1581. &blob_len, ses, server,
  1582. sess_data->nls_cp);
  1583. if (rc)
  1584. goto out_free_ntlmsspblob;
  1585. sess_data->iov[1].iov_len = blob_len;
  1586. sess_data->iov[1].iov_base = ntlmsspblob;
  1587. pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
  1588. /*
  1589. * Make sure that we tell the server that we are using
  1590. * the uid that it just gave us back on the response
  1591. * (challenge)
  1592. */
  1593. smb_buf->Uid = ses->Suid;
  1594. rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
  1595. if (rc)
  1596. goto out_free_ntlmsspblob;
  1597. rc = sess_sendreceive(sess_data);
  1598. if (rc)
  1599. goto out_free_ntlmsspblob;
  1600. pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
  1601. smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
  1602. if (smb_buf->WordCount != 4) {
  1603. rc = -EIO;
  1604. cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
  1605. goto out_free_ntlmsspblob;
  1606. }
  1607. if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
  1608. cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
  1609. if (ses->Suid != smb_buf->Uid) {
  1610. ses->Suid = smb_buf->Uid;
  1611. cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
  1612. }
  1613. bytes_remaining = get_bcc(smb_buf);
  1614. bcc_ptr = pByteArea(smb_buf);
  1615. blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
  1616. if (blob_len > bytes_remaining) {
  1617. cifs_dbg(VFS, "bad security blob length %d\n",
  1618. blob_len);
  1619. rc = -EINVAL;
  1620. goto out_free_ntlmsspblob;
  1621. }
  1622. bcc_ptr += blob_len;
  1623. bytes_remaining -= blob_len;
  1624. /* BB check if Unicode and decode strings */
  1625. if (bytes_remaining == 0) {
  1626. /* no string area to decode, do nothing */
  1627. } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
  1628. /* unicode string area must be word-aligned */
  1629. if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
  1630. ++bcc_ptr;
  1631. --bytes_remaining;
  1632. }
  1633. decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
  1634. sess_data->nls_cp);
  1635. } else {
  1636. decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
  1637. sess_data->nls_cp);
  1638. }
  1639. out_free_ntlmsspblob:
  1640. kfree_sensitive(ntlmsspblob);
  1641. out:
  1642. sess_free_buffer(sess_data);
  1643. if (!rc)
  1644. rc = sess_establish_session(sess_data);
  1645. /* Cleanup */
  1646. kfree_sensitive(ses->auth_key.response);
  1647. ses->auth_key.response = NULL;
  1648. kfree_sensitive(ses->ntlmssp);
  1649. ses->ntlmssp = NULL;
  1650. sess_data->func = NULL;
  1651. sess_data->result = rc;
  1652. }
  1653. static int select_sec(struct sess_data *sess_data)
  1654. {
  1655. int type;
  1656. struct cifs_ses *ses = sess_data->ses;
  1657. struct TCP_Server_Info *server = sess_data->server;
  1658. type = cifs_select_sectype(server, ses->sectype);
  1659. cifs_dbg(FYI, "sess setup type %d\n", type);
  1660. if (type == Unspecified) {
  1661. cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
  1662. return -EINVAL;
  1663. }
  1664. switch (type) {
  1665. case NTLMv2:
  1666. sess_data->func = sess_auth_ntlmv2;
  1667. break;
  1668. case Kerberos:
  1669. #ifdef CONFIG_CIFS_UPCALL
  1670. sess_data->func = sess_auth_kerberos;
  1671. break;
  1672. #else
  1673. cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
  1674. return -ENOSYS;
  1675. #endif /* CONFIG_CIFS_UPCALL */
  1676. case RawNTLMSSP:
  1677. sess_data->func = sess_auth_rawntlmssp_negotiate;
  1678. break;
  1679. default:
  1680. cifs_dbg(VFS, "secType %d not supported!\n", type);
  1681. return -ENOSYS;
  1682. }
  1683. return 0;
  1684. }
  1685. int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
  1686. struct TCP_Server_Info *server,
  1687. const struct nls_table *nls_cp)
  1688. {
  1689. int rc = 0;
  1690. struct sess_data *sess_data;
  1691. if (ses == NULL) {
  1692. WARN(1, "%s: ses == NULL!", __func__);
  1693. return -EINVAL;
  1694. }
  1695. sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
  1696. if (!sess_data)
  1697. return -ENOMEM;
  1698. sess_data->xid = xid;
  1699. sess_data->ses = ses;
  1700. sess_data->server = server;
  1701. sess_data->buf0_type = CIFS_NO_BUFFER;
  1702. sess_data->nls_cp = (struct nls_table *) nls_cp;
  1703. rc = select_sec(sess_data);
  1704. if (rc)
  1705. goto out;
  1706. while (sess_data->func)
  1707. sess_data->func(sess_data);
  1708. /* Store result before we free sess_data */
  1709. rc = sess_data->result;
  1710. out:
  1711. kfree_sensitive(sess_data);
  1712. return rc;
  1713. }
  1714. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */