nscd_getserv_r.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* Copyright (C) 2007-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <not-cancel.h>
  18. #include <_itoa.h>
  19. #include <stdint.h>
  20. #include "nscd-client.h"
  21. #include "nscd_proto.h"
  22. int __nss_not_use_nscd_services;
  23. static int nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
  24. request_type type, struct servent *resultbuf,
  25. char *buf, size_t buflen, struct servent **result);
  26. int
  27. __nscd_getservbyname_r (const char *name, const char *proto,
  28. struct servent *result_buf, char *buf, size_t buflen,
  29. struct servent **result)
  30. {
  31. return nscd_getserv_r (name, strlen (name), proto, GETSERVBYNAME, result_buf,
  32. buf, buflen, result);
  33. }
  34. int
  35. __nscd_getservbyport_r (int port, const char *proto,
  36. struct servent *result_buf, char *buf, size_t buflen,
  37. struct servent **result)
  38. {
  39. char portstr[3 * sizeof (int) + 2];
  40. portstr[sizeof (portstr) - 1] = '\0';
  41. char *cp = _itoa_word (port, portstr + sizeof (portstr) - 1, 10, 0);
  42. return nscd_getserv_r (cp, portstr + sizeof (portstr) - 1 - cp, proto,
  43. GETSERVBYPORT, result_buf, buf, buflen, result);
  44. }
  45. libc_locked_map_ptr (, __serv_map_handle) attribute_hidden;
  46. /* Note that we only free the structure if necessary. The memory
  47. mapping is not removed since it is not visible to the malloc
  48. handling. */
  49. void
  50. __nscd_serv_map_freemem (void)
  51. {
  52. if (__serv_map_handle.mapped != NO_MAPPING)
  53. {
  54. void *p = __serv_map_handle.mapped;
  55. __serv_map_handle.mapped = NO_MAPPING;
  56. free (p);
  57. }
  58. }
  59. static int
  60. nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
  61. request_type type, struct servent *resultbuf,
  62. char *buf, size_t buflen, struct servent **result)
  63. {
  64. int gc_cycle;
  65. int nretries = 0;
  66. size_t alloca_used = 0;
  67. /* If the mapping is available, try to search there instead of
  68. communicating with the nscd. */
  69. struct mapped_database *mapped;
  70. mapped = __nscd_get_map_ref (GETFDSERV, "services", &__serv_map_handle,
  71. &gc_cycle);
  72. size_t protolen = proto == NULL ? 0 : strlen (proto);
  73. size_t keylen = critlen + 1 + protolen + 1;
  74. int alloca_key = __libc_use_alloca (keylen);
  75. char *key;
  76. if (alloca_key)
  77. key = alloca_account (keylen, alloca_used);
  78. else
  79. {
  80. key = malloc (keylen);
  81. if (key == NULL)
  82. return -1;
  83. }
  84. memcpy (__mempcpy (__mempcpy (key, crit, critlen),
  85. "/", 1), proto ?: "", protolen + 1);
  86. retry:;
  87. const char *s_name = NULL;
  88. const char *s_proto = NULL;
  89. int alloca_aliases_len = 0;
  90. const uint32_t *aliases_len = NULL;
  91. const char *aliases_list = NULL;
  92. int retval = -1;
  93. const char *recend = (const char *) ~UINTMAX_C (0);
  94. int sock = -1;
  95. serv_response_header serv_resp;
  96. if (mapped != NO_MAPPING)
  97. {
  98. struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
  99. sizeof serv_resp);
  100. if (found != NULL)
  101. {
  102. s_name = (char *) (&found->data[0].servdata + 1);
  103. serv_resp = found->data[0].servdata;
  104. s_proto = s_name + serv_resp.s_name_len;
  105. alloca_aliases_len = 1;
  106. aliases_len = (uint32_t *) (s_proto + serv_resp.s_proto_len);
  107. aliases_list = ((char *) aliases_len
  108. + serv_resp.s_aliases_cnt * sizeof (uint32_t));
  109. recend = (const char *) found->data + found->recsize;
  110. /* Now check if we can trust serv_resp fields. If GC is
  111. in progress, it can contain anything. */
  112. if (mapped->head->gc_cycle != gc_cycle)
  113. {
  114. retval = -2;
  115. goto out;
  116. }
  117. if (__builtin_expect ((const char *) aliases_len
  118. + serv_resp.s_aliases_cnt * sizeof (uint32_t)
  119. > recend, 0))
  120. goto out;
  121. /* The aliases_len array in the mapped database might very
  122. well be unaligned. We will access it word-wise so on
  123. platforms which do not tolerate unaligned accesses we
  124. need to make an aligned copy. */
  125. if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
  126. != 0)
  127. {
  128. uint32_t *tmp;
  129. alloca_aliases_len
  130. = __libc_use_alloca (alloca_used
  131. + (serv_resp.s_aliases_cnt
  132. * sizeof (uint32_t)));
  133. if (alloca_aliases_len)
  134. tmp = alloca_account (serv_resp.s_aliases_cnt
  135. * sizeof (uint32_t),
  136. alloca_used);
  137. else
  138. {
  139. tmp = malloc (serv_resp.s_aliases_cnt * sizeof (uint32_t));
  140. if (tmp == NULL)
  141. {
  142. retval = ENOMEM;
  143. goto out;
  144. }
  145. }
  146. aliases_len = memcpy (tmp, aliases_len,
  147. serv_resp.s_aliases_cnt
  148. * sizeof (uint32_t));
  149. }
  150. }
  151. }
  152. if (s_name == NULL)
  153. {
  154. sock = __nscd_open_socket (key, keylen, type, &serv_resp,
  155. sizeof (serv_resp));
  156. if (sock == -1)
  157. {
  158. __nss_not_use_nscd_services = 1;
  159. goto out;
  160. }
  161. }
  162. /* No value found so far. */
  163. *result = NULL;
  164. if (__glibc_unlikely (serv_resp.found == -1))
  165. {
  166. /* The daemon does not cache this database. */
  167. __nss_not_use_nscd_services = 1;
  168. goto out_close;
  169. }
  170. if (serv_resp.found == 1)
  171. {
  172. char *cp = buf;
  173. uintptr_t align1;
  174. uintptr_t align2;
  175. size_t total_len;
  176. ssize_t cnt;
  177. int n;
  178. /* A first check whether the buffer is sufficiently large is possible. */
  179. /* Now allocate the buffer the array for the group members. We must
  180. align the pointer and the base of the h_addr_list pointers. */
  181. align1 = ((__alignof__ (char *) - ((uintptr_t) cp))
  182. & (__alignof__ (char *) - 1));
  183. align2 = ((__alignof__ (char *) - ((uintptr_t) (cp + align1 + serv_resp.s_name_len
  184. + serv_resp.s_proto_len)))
  185. & (__alignof__ (char *) - 1));
  186. if (buflen < (align1 + serv_resp.s_name_len + serv_resp.s_proto_len
  187. + align2
  188. + (serv_resp.s_aliases_cnt + 1) * sizeof (char *)))
  189. {
  190. no_room:
  191. __set_errno (ERANGE);
  192. retval = ERANGE;
  193. goto out_close;
  194. }
  195. cp += align1;
  196. /* Prepare the result as far as we can. */
  197. resultbuf->s_aliases = (char **) cp;
  198. cp += (serv_resp.s_aliases_cnt + 1) * sizeof (char *);
  199. resultbuf->s_name = cp;
  200. cp += serv_resp.s_name_len;
  201. resultbuf->s_proto = cp;
  202. cp += serv_resp.s_proto_len + align2;
  203. resultbuf->s_port = serv_resp.s_port;
  204. if (s_name == NULL)
  205. {
  206. struct iovec vec[2];
  207. vec[0].iov_base = resultbuf->s_name;
  208. vec[0].iov_len = serv_resp.s_name_len + serv_resp.s_proto_len;
  209. total_len = vec[0].iov_len;
  210. n = 1;
  211. if (serv_resp.s_aliases_cnt > 0)
  212. {
  213. assert (alloca_aliases_len == 0);
  214. alloca_aliases_len
  215. = __libc_use_alloca (alloca_used
  216. + (serv_resp.s_aliases_cnt
  217. * sizeof (uint32_t)));
  218. if (alloca_aliases_len)
  219. aliases_len = alloca_account (serv_resp.s_aliases_cnt
  220. * sizeof (uint32_t),
  221. alloca_used);
  222. else
  223. {
  224. aliases_len = malloc (serv_resp.s_aliases_cnt
  225. * sizeof (uint32_t));
  226. if (aliases_len == NULL)
  227. {
  228. retval = ENOMEM;
  229. goto out_close;
  230. }
  231. }
  232. vec[n].iov_base = (void *) aliases_len;
  233. vec[n].iov_len = serv_resp.s_aliases_cnt * sizeof (uint32_t);
  234. total_len += serv_resp.s_aliases_cnt * sizeof (uint32_t);
  235. ++n;
  236. }
  237. if ((size_t) __readvall (sock, vec, n) != total_len)
  238. goto out_close;
  239. }
  240. else
  241. memcpy (resultbuf->s_name, s_name,
  242. serv_resp.s_name_len + serv_resp.s_proto_len);
  243. /* Now we also can read the aliases. */
  244. total_len = 0;
  245. for (cnt = 0; cnt < serv_resp.s_aliases_cnt; ++cnt)
  246. {
  247. resultbuf->s_aliases[cnt] = cp;
  248. cp += aliases_len[cnt];
  249. total_len += aliases_len[cnt];
  250. }
  251. resultbuf->s_aliases[cnt] = NULL;
  252. if (__builtin_expect ((const char *) aliases_list + total_len > recend,
  253. 0))
  254. {
  255. /* aliases_len array might contain garbage during nscd GC cycle,
  256. retry rather than fail in that case. */
  257. if (aliases_list != NULL && mapped->head->gc_cycle != gc_cycle)
  258. retval = -2;
  259. goto out_close;
  260. }
  261. /* See whether this would exceed the buffer capacity. */
  262. if (__glibc_unlikely (cp > buf + buflen))
  263. {
  264. /* aliases_len array might contain garbage during nscd GC cycle,
  265. retry rather than fail in that case. */
  266. if (aliases_list != NULL && mapped->head->gc_cycle != gc_cycle)
  267. {
  268. retval = -2;
  269. goto out_close;
  270. }
  271. goto no_room;
  272. }
  273. /* And finally read the aliases. */
  274. if (aliases_list == NULL)
  275. {
  276. if (total_len == 0
  277. || ((size_t) __readall (sock, resultbuf->s_aliases[0], total_len)
  278. == total_len))
  279. {
  280. retval = 0;
  281. *result = resultbuf;
  282. }
  283. }
  284. else
  285. {
  286. memcpy (resultbuf->s_aliases[0], aliases_list, total_len);
  287. /* Try to detect corrupt databases. */
  288. if (resultbuf->s_name[serv_resp.s_name_len - 1] != '\0'
  289. || resultbuf->s_proto[serv_resp.s_proto_len - 1] != '\0'
  290. || ({for (cnt = 0; cnt < serv_resp.s_aliases_cnt; ++cnt)
  291. if (resultbuf->s_aliases[cnt][aliases_len[cnt] - 1]
  292. != '\0')
  293. break;
  294. cnt < serv_resp.s_aliases_cnt; }))
  295. {
  296. /* We cannot use the database. */
  297. if (mapped->head->gc_cycle != gc_cycle)
  298. retval = -2;
  299. goto out_close;
  300. }
  301. retval = 0;
  302. *result = resultbuf;
  303. }
  304. }
  305. else
  306. {
  307. /* Set errno to 0 to indicate no error, just no found record. */
  308. __set_errno (0);
  309. /* Even though we have not found anything, the result is zero. */
  310. retval = 0;
  311. }
  312. out_close:
  313. if (sock != -1)
  314. __close_nocancel_nostatus (sock);
  315. out:
  316. if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
  317. {
  318. /* When we come here this means there has been a GC cycle while we
  319. were looking for the data. This means the data might have been
  320. inconsistent. Retry if possible. */
  321. if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
  322. {
  323. /* nscd is just running gc now. Disable using the mapping. */
  324. if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
  325. __nscd_unmap (mapped);
  326. mapped = NO_MAPPING;
  327. }
  328. if (retval != -1)
  329. {
  330. if (!alloca_aliases_len)
  331. free ((void *) aliases_len);
  332. goto retry;
  333. }
  334. }
  335. if (!alloca_aliases_len)
  336. free ((void *) aliases_len);
  337. if (!alloca_key)
  338. free (key);
  339. return retval;
  340. }