Branch data Line data Source code
1 : : /* Retrieve ELF / DWARF / source files from the debuginfod.
2 : : Copyright (C) 2019-2024 Red Hat, Inc.
3 : : Copyright (C) 2021, 2022 Mark J. Wielaard <mark@klomp.org>
4 : : This file is part of elfutils.
5 : :
6 : : This file is free software; you can redistribute it and/or modify
7 : : it under the terms of either
8 : :
9 : : * the GNU Lesser General Public License as published by the Free
10 : : Software Foundation; either version 3 of the License, or (at
11 : : your option) any later version
12 : :
13 : : or
14 : :
15 : : * the GNU General Public License as published by the Free
16 : : Software Foundation; either version 2 of the License, or (at
17 : : your option) any later version
18 : :
19 : : or both in parallel, as here.
20 : :
21 : : elfutils is distributed in the hope that it will be useful, but
22 : : WITHOUT ANY WARRANTY; without even the implied warranty of
23 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : : General Public License for more details.
25 : :
26 : : You should have received copies of the GNU General Public License and
27 : : the GNU Lesser General Public License along with this program. If
28 : : not, see <http://www.gnu.org/licenses/>. */
29 : :
30 : :
31 : : /* cargo-cult from libdwfl linux-kernel-modules.c */
32 : : /* In case we have a bad fts we include this before config.h because it
33 : : can't handle _FILE_OFFSET_BITS.
34 : : Everything we need here is fine if its declarations just come first.
35 : : Also, include sys/types.h before fts. On some systems fts.h is not self
36 : : contained. */
37 : : #ifdef BAD_FTS
38 : : #include <sys/types.h>
39 : : #include <fts.h>
40 : : #endif
41 : :
42 : : #include "config.h"
43 : : #include "debuginfod.h"
44 : : #include "system.h"
45 : : #include <ctype.h>
46 : : #include <errno.h>
47 : : #include <stdlib.h>
48 : : #include <gelf.h>
49 : :
50 : : #ifdef ENABLE_IMA_VERIFICATION
51 : : #include <openssl/sha.h>
52 : : #include <openssl/pem.h>
53 : : #include <openssl/evp.h>
54 : : #include <openssl/x509v3.h>
55 : : #include <arpa/inet.h>
56 : : #include <imaevm.h>
57 : : #endif
58 : : typedef enum {ignore, enforcing, undefined} ima_policy_t;
59 : :
60 : :
61 : : /* We might be building a bootstrap dummy library, which is really simple. */
62 : : #ifdef DUMMY_LIBDEBUGINFOD
63 : :
64 : : debuginfod_client *debuginfod_begin (void) { errno = ENOSYS; return NULL; }
65 : : int debuginfod_find_debuginfo (debuginfod_client *c, const unsigned char *b,
66 : : int s, char **p) { return -ENOSYS; }
67 : : int debuginfod_find_executable (debuginfod_client *c, const unsigned char *b,
68 : : int s, char **p) { return -ENOSYS; }
69 : : int debuginfod_find_source (debuginfod_client *c, const unsigned char *b,
70 : : int s, const char *f, char **p) { return -ENOSYS; }
71 : : int debuginfod_find_section (debuginfod_client *c, const unsigned char *b,
72 : : int s, const char *scn, char **p)
73 : : { return -ENOSYS; }
74 : : int debuginfod_find_metadata (debuginfod_client *c,
75 : : const char *k, const char *v, char **p) { return -ENOSYS; }
76 : : void debuginfod_set_progressfn(debuginfod_client *c,
77 : : debuginfod_progressfn_t fn) { }
78 : : void debuginfod_set_verbose_fd(debuginfod_client *c, int fd) { }
79 : : void debuginfod_set_user_data (debuginfod_client *c, void *d) { }
80 : : void* debuginfod_get_user_data (debuginfod_client *c) { return NULL; }
81 : : const char* debuginfod_get_url (debuginfod_client *c) { return NULL; }
82 : : int debuginfod_add_http_header (debuginfod_client *c,
83 : : const char *h) { return -ENOSYS; }
84 : : const char* debuginfod_get_headers (debuginfod_client *c) { return NULL; }
85 : : int debuginfod_default_progressfn (debuginfod_client *c, long a, long b)
86 : : { return 0; }
87 : :
88 : : void debuginfod_end (debuginfod_client *c) { }
89 : :
90 : : #else /* DUMMY_LIBDEBUGINFOD */
91 : :
92 : : #include <assert.h>
93 : : #include <dirent.h>
94 : : #include <stdio.h>
95 : : #include <errno.h>
96 : : #include <unistd.h>
97 : : #include <fcntl.h>
98 : : #include <fts.h>
99 : : #include <regex.h>
100 : : #include <string.h>
101 : : #include <stdbool.h>
102 : : #include <limits.h>
103 : : #include <time.h>
104 : : #include <utime.h>
105 : : #include <sys/syscall.h>
106 : : #include <sys/types.h>
107 : : #include <sys/stat.h>
108 : : #include <sys/utsname.h>
109 : : #include <curl/curl.h>
110 : : #include <fnmatch.h>
111 : : #include <json-c/json.h>
112 : :
113 : : #ifndef PATH_MAX
114 : : #define PATH_MAX 4096
115 : : #endif
116 : :
117 : : /* If fts.h is included before config.h, its indirect inclusions may not
118 : : give us the right LFS aliases of these functions, so map them manually. */
119 : : #ifdef BAD_FTS
120 : : #ifdef _FILE_OFFSET_BITS
121 : : #define open open64
122 : : #define fopen fopen64
123 : : #endif
124 : : #else
125 : : #include <sys/types.h>
126 : : #include <fts.h>
127 : : #endif
128 : :
129 : : /* Older curl.h don't define CURL_AT_LEAST_VERSION. */
130 : : #ifndef CURL_AT_LEAST_VERSION
131 : : #define CURL_VERSION_BITS(x,y,z) ((x)<<16|(y)<<8|(z))
132 : : #define CURL_AT_LEAST_VERSION(x,y,z) \
133 : : (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS(x, y, z))
134 : : #endif
135 : :
136 : : #include <pthread.h>
137 : :
138 : : static pthread_once_t init_control = PTHREAD_ONCE_INIT;
139 : : static bool curl_has_https; // = false
140 : :
141 : : static void
142 : 524 : libcurl_init(void)
143 : : {
144 : 524 : curl_global_init(CURL_GLOBAL_DEFAULT);
145 : :
146 : 524 : for (const char *const *protocol = curl_version_info(CURLVERSION_NOW)->protocols;
147 [ + + ]: 17292 : *protocol != NULL; ++protocol)
148 : : {
149 [ + + ]: 16768 : if(strcmp("https", *protocol) == 0)
150 : 524 : curl_has_https = true;
151 : : }
152 : 524 : }
153 : :
154 : :
155 : : #ifdef ENABLE_IMA_VERIFICATION
156 : : struct public_key_entry
157 : : {
158 : : struct public_key_entry *next; /* singly-linked list */
159 : : uint32_t keyid; /* last 4 bytes of sha1 of public key */
160 : : EVP_PKEY *key; /* openssl */
161 : : };
162 : : #endif
163 : :
164 : :
165 : : struct debuginfod_client
166 : : {
167 : : /* Progress/interrupt callback function. */
168 : : debuginfod_progressfn_t progressfn;
169 : :
170 : : /* Stores user data. */
171 : : void* user_data;
172 : :
173 : : /* Stores current/last url, if any. */
174 : : char* url;
175 : :
176 : : /* Accumulates outgoing http header names/values. */
177 : : int user_agent_set_p; /* affects add_default_headers */
178 : : struct curl_slist *headers;
179 : :
180 : : /* Flags the default_progressfn having printed something that
181 : : debuginfod_end needs to terminate. */
182 : : int default_progressfn_printed_p;
183 : :
184 : : /* Indicates whether the last query was cancelled by progressfn. */
185 : : bool progressfn_cancel;
186 : :
187 : : /* File descriptor to output any verbose messages if > 0. */
188 : : int verbose_fd;
189 : :
190 : : /* Maintain a long-lived curl multi-handle, which keeps a
191 : : connection/tls/dns cache to recently seen servers. */
192 : : CURLM *server_mhandle;
193 : :
194 : : /* Can contain all other context, like cache_path, server_urls,
195 : : timeout or other info gotten from environment variables, the
196 : : handle data, etc. So those don't have to be reparsed and
197 : : recreated on each request. */
198 : : char * winning_headers;
199 : :
200 : : #ifdef ENABLE_IMA_VERIFICATION
201 : : /* IMA public keys */
202 : : struct public_key_entry *ima_public_keys;
203 : : #endif
204 : : };
205 : :
206 : :
207 : : /* The cache_clean_interval_s file within the debuginfod cache specifies
208 : : how frequently the cache should be cleaned. The file's st_mtime represents
209 : : the time of last cleaning. */
210 : : static const char *cache_clean_interval_filename = "cache_clean_interval_s";
211 : : static const long cache_clean_default_interval_s = 86400; /* 1 day */
212 : :
213 : : /* The cache_miss_default_s within the debuginfod cache specifies how
214 : : frequently the empty file should be released.*/
215 : : static const long cache_miss_default_s = 600; /* 10 min */
216 : : static const char *cache_miss_filename = "cache_miss_s";
217 : :
218 : : /* The cache_max_unused_age_s file within the debuginfod cache specifies the
219 : : the maximum time since last access that a file will remain in the cache. */
220 : : static const char *cache_max_unused_age_filename = "max_unused_age_s";
221 : : static const long cache_default_max_unused_age_s = 604800; /* 1 week */
222 : :
223 : : /* The metadata_retention_default_s file within the debuginfod cache
224 : : specifies how long metadata query results should be cached. */
225 : : static const long metadata_retention_default_s = 3600; /* 1 hour */
226 : : static const char *metadata_retention_filename = "metadata_retention_s";
227 : :
228 : : /* Location of the cache of files downloaded from debuginfods.
229 : : The default parent directory is $HOME, or '/' if $HOME doesn't exist. */
230 : : static const char *cache_default_name = ".debuginfod_client_cache";
231 : : static const char *cache_xdg_name = "debuginfod_client";
232 : :
233 : : /* URLs of debuginfods, separated by url_delim. */
234 : : static const char *url_delim = " ";
235 : :
236 : : /* Timeout for debuginfods, in seconds. Applies separately to the server
237 : : connect phase and to download at least 100K once connection is
238 : : established. */
239 : : static const long default_timeout = 90;
240 : :
241 : : /* Default retry count for download error. */
242 : : static const long default_retry_limit = 2;
243 : :
244 : : /* Data associated with a particular CURL easy handle. Passed to
245 : : the write callback. */
246 : : struct handle_data
247 : : {
248 : : /* Cache file to be written to in case query is successful. */
249 : : int fd;
250 : :
251 : : /* URL queried by this handle. */
252 : : char url[PATH_MAX];
253 : :
254 : : /* error buffer for this handle. */
255 : : char errbuf[CURL_ERROR_SIZE];
256 : :
257 : : /* This handle. */
258 : : CURL *handle;
259 : :
260 : : /* The client object whom we're serving. */
261 : : debuginfod_client *client;
262 : :
263 : : /* Pointer to handle that should write to fd. Initially points to NULL,
264 : : then points to the first handle that begins writing the target file
265 : : to the cache. Used to ensure that a file is not downloaded from
266 : : multiple servers unnecessarily. */
267 : : CURL **target_handle;
268 : :
269 : : /* Response http headers for this client handle, sent from the server */
270 : : char *response_data;
271 : : size_t response_data_size;
272 : :
273 : : /* Response metadata values for this client handle, sent from the server */
274 : : char *metadata;
275 : : size_t metadata_size;
276 : : };
277 : :
278 : :
279 : :
280 : : #ifdef ENABLE_IMA_VERIFICATION
281 : : static inline unsigned char hex2dec(char c)
282 : : {
283 : : if (c >= '0' && c <= '9') return (c - '0');
284 : : if (c >= 'a' && c <= 'f') return (c - 'a') + 10;
285 : : if (c >= 'A' && c <= 'F') return (c - 'A') + 10;
286 : : return 0;
287 : : }
288 : :
289 : : static inline ima_policy_t ima_policy_str2enum(const char* ima_pol)
290 : : {
291 : : if (NULL == ima_pol) return undefined;
292 : : if (0 == strcmp(ima_pol, "ignore")) return ignore;
293 : : if (0 == strcmp(ima_pol, "enforcing")) return enforcing;
294 : : return undefined;
295 : : }
296 : :
297 : : static inline const char* ima_policy_enum2str(ima_policy_t ima_pol)
298 : : {
299 : : switch (ima_pol)
300 : : {
301 : : case ignore:
302 : : return "ignore";
303 : : case enforcing:
304 : : return "enforcing";
305 : : case undefined:
306 : : return "undefined";
307 : : }
308 : : return "";
309 : : }
310 : :
311 : :
312 : : static uint32_t extract_skid_pk(EVP_PKEY *pkey) // compute keyid by public key hashing
313 : : {
314 : : if (!pkey) return 0;
315 : : uint32_t keyid = 0;
316 : : X509_PUBKEY *pk = NULL;
317 : : const unsigned char *public_key = NULL;
318 : : int len;
319 : : if (X509_PUBKEY_set(&pk, pkey) &&
320 : : X509_PUBKEY_get0_param(NULL, &public_key, &len, NULL, pk))
321 : : {
322 : : uint8_t sha1[SHA_DIGEST_LENGTH];
323 : : SHA1(public_key, len, sha1);
324 : : memcpy(&keyid, sha1 + 16, 4);
325 : : }
326 : : X509_PUBKEY_free(pk);
327 : : return ntohl(keyid);
328 : : }
329 : :
330 : :
331 : : static uint32_t extract_skid(X509* x509) // compute keyid from cert or its public key
332 : : {
333 : : if (!x509) return 0;
334 : : uint32_t keyid = 0;
335 : : // Attempt to get the skid from the certificate
336 : : const ASN1_OCTET_STRING *skid_asn1_str = X509_get0_subject_key_id(x509);
337 : : if (skid_asn1_str)
338 : : {
339 : : int skid_len = ASN1_STRING_length(skid_asn1_str);
340 : : memcpy(&keyid, ASN1_STRING_get0_data(skid_asn1_str) + skid_len - sizeof(keyid), sizeof(keyid));
341 : : }
342 : : else // compute keyid ourselves by hashing public key
343 : : {
344 : : EVP_PKEY *pkey = X509_get0_pubkey(x509);
345 : : keyid = htonl(extract_skid_pk(pkey));
346 : : }
347 : : return ntohl(keyid);
348 : : }
349 : :
350 : :
351 : : static void load_ima_public_keys (debuginfod_client *c)
352 : : {
353 : : /* Iterate over the directories in DEBUGINFOD_IMA_CERT_PATH. */
354 : : char *cert_paths = getenv(DEBUGINFOD_IMA_CERT_PATH_ENV_VAR);
355 : : if (cert_paths == NULL || cert_paths[0] == '\0')
356 : : return;
357 : : cert_paths = strdup(cert_paths); // Modified during tokenization
358 : : if (cert_paths == NULL)
359 : : return;
360 : :
361 : : char* cert_dir_path;
362 : : DIR *dp;
363 : : struct dirent *entry;
364 : : int vfd = c->verbose_fd;
365 : :
366 : : char *strtok_context = NULL;
367 : : for(cert_dir_path = strtok_r(cert_paths, ":", &strtok_context);
368 : : cert_dir_path != NULL;
369 : : cert_dir_path = strtok_r(NULL, ":", &strtok_context))
370 : : {
371 : : dp = opendir(cert_dir_path);
372 : : if(!dp) continue;
373 : : while((entry = readdir(dp)))
374 : : {
375 : : // Only consider regular files with common x509 cert extensions
376 : : if(entry->d_type != DT_REG || 0 != fnmatch("*.@(der|pem|crt|cer|cert)", entry->d_name, FNM_EXTMATCH)) continue;
377 : : char certfile[PATH_MAX];
378 : : strncpy(certfile, cert_dir_path, PATH_MAX - 1);
379 : : if(certfile[strlen(certfile)-1] != '/') certfile[strlen(certfile)] = '/';
380 : : strncat(certfile, entry->d_name, PATH_MAX - strlen(certfile) - 1);
381 : : certfile[strlen(certfile)] = '\0';
382 : :
383 : : FILE *cert_fp = fopen(certfile, "r");
384 : : if(!cert_fp) continue;
385 : :
386 : : X509 *x509 = NULL;
387 : : EVP_PKEY *pkey = NULL;
388 : : char *fmt = "";
389 : : // Attempt to read the fp as DER
390 : : if(d2i_X509_fp(cert_fp, &x509))
391 : : fmt = "der ";
392 : : // Attempt to read the fp as PEM and assuming the key matches that of the signature add this key to be used
393 : : // Note we fseek since this is the second time we read from the fp
394 : : else if(0 == fseek(cert_fp, 0, SEEK_SET) && PEM_read_X509(cert_fp, &x509, NULL, NULL))
395 : : fmt = "pem "; // PEM with full certificate
396 : : else if(0 == fseek(cert_fp, 0, SEEK_SET) && PEM_read_PUBKEY(cert_fp, &pkey, NULL, NULL))
397 : : fmt = "pem "; // some PEM files have just a PUBLIC KEY in them
398 : : fclose(cert_fp);
399 : :
400 : : if (x509)
401 : : {
402 : : struct public_key_entry *ne = calloc(1, sizeof(struct public_key_entry));
403 : : if (ne)
404 : : {
405 : : ne->key = X509_extract_key(x509);
406 : : ne->keyid = extract_skid(x509);
407 : : ne->next = c->ima_public_keys;
408 : : c->ima_public_keys = ne;
409 : : if (vfd >= 0)
410 : : dprintf(vfd, "Loaded %scertificate %s, keyid = %04x\n", fmt, certfile, ne->keyid);
411 : : }
412 : : X509_free (x509);
413 : : }
414 : : else if (pkey)
415 : : {
416 : : struct public_key_entry *ne = calloc(1, sizeof(struct public_key_entry));
417 : : if (ne)
418 : : {
419 : : ne->key = pkey; // preserve refcount
420 : : ne->keyid = extract_skid_pk(pkey);
421 : : ne->next = c->ima_public_keys;
422 : : c->ima_public_keys = ne;
423 : : if (vfd >= 0)
424 : : dprintf(vfd, "Loaded %spubkey %s, keyid %04x\n", fmt, certfile, ne->keyid);
425 : : }
426 : : }
427 : : else
428 : : {
429 : : if (vfd >= 0)
430 : : dprintf(vfd, "Cannot load certificate %s\n", certfile);
431 : : }
432 : : } /* for each file in directory */
433 : : closedir(dp);
434 : : } /* for each directory */
435 : :
436 : : free(cert_paths);
437 : : }
438 : :
439 : :
440 : : static void free_ima_public_keys (debuginfod_client *c)
441 : : {
442 : : while (c->ima_public_keys)
443 : : {
444 : : EVP_PKEY_free (c->ima_public_keys->key);
445 : : struct public_key_entry *oen = c->ima_public_keys->next;
446 : : free (c->ima_public_keys);
447 : : c->ima_public_keys = oen;
448 : : }
449 : : }
450 : : #endif
451 : :
452 : :
453 : :
454 : : static size_t
455 : 10443 : debuginfod_write_callback (char *ptr, size_t size, size_t nmemb, void *data)
456 : : {
457 : 10443 : ssize_t count = size * nmemb;
458 : :
459 : 10443 : struct handle_data *d = (struct handle_data*)data;
460 : :
461 : : /* Indicate to other handles that they can abort their transfer. */
462 [ + + ]: 10443 : if (*d->target_handle == NULL)
463 : : {
464 : 1460 : *d->target_handle = d->handle;
465 : : /* update the client object */
466 : 1460 : const char *url = NULL;
467 : 1460 : CURLcode curl_res = curl_easy_getinfo (d->handle,
468 : : CURLINFO_EFFECTIVE_URL, &url);
469 [ + - + - ]: 1460 : if (curl_res == CURLE_OK && url)
470 : : {
471 : 1460 : free (d->client->url);
472 : 1460 : d->client->url = strdup(url); /* ok if fails */
473 : : }
474 : : }
475 : :
476 : : /* If this handle isn't the target handle, abort transfer. */
477 [ + - ]: 10443 : if (*d->target_handle != d->handle)
478 : : return -1;
479 : :
480 : 10443 : return (size_t) write(d->fd, (void*)ptr, count);
481 : : }
482 : :
483 : : /* handle config file read and write */
484 : : static int
485 : 2118 : debuginfod_config_cache(debuginfod_client *c, char *config_path,
486 : : long cache_config_default_s,
487 : : struct stat *st)
488 : : {
489 : 2118 : int fd = open(config_path, O_CREAT | O_RDWR, DEFFILEMODE);
490 [ - + ]: 2118 : if (fd < 0)
491 : 0 : return -errno;
492 : :
493 [ - + ]: 2118 : if (fstat (fd, st) < 0)
494 : : {
495 : 0 : int ret = -errno;
496 : 0 : close (fd);
497 : 0 : return ret;
498 : : }
499 : :
500 [ + + ]: 2118 : if (st->st_size == 0)
501 : : {
502 [ - + ]: 82 : if (dprintf(fd, "%ld", cache_config_default_s) < 0)
503 : : {
504 : 0 : int ret = -errno;
505 : 0 : close (fd);
506 : 0 : return ret;
507 : : }
508 : :
509 : 82 : close (fd);
510 : 82 : return cache_config_default_s;
511 : : }
512 : :
513 : 2036 : long cache_config;
514 : : /* PR29696 - NB: When using fdopen, the file descriptor is NOT
515 : : dup'ed and will be closed when the stream is closed. Manually
516 : : closing fd after fclose is called will lead to a race condition
517 : : where, if reused, the file descriptor will compete for its
518 : : regular use before being incorrectly closed here. */
519 : 2036 : FILE *config_file = fdopen(fd, "r");
520 [ + - ]: 2036 : if (config_file)
521 : : {
522 [ - + ]: 2036 : if (fscanf(config_file, "%ld", &cache_config) != 1)
523 : 0 : cache_config = cache_config_default_s;
524 [ - + - - ]: 2036 : if (0 != fclose (config_file) && c->verbose_fd >= 0)
525 : 0 : dprintf (c->verbose_fd, "fclose failed with %s (err=%d)\n",
526 : 0 : strerror (errno), errno);
527 : : }
528 : : else
529 : : {
530 : 0 : cache_config = cache_config_default_s;
531 [ # # # # ]: 0 : if (0 != close (fd) && c->verbose_fd >= 0)
532 : 0 : dprintf (c->verbose_fd, "close failed with %s (err=%d)\n",
533 : 0 : strerror (errno), errno);
534 : : }
535 : 2036 : return cache_config;
536 : : }
537 : :
538 : : /* Delete any files that have been unmodied for a period
539 : : longer than $DEBUGINFOD_CACHE_CLEAN_INTERVAL_S. */
540 : : static int
541 : 2100 : debuginfod_clean_cache(debuginfod_client *c,
542 : : char *cache_path, char *interval_path,
543 : : char *max_unused_path)
544 : : {
545 : 2100 : time_t clean_interval, max_unused_age;
546 : 2100 : int rc = -1;
547 : 2100 : struct stat st;
548 : :
549 : : /* Create new interval file. */
550 : 2100 : rc = debuginfod_config_cache(c, interval_path,
551 : : cache_clean_default_interval_s, &st);
552 [ + - ]: 2100 : if (rc < 0)
553 : : return rc;
554 : 2100 : clean_interval = (time_t)rc;
555 : :
556 : : /* Check timestamp of interval file to see whether cleaning is necessary. */
557 [ + + ]: 2100 : if (time(NULL) - st.st_mtime < clean_interval)
558 : : /* Interval has not passed, skip cleaning. */
559 : : return 0;
560 : :
561 : : /* Update timestamp representing when the cache was last cleaned.
562 : : Do it at the start to reduce the number of threads trying to do a
563 : : cleanup simultaneously. */
564 : 2 : utime (interval_path, NULL);
565 : :
566 : : /* Read max unused age value from config file. */
567 : 2 : rc = debuginfod_config_cache(c, max_unused_path,
568 : : cache_default_max_unused_age_s, &st);
569 [ + - ]: 2 : if (rc < 0)
570 : : return rc;
571 : 2 : max_unused_age = (time_t)rc;
572 : :
573 : 2 : char * const dirs[] = { cache_path, NULL, };
574 : :
575 : 2 : FTS *fts = fts_open(dirs, 0, NULL);
576 [ - + ]: 2 : if (fts == NULL)
577 : 0 : return -errno;
578 : :
579 : 2 : regex_t re;
580 : 2 : const char * pattern = ".*/(metadata.*|[a-f0-9]+(/hdr.*|/debuginfo|/executable|/source.*|))$"; /* include dirs */
581 : : /* NB: also matches .../section/ subdirs, so extracted section files also get cleaned. */
582 [ + - ]: 2 : if (regcomp (&re, pattern, REG_EXTENDED | REG_NOSUB) != 0)
583 : : return -ENOMEM;
584 : :
585 : 2 : FTSENT *f;
586 : 2 : long files = 0;
587 : 2 : time_t now = time(NULL);
588 [ + + ]: 28 : while ((f = fts_read(fts)) != NULL)
589 : : {
590 : : /* ignore any files that do not match the pattern. */
591 [ + + ]: 24 : if (regexec (&re, f->fts_path, 0, NULL, 0) != 0)
592 : 16 : continue;
593 : :
594 : 8 : files++;
595 [ + - ]: 8 : if (c->progressfn) /* inform/check progress callback */
596 [ + - ]: 8 : if ((c->progressfn) (c, files, 0))
597 : : break;
598 : :
599 [ + - + ]: 8 : switch (f->fts_info)
600 : : {
601 : 0 : case FTS_F:
602 : : /* delete file if max_unused_age has been met or exceeded w.r.t. atime. */
603 [ # # ]: 0 : if (now - f->fts_statp->st_atime >= max_unused_age)
604 : 0 : (void) unlink (f->fts_path);
605 : : break;
606 : :
607 : 4 : case FTS_DP:
608 : : /* Remove if old & empty. Weaken race against concurrent creation by
609 : : checking mtime. */
610 [ - + ]: 4 : if (now - f->fts_statp->st_mtime >= max_unused_age)
611 : 4 : (void) rmdir (f->fts_path);
612 : : break;
613 : :
614 : : default:
615 : 26 : ;
616 : : }
617 : : }
618 : 2 : fts_close (fts);
619 : 2 : regfree (&re);
620 : :
621 : 2 : return 0;
622 : : }
623 : :
624 : :
625 : : #define MAX_BUILD_ID_BYTES 64
626 : :
627 : :
628 : : static void
629 : 2124 : add_default_headers(debuginfod_client *client)
630 : : {
631 [ + + ]: 2124 : if (client->user_agent_set_p)
632 : 560 : return;
633 : :
634 : : /* Compute a User-Agent: string to send. The more accurately this
635 : : describes this host, the likelier that the debuginfod servers
636 : : might be able to locate debuginfo for us. */
637 : :
638 : 1564 : char* utspart = NULL;
639 : 1564 : struct utsname uts;
640 : 1564 : int rc = 0;
641 : 1564 : rc = uname (&uts);
642 [ + - ]: 1564 : if (rc == 0)
643 : 1564 : rc = asprintf(& utspart, "%s/%s", uts.sysname, uts.machine);
644 [ - + ]: 1564 : if (rc < 0)
645 : 0 : utspart = NULL;
646 : :
647 : 1564 : FILE *f = fopen ("/etc/os-release", "r");
648 [ - + ]: 1564 : if (f == NULL)
649 : 0 : f = fopen ("/usr/lib/os-release", "r");
650 : 1564 : char *id = NULL;
651 : 1564 : char *version = NULL;
652 [ - + ]: 1564 : if (f != NULL)
653 : : {
654 [ + + ]: 12512 : while (id == NULL || version == NULL)
655 : : {
656 : 10948 : char buf[128];
657 : 10948 : char *s = &buf[0];
658 [ + - ]: 10948 : if (fgets (s, sizeof(buf), f) == NULL)
659 : : break;
660 : :
661 : 10948 : int len = strlen (s);
662 [ - + ]: 10948 : if (len < 3)
663 : 0 : continue;
664 [ + - ]: 10948 : if (s[len - 1] == '\n')
665 : : {
666 : 10948 : s[len - 1] = '\0';
667 : 10948 : len--;
668 : : }
669 : :
670 : 10948 : char *v = strchr (s, '=');
671 [ + - - + ]: 10948 : if (v == NULL || strlen (v) < 2)
672 : 0 : continue;
673 : :
674 : : /* Split var and value. */
675 : 10948 : *v = '\0';
676 : 10948 : v++;
677 : :
678 : : /* Remove optional quotes around value string. */
679 [ + + ]: 10948 : if (*v == '"' || *v == '\'')
680 : : {
681 : 6256 : v++;
682 : 6256 : s[len - 1] = '\0';
683 : : }
684 [ + - + + ]: 10948 : if (id == NULL && strcmp (s, "ID") == 0)
685 : 1564 : id = strdup (v);
686 [ + + + + ]: 10948 : if (version == NULL && strcmp (s, "VERSION_ID") == 0)
687 : 1564 : version = strdup (v);
688 : : }
689 : 1564 : fclose (f);
690 : : }
691 : :
692 : 1564 : char *ua = NULL;
693 [ + - + - ]: 3128 : rc = asprintf(& ua, "User-Agent: %s/%s,%s,%s/%s",
694 : : PACKAGE_NAME, PACKAGE_VERSION,
695 [ - + ]: 1564 : utspart ?: "",
696 : : id ?: "",
697 : : version ?: "");
698 [ - + ]: 1564 : if (rc < 0)
699 : 0 : ua = NULL;
700 : :
701 [ + - ]: 1564 : if (ua)
702 : 1564 : (void) debuginfod_add_http_header (client, ua);
703 : :
704 : 1564 : free (ua);
705 : 1564 : free (id);
706 : 1564 : free (version);
707 : 1564 : free (utspart);
708 : : }
709 : :
710 : : /* Add HTTP headers found in the given file, one per line. Blank lines or invalid
711 : : * headers are ignored.
712 : : */
713 : : static void
714 : 0 : add_headers_from_file(debuginfod_client *client, const char* filename)
715 : : {
716 : 0 : int vds = client->verbose_fd;
717 : 0 : FILE *f = fopen (filename, "r");
718 [ # # ]: 0 : if (f == NULL)
719 : : {
720 [ # # ]: 0 : if (vds >= 0)
721 : 0 : dprintf(vds, "header file %s: %s\n", filename, strerror(errno));
722 : 0 : return;
723 : : }
724 : :
725 : 0 : while (1)
726 : 0 : {
727 : 0 : char buf[8192];
728 : 0 : char *s = &buf[0];
729 [ # # ]: 0 : if (feof(f))
730 : : break;
731 [ # # ]: 0 : if (fgets (s, sizeof(buf), f) == NULL)
732 : : break;
733 [ # # ]: 0 : for (char *c = s; *c != '\0'; ++c)
734 [ # # ]: 0 : if (!isspace(*c))
735 : 0 : goto nonempty;
736 : 0 : continue;
737 : 0 : nonempty:
738 : 0 : ;
739 : 0 : size_t last = strlen(s)-1;
740 [ # # ]: 0 : if (s[last] == '\n')
741 : 0 : s[last] = '\0';
742 : 0 : int rc = debuginfod_add_http_header(client, s);
743 [ # # ]: 0 : if (rc < 0 && vds >= 0)
744 : 0 : dprintf(vds, "skipping bad header: %s\n", strerror(-rc));
745 : : }
746 : 0 : fclose (f);
747 : : }
748 : :
749 : :
750 : : #define xalloc_str(p, fmt, args...) \
751 : : do \
752 : : { \
753 : : if (asprintf (&p, fmt, args) < 0) \
754 : : { \
755 : : p = NULL; \
756 : : rc = -ENOMEM; \
757 : : goto out; \
758 : : } \
759 : : } while (0)
760 : :
761 : :
762 : : /* Offer a basic form of progress tracing */
763 : : int
764 : 2 : debuginfod_default_progressfn (debuginfod_client *c, long a, long b)
765 : : {
766 : 2 : const char* url = debuginfod_get_url (c);
767 : 2 : int len = 0;
768 : :
769 : : /* We prefer to print the host part of the URL to keep the
770 : : message short. */
771 : 2 : if (url != NULL)
772 : : {
773 : 2 : const char* buildid = strstr(url, "buildid/");
774 [ + - ]: 2 : if (buildid != NULL)
775 : 2 : len = (buildid - url);
776 : : else
777 : 0 : len = strlen(url);
778 : : }
779 : :
780 [ - + ]: 2 : if (b == 0 || url==NULL) /* early stage */
781 : 0 : dprintf(STDERR_FILENO,
782 : 0 : "\rDownloading %c", "-/|\\"[a % 4]);
783 [ - + ]: 2 : else if (b < 0) /* download in progress but unknown total length */
784 : 0 : dprintf(STDERR_FILENO,
785 : : "\rDownloading from %.*s %ld",
786 : : len, url, a);
787 : : else /* download in progress, and known total length */
788 : 2 : dprintf(STDERR_FILENO,
789 : : "\rDownloading from %.*s %ld/%ld",
790 : : len, url, a, b);
791 : 2 : c->default_progressfn_printed_p = 1;
792 : :
793 : 2 : return 0;
794 : : }
795 : :
796 : : /* This is a callback function that receives http response headers in buffer for use
797 : : * in this program. https://curl.se/libcurl/c/CURLOPT_HEADERFUNCTION.html is the
798 : : * online documentation.
799 : : */
800 : : static size_t
801 : 13855 : header_callback (char * buffer, size_t size, size_t numitems, void * userdata)
802 : : {
803 : 13855 : struct handle_data *data = (struct handle_data *) userdata;
804 [ - + ]: 13855 : if (size != 1)
805 : : return 0;
806 [ + - ]: 13855 : if (data->client
807 [ + + ]: 13855 : && data->client->verbose_fd >= 0
808 [ + + ]: 10000 : && numitems > 2)
809 : 8886 : dprintf (data->client->verbose_fd, "header %.*s", (int)numitems, buffer);
810 : : // Some basic checks to ensure the headers received are of the expected format
811 [ + + ]: 13855 : if (strncasecmp(buffer, "X-DEBUGINFOD", 11)
812 [ + + ]: 3218 : || buffer[numitems-2] != '\r'
813 [ + - ]: 3216 : || buffer[numitems-1] != '\n'
814 [ - + ]: 3216 : || (buffer == strstr(buffer, ":")) ){
815 : : return numitems;
816 : : }
817 : : /* Temporary buffer for realloc */
818 : 3216 : char *temp = NULL;
819 : 3216 : temp = realloc(data->response_data, data->response_data_size + numitems);
820 [ - + ]: 3216 : if (temp == NULL)
821 : : return 0;
822 : :
823 : 3216 : memcpy(temp + data->response_data_size, buffer, numitems-1);
824 : 3216 : data->response_data = temp;
825 : 3216 : data->response_data_size += numitems-1;
826 : 3216 : data->response_data[data->response_data_size-1] = '\n';
827 : 3216 : data->response_data[data->response_data_size] = '\0';
828 : 3216 : return numitems;
829 : : }
830 : :
831 : :
832 : : static size_t
833 : 20 : metadata_callback (char * buffer, size_t size, size_t numitems, void * userdata)
834 : : {
835 [ - + ]: 20 : if (size != 1)
836 : : return 0;
837 : : /* Temporary buffer for realloc */
838 : 20 : char *temp = NULL;
839 : 20 : struct handle_data *data = (struct handle_data *) userdata;
840 : 20 : temp = realloc(data->metadata, data->metadata_size + numitems + 1);
841 [ - + ]: 20 : if (temp == NULL)
842 : : return 0;
843 : :
844 : 20 : memcpy(temp + data->metadata_size, buffer, numitems);
845 : 20 : data->metadata = temp;
846 : 20 : data->metadata_size += numitems;
847 : 20 : data->metadata[data->metadata_size] = '\0';
848 : 20 : return numitems;
849 : : }
850 : :
851 : :
852 : : /* This function takes a copy of DEBUGINFOD_URLS, server_urls, and
853 : : * separates it into an array of urls to query, each with a
854 : : * corresponding IMA policy. The url_subdir is either 'buildid' or
855 : : * 'metadata', corresponding to the query type. Returns 0 on success
856 : : * and -Posix error on failure.
857 : : */
858 : : static int
859 : 2060 : init_server_urls(char* url_subdir, const char* type,
860 : : char *server_urls, char ***server_url_list, ima_policy_t **url_ima_policies,
861 : : int *num_urls, int vfd)
862 : : {
863 : : /* Initialize the memory to zero */
864 : 2060 : char *strtok_saveptr;
865 : 2060 : ima_policy_t verification_mode = ignore; // The default mode
866 : 2060 : char *server_url = strtok_r(server_urls, url_delim, &strtok_saveptr);
867 : : /* Count number of URLs. */
868 : 2060 : int n = 0;
869 : :
870 [ + + ]: 4186 : while (server_url != NULL)
871 : : {
872 : : // When we encountered a (well-formed) token off the form
873 : : // ima:foo, we update the policy under which results from that
874 : : // server will be ima verified
875 [ - + ]: 2126 : if (startswith(server_url, "ima:"))
876 : : {
877 : : #ifdef ENABLE_IMA_VERIFICATION
878 : : ima_policy_t m = ima_policy_str2enum(server_url + strlen("ima:"));
879 : : if(m != undefined)
880 : : verification_mode = m;
881 : : else if (vfd >= 0)
882 : : dprintf(vfd, "IMA mode not recognized, skipping %s\n", server_url);
883 : : #else
884 [ # # ]: 0 : if (vfd >= 0)
885 : 0 : dprintf(vfd, "IMA signature verification is not enabled, treating %s as ima:ignore\n", server_url);
886 : : #endif
887 : 0 : goto continue_next_url;
888 : : }
889 : :
890 : 2126 : if (verification_mode==enforcing &&
891 : : 0==strcmp(url_subdir, "buildid") &&
892 : : 0==strcmp(type,"section")) // section queries are unsecurable
893 : : {
894 : : if (vfd >= 0)
895 : : dprintf(vfd, "skipping server %s section query in IMA enforcing mode\n", server_url);
896 : : goto continue_next_url;
897 : : }
898 : :
899 : : // Construct actual URL for libcurl
900 : 2126 : int r;
901 : 2126 : char *tmp_url;
902 [ + - + + ]: 2126 : if (strlen(server_url) > 1 && server_url[strlen(server_url)-1] == '/')
903 : 1112 : r = asprintf(&tmp_url, "%s%s", server_url, url_subdir);
904 : : else
905 : 1014 : r = asprintf(&tmp_url, "%s/%s", server_url, url_subdir);
906 : :
907 [ + - ]: 2126 : if (r == -1)
908 : 0 : return -ENOMEM;
909 : :
910 : : /* PR 27983: If the url is duplicate, skip it */
911 : : int url_index;
912 [ + + ]: 2260 : for (url_index = 0; url_index < n; ++url_index)
913 : : {
914 [ + + ]: 138 : if(strcmp(tmp_url, (*server_url_list)[url_index]) == 0)
915 : : {
916 : : url_index = -1;
917 : : break;
918 : : }
919 : : }
920 [ + + ]: 2126 : if (url_index == -1)
921 : : {
922 [ + - ]: 4 : if (vfd >= 0)
923 : 4 : dprintf(vfd, "duplicate url: %s, skipping\n", tmp_url);
924 : 4 : free(tmp_url);
925 : : }
926 : : else
927 : : {
928 : : /* Have unique URL, save it, along with its IMA verification tag. */
929 : 2122 : n ++;
930 [ + - ]: 2122 : if (NULL == (*server_url_list = reallocarray(*server_url_list, n, sizeof(char*)))
931 [ - + ]: 2122 : || NULL == (*url_ima_policies = reallocarray(*url_ima_policies, n, sizeof(ima_policy_t))))
932 : : {
933 : 0 : free (tmp_url);
934 : 0 : return -ENOMEM;
935 : : }
936 : 2122 : (*server_url_list)[n-1] = tmp_url;
937 : 2122 : if(NULL != url_ima_policies) (*url_ima_policies)[n-1] = verification_mode;
938 : : }
939 : :
940 : 2126 : continue_next_url:
941 : 2126 : server_url = strtok_r(NULL, url_delim, &strtok_saveptr);
942 : : }
943 : 2060 : *num_urls = n;
944 : 2060 : return 0;
945 : : }
946 : :
947 : : /* Some boilerplate for checking curl_easy_setopt. */
948 : : #define curl_easy_setopt_ck(H,O,P) do { \
949 : : CURLcode curl_res = curl_easy_setopt (H,O,P); \
950 : : if (curl_res != CURLE_OK) \
951 : : { \
952 : : if (vfd >= 0) \
953 : : dprintf (vfd, \
954 : : "Bad curl_easy_setopt: %s\n", \
955 : : curl_easy_strerror(curl_res)); \
956 : : return -EINVAL; \
957 : : } \
958 : : } while (0)
959 : :
960 : :
961 : : /*
962 : : * This function initializes a CURL handle. It takes optional callbacks for the write
963 : : * function and the header function, which if defined will use userdata of type struct handle_data*.
964 : : * Specifically the data[i] within an array of struct handle_data's.
965 : : * Returns 0 on success and -Posix error on failure.
966 : : */
967 : : static int
968 : 3214 : init_handle(debuginfod_client *client,
969 : : size_t (*w_callback)(char *buffer, size_t size, size_t nitems, void *userdata),
970 : : size_t (*h_callback)(char *buffer, size_t size, size_t nitems, void *userdata),
971 : : struct handle_data *data, int i, long timeout,
972 : : int vfd)
973 : : {
974 : 3214 : data->handle = curl_easy_init();
975 [ + - ]: 3214 : if (data->handle == NULL)
976 : : return -ENETUNREACH;
977 : :
978 [ + + ]: 3214 : if (vfd >= 0)
979 : 1144 : dprintf (vfd, "url %d %s\n", i, data->url);
980 : :
981 : : /* Only allow http:// + https:// + file:// so we aren't being
982 : : redirected to some unsupported protocol.
983 : : libcurl will fail if we request a single protocol that is not
984 : : available. https missing is the most likely issue */
985 : : #if CURL_AT_LEAST_VERSION(7, 85, 0)
986 [ - + - + : 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_PROTOCOLS_STR,
- - ]
987 : : curl_has_https ? "https,http,file" : "http,file");
988 : : #else
989 : : curl_easy_setopt_ck(data->handle, CURLOPT_PROTOCOLS,
990 : : ((curl_has_https ? CURLPROTO_HTTPS : 0) | CURLPROTO_HTTP | CURLPROTO_FILE));
991 : : #endif
992 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_URL, data->url);
993 [ + + ]: 3214 : if (vfd >= 0)
994 [ - + ]: 1144 : curl_easy_setopt_ck(data->handle, CURLOPT_ERRORBUFFER,
995 : : data->errbuf);
996 [ + - ]: 3214 : if (w_callback)
997 : : {
998 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle,
999 : : CURLOPT_WRITEFUNCTION, w_callback);
1000 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_WRITEDATA, data);
1001 : : }
1002 [ + - ]: 3214 : if (timeout > 0)
1003 : : {
1004 : : /* Make sure there is at least some progress,
1005 : : try to get at least 100K per timeout seconds. */
1006 [ - + - - ]: 3214 : curl_easy_setopt_ck (data->handle, CURLOPT_LOW_SPEED_TIME,
1007 : : timeout);
1008 [ - + - - ]: 3214 : curl_easy_setopt_ck (data->handle, CURLOPT_LOW_SPEED_LIMIT,
1009 : : 100 * 1024L);
1010 : :
1011 : : /* CURLOPT_LOW_SPEED_LIMIT/_TIME don't apply during the server connection
1012 : : phase. Set CURLOPT_CONNECTTIMEOUT_MS as well so the timeout also
1013 : : limits how long we wait for an unresponsive server. */
1014 : 6428 : long connect_timeout_ms = (timeout > LONG_MAX / 1000L
1015 [ + - ]: 3214 : ? LONG_MAX : timeout * 1000L);
1016 [ - + - - ]: 3214 : curl_easy_setopt_ck (data->handle, CURLOPT_CONNECTTIMEOUT_MS,
1017 : : connect_timeout_ms);
1018 : : }
1019 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_FILETIME, (long) 1);
1020 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_FOLLOWLOCATION, (long) 1);
1021 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_MAXREDIRS, (long) 6);
1022 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_FAILONERROR, (long) 1);
1023 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_NOSIGNAL, (long) 1);
1024 [ + - ]: 3214 : if (h_callback)
1025 : : {
1026 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle,
1027 : : CURLOPT_HEADERFUNCTION, h_callback);
1028 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_HEADERDATA, data);
1029 : : }
1030 : : #if LIBCURL_VERSION_NUM >= 0x072a00 /* 7.42.0 */
1031 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_PATH_AS_IS, (long) 1);
1032 : : #else
1033 : : /* On old curl; no big deal, canonicalization here is almost the
1034 : : same, except perhaps for ? # type decorations at the tail. */
1035 : : #endif
1036 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_AUTOREFERER, (long) 1);
1037 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_ACCEPT_ENCODING, "");
1038 [ - + - - ]: 3214 : curl_easy_setopt_ck(data->handle, CURLOPT_HTTPHEADER, client->headers);
1039 : :
1040 : : return 0;
1041 : : }
1042 : :
1043 : :
1044 : : /*
1045 : : * This function busy-waits on one or more curl queries to complete. This can
1046 : : * be controlled via only_one, which, if true, will find the first winner and exit
1047 : : * once found. If positive maxtime and maxsize dictate the maximum allowed wait times
1048 : : * and download sizes respectively. Returns 0 on success and -Posix error on failure.
1049 : : */
1050 : : static int
1051 : 3148 : perform_queries(CURLM *curlm, CURL **target_handle, struct handle_data *data, debuginfod_client *c,
1052 : : int num_urls, long maxtime, long maxsize, bool only_one, int vfd, int *committed_to)
1053 : : {
1054 : 3148 : int still_running = -1;
1055 : 3148 : long loops = 0;
1056 : 3148 : *committed_to = -1;
1057 : 3148 : bool verbose_reported = false;
1058 : 3148 : struct timespec start_time, cur_time;
1059 [ - + ]: 3148 : if (c->winning_headers != NULL)
1060 : : {
1061 : 0 : free (c->winning_headers);
1062 : 0 : c->winning_headers = NULL;
1063 : : }
1064 [ + + - + ]: 3148 : if (maxtime > 0 && clock_gettime(CLOCK_MONOTONIC_RAW, &start_time) == -1)
1065 : 0 : return -errno;
1066 : 7235 : long delta = 0;
1067 : 7235 : do
1068 : : {
1069 : : /* Check to see how long querying is taking. */
1070 [ + + ]: 7235 : if (maxtime > 0)
1071 : : {
1072 [ - + ]: 4 : if (clock_gettime(CLOCK_MONOTONIC_RAW, &cur_time) == -1)
1073 : 0 : return -errno;
1074 : 4 : delta = cur_time.tv_sec - start_time.tv_sec;
1075 [ - + ]: 4 : if ( delta > maxtime)
1076 : : {
1077 : 0 : dprintf(vfd, "Timeout with max time=%lds and transfer time=%lds\n", maxtime, delta );
1078 : 0 : return -ETIME;
1079 : : }
1080 : : }
1081 : : /* Wait 1 second, the minimum DEBUGINFOD_TIMEOUT. */
1082 : 7235 : curl_multi_wait(curlm, NULL, 0, 1000, NULL);
1083 : 7235 : CURLMcode curlm_res = curl_multi_perform(curlm, &still_running);
1084 : :
1085 [ + + ]: 7235 : if (only_one)
1086 : : {
1087 : : /* If the target file has been found, abort the other queries. */
1088 [ + - + + ]: 7164 : if (target_handle && *target_handle != NULL)
1089 : : {
1090 [ + + ]: 5580 : for (int i = 0; i < num_urls; i++)
1091 [ + + ]: 3462 : if (data[i].handle != *target_handle)
1092 : 1344 : curl_multi_remove_handle(curlm, data[i].handle);
1093 : : else
1094 : : {
1095 : 2118 : *committed_to = i;
1096 [ + + ]: 2118 : if (c->winning_headers == NULL)
1097 : : {
1098 : 1460 : c->winning_headers = data[*committed_to].response_data;
1099 [ + + + - ]: 1460 : if (vfd >= 0 && c->winning_headers != NULL)
1100 : 1104 : dprintf(vfd, "\n%s", c->winning_headers);
1101 : 1460 : data[*committed_to].response_data = NULL;
1102 : 1460 : data[*committed_to].response_data_size = 0;
1103 : : }
1104 : : }
1105 : : }
1106 : :
1107 [ + + + + ]: 7164 : if (vfd >= 0 && !verbose_reported && *committed_to >= 0)
1108 : : {
1109 [ - + - - ]: 2208 : bool pnl = (c->default_progressfn_printed_p && vfd == STDERR_FILENO);
1110 : 1104 : dprintf (vfd, "%scommitted to url %d\n", pnl ? "\n" : "",
1111 : : *committed_to);
1112 [ - + ]: 1104 : if (pnl)
1113 : 0 : c->default_progressfn_printed_p = 0;
1114 : : verbose_reported = true;
1115 : : }
1116 : : }
1117 : :
1118 [ - + ]: 7235 : if (curlm_res != CURLM_OK)
1119 : : {
1120 [ # # # ]: 0 : switch (curlm_res)
1121 : : {
1122 : 0 : case CURLM_CALL_MULTI_PERFORM: continue;
1123 : : case CURLM_OUT_OF_MEMORY: return -ENOMEM;
1124 : 0 : default: return -ENETUNREACH;
1125 : : }
1126 : : }
1127 : :
1128 : 7235 : long dl_size = -1;
1129 [ + + + + : 7235 : if (target_handle && *target_handle && (c->progressfn || maxsize > 0))
+ + - + ]
1130 : : {
1131 : : /* Get size of file being downloaded. NB: If going through
1132 : : deflate-compressing proxies, this number is likely to be
1133 : : unavailable, so -1 may show. */
1134 : 1026 : CURLcode curl_res;
1135 : : #if CURL_AT_LEAST_VERSION(7, 55, 0)
1136 : 1026 : curl_off_t cl;
1137 : 1026 : curl_res = curl_easy_getinfo(*target_handle,
1138 : : CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
1139 : : &cl);
1140 [ + - - + ]: 1026 : if (curl_res == CURLE_OK && cl >= 0)
1141 : : dl_size = (cl > LONG_MAX ? LONG_MAX : (long)cl);
1142 : : #else
1143 : : double cl;
1144 : : curl_res = curl_easy_getinfo(*target_handle,
1145 : : CURLINFO_CONTENT_LENGTH_DOWNLOAD,
1146 : : &cl);
1147 : : if (curl_res == CURLE_OK && cl >= 0)
1148 : : dl_size = (cl >= (double)(LONG_MAX+1UL) ? LONG_MAX : (long)cl);
1149 : : #endif
1150 : : /* If Content-Length is -1, try to get the size from
1151 : : X-Debuginfod-Size */
1152 [ # # ]: 0 : if (dl_size == -1 && c->winning_headers != NULL)
1153 : : {
1154 : 0 : long xdl;
1155 : 0 : char *hdr = strcasestr(c->winning_headers, "x-debuginfod-size");
1156 : 0 : size_t off = strlen("x-debuginfod-size:");
1157 : :
1158 [ # # # # ]: 0 : if (hdr != NULL && sscanf(hdr + off, "%ld", &xdl) == 1)
1159 : 0 : dl_size = xdl;
1160 : : }
1161 : : }
1162 : :
1163 [ + + ]: 7235 : if (c->progressfn) /* inform/check progress callback */
1164 : : {
1165 : 4981 : loops ++;
1166 : 4981 : long pa = loops; /* default param for progress callback */
1167 [ + + + + ]: 4981 : if (target_handle && *target_handle) /* we've committed to a server; report its download progress */
1168 : : {
1169 : : /* PR30809: Check actual size of cached file. This same
1170 : : fd is shared by all the multi-curl handles (but only
1171 : : one will end up writing to it). Another way could be
1172 : : to tabulate totals in debuginfod_write_callback(). */
1173 : 1026 : struct stat cached;
1174 : 1026 : int statrc = fstat(data[*committed_to].fd, &cached);
1175 [ + - ]: 1026 : if (statrc == 0)
1176 : 1026 : pa = (long) cached.st_size;
1177 : : else
1178 : : {
1179 : : /* Otherwise, query libcurl for its tabulated total.
1180 : : However, that counts http body length, not
1181 : : decoded/decompressed content length, so does not
1182 : : measure quite the same thing as dl. */
1183 : 0 : CURLcode curl_res;
1184 : : #if CURL_AT_LEAST_VERSION(7, 55, 0)
1185 : 0 : curl_off_t dl;
1186 : 0 : curl_res = curl_easy_getinfo(target_handle,
1187 : : CURLINFO_SIZE_DOWNLOAD_T,
1188 : : &dl);
1189 [ # # # # ]: 0 : if (curl_res == 0 && dl >= 0)
1190 : 0 : pa = (dl > LONG_MAX ? LONG_MAX : (long)dl);
1191 : : #else
1192 : : double dl;
1193 : : curl_res = curl_easy_getinfo(target_handle,
1194 : : CURLINFO_SIZE_DOWNLOAD,
1195 : : &dl);
1196 : : if (curl_res == 0)
1197 : : pa = (dl >= (double)(LONG_MAX+1UL) ? LONG_MAX : (long)dl);
1198 : : #endif
1199 : : }
1200 : :
1201 [ - + - + ]: 1026 : if ((*c->progressfn) (c, pa, dl_size == -1 ? 0 : dl_size))
1202 : : {
1203 : 0 : c->progressfn_cancel = true;
1204 : 0 : break;
1205 : : }
1206 : : }
1207 : : }
1208 : : /* Check to see if we are downloading something which exceeds maxsize, if set.*/
1209 [ + + + + : 7203 : if (target_handle && *target_handle && dl_size > maxsize && maxsize > 0)
- + ]
1210 : : {
1211 [ # # ]: 0 : if (vfd >=0)
1212 : 0 : dprintf(vfd, "Content-Length too large.\n");
1213 : 0 : return -EFBIG;
1214 : : }
1215 [ + + ]: 7235 : } while (still_running);
1216 : :
1217 : : return 0;
1218 : : }
1219 : :
1220 : :
1221 : : /* Copy SRC to DEST, s,/,#,g */
1222 : :
1223 : : static void
1224 : 1158 : path_escape (const char *src, char *dest, size_t dest_len)
1225 : : {
1226 : : /* PR32218: Reversibly-escaping src character-by-character, for
1227 : : large enough strings, risks ENAMETOOLONG errors. For long names,
1228 : : a simple hash based generated name instead, while still
1229 : : attempting to preserve the as much of the content as possible.
1230 : : It's possible that absurd choices of incoming names will collide
1231 : : or still get truncated, but c'est la vie.
1232 : : */
1233 : :
1234 : : /* Compute a three-way min() for the actual output string generated. */
1235 [ - + ]: 1158 : assert (dest_len > 10); /* Space enough for degenerate case of
1236 : : "HASHHASH-\0". NB: dest_len is not
1237 : : user-controlled. */
1238 : : /* Use only NAME_MAX/2 characters in the output file name.
1239 : : ENAMETOOLONG has been observed even on 300-ish character names on
1240 : : some filesystems. */
1241 : 1158 : const size_t max_dest_len = NAME_MAX/2;
1242 : 1158 : dest_len = dest_len > max_dest_len ? max_dest_len : dest_len;
1243 : : /* Use only strlen(src)+10 bytes, if that's smaller. Yes, we could
1244 : : just fit an entire escaped name in there in theory, without the
1245 : : hash+etc. But then again the hashing protects against #-escape
1246 : : aliasing collisions: "foo[bar" "foo]bar" both escape to
1247 : : "foo#bar", thus aliasing, but have different "HASH-foo#bar".
1248 : : */
1249 : 1158 : const size_t hash_prefix_destlen = strlen(src)+10; /* DEADBEEF-src\0 */
1250 : 1158 : dest_len = dest_len > hash_prefix_destlen ? hash_prefix_destlen : dest_len;
1251 : :
1252 : 1158 : char *dest_write = dest + dest_len - 1;
1253 : 1158 : (*dest_write--) = '\0'; /* Ensure a \0 there. */
1254 : :
1255 : : /* Copy from back toward front, preferring to keep the .extension. */
1256 [ + + ]: 87880 : for (int fi=strlen(src)-1; fi >= 0 && dest_write >= dest; fi--)
1257 : : {
1258 : 86722 : char src_char = src[fi];
1259 [ + + ]: 86722 : switch (src_char)
1260 : : {
1261 : : /* Pass through ordinary identifier chars. */
1262 : 77666 : case '.': case '-': case '_':
1263 : : case 'a'...'z':
1264 : : case 'A'...'Z':
1265 : : case '0'...'9':
1266 : 77666 : *dest_write-- = src_char;
1267 : 77666 : break;
1268 : :
1269 : : /* Replace everything else, esp. security-sensitive /. */
1270 : 9056 : default:
1271 : 9056 : *dest_write-- = '#';
1272 : 9056 : break;
1273 : : }
1274 : : }
1275 : :
1276 : : /* djb2 hash algorithm: DJBX33A */
1277 : : unsigned long hash = 5381;
1278 : : const char *c = src;
1279 [ + + ]: 88682 : while (*c)
1280 : 87524 : hash = ((hash << 5) + hash) + *c++;
1281 : 1158 : char name_hash_str [9];
1282 : : /* Truncate to 4 bytes; along with the remaining hundredish bytes of text,
1283 : : should be ample against accidental collisions. */
1284 : 1158 : snprintf (name_hash_str, sizeof(name_hash_str), "%08x", (unsigned int) hash);
1285 : 1158 : memcpy (&dest[0], name_hash_str, 8); /* Overwrite the first few characters */
1286 : 1158 : dest[8] = '-'; /* Add a bit of punctuation to make hash stand out */
1287 : 1158 : }
1288 : :
1289 : : /* Attempt to update the atime */
1290 : : static void
1291 : 114 : update_atime (int fd)
1292 : : {
1293 : 114 : struct timespec tvs[2];
1294 : :
1295 : 114 : tvs[0].tv_sec = tvs[1].tv_sec = 0;
1296 : 114 : tvs[0].tv_nsec = UTIME_NOW;
1297 : 114 : tvs[1].tv_nsec = UTIME_OMIT;
1298 : :
1299 : 114 : (void) futimens (fd, tvs); /* best effort */
1300 : 114 : }
1301 : :
1302 : : /* Attempt to read an ELF/DWARF section with name SECTION from FD and write
1303 : : it to a separate file in the debuginfod cache. If successful the absolute
1304 : : path of the separate file containing SECTION will be stored in USR_PATH.
1305 : : FD_PATH is the absolute path for FD.
1306 : :
1307 : : If the section cannot be extracted, then return a negative error code.
1308 : : -ENOENT indicates that the parent file was able to be read but the
1309 : : section name was not found. -EEXIST indicates that the section was
1310 : : found but had type SHT_NOBITS. */
1311 : :
1312 : : static int
1313 : 12 : extract_section (int fd, const char *section, char *fd_path, char **usr_path)
1314 : : {
1315 : 12 : elf_version (EV_CURRENT);
1316 : :
1317 : 12 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
1318 [ + - ]: 12 : if (elf == NULL)
1319 : : return -EIO;
1320 : :
1321 : 12 : size_t shstrndx;
1322 : 12 : int rc = elf_getshdrstrndx (elf, &shstrndx);
1323 [ - + ]: 12 : if (rc < 0)
1324 : : {
1325 : 0 : rc = -EIO;
1326 : 0 : goto out;
1327 : : }
1328 : :
1329 : 12 : int sec_fd = -1;
1330 : 12 : char *sec_path_tmp = NULL;
1331 : 12 : Elf_Scn *scn = NULL;
1332 : :
1333 : : /* Try to find the target section and copy the contents into a
1334 : : separate file. */
1335 : 424 : while (true)
1336 : 206 : {
1337 : 218 : scn = elf_nextscn (elf, scn);
1338 [ - + ]: 218 : if (scn == NULL)
1339 : : {
1340 : 0 : rc = -ENOENT;
1341 : 4 : goto out;
1342 : : }
1343 : 218 : GElf_Shdr shdr_storage;
1344 : 218 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_storage);
1345 [ - + ]: 218 : if (shdr == NULL)
1346 : : {
1347 : 0 : rc = -EIO;
1348 : 0 : goto out;
1349 : : }
1350 : :
1351 : 218 : const char *scn_name = elf_strptr (elf, shstrndx, shdr->sh_name);
1352 [ - + ]: 218 : if (scn_name == NULL)
1353 : : {
1354 : 0 : rc = -EIO;
1355 : 0 : goto out;
1356 : : }
1357 [ + + ]: 218 : if (strcmp (scn_name, section) == 0)
1358 : : {
1359 : : /* We found the desired section. */
1360 [ + + ]: 12 : if (shdr->sh_type == SHT_NOBITS)
1361 : : {
1362 : 4 : rc = -EEXIST;
1363 : 4 : goto out;
1364 : : }
1365 : :
1366 : 8 : Elf_Data *data = NULL;
1367 : 8 : data = elf_rawdata (scn, NULL);
1368 [ - + ]: 8 : if (data == NULL)
1369 : : {
1370 : 0 : rc = -EIO;
1371 : 0 : goto out;
1372 : : }
1373 : :
1374 [ - + ]: 8 : if (data->d_buf == NULL)
1375 : : {
1376 : 0 : rc = -EIO;
1377 : 0 : goto out;
1378 : : }
1379 : :
1380 : : /* Compute the absolute filename we'll write the section to.
1381 : : Replace the last component of FD_PATH with the path-escaped
1382 : : section filename. */
1383 : 8 : int i = strlen (fd_path);
1384 [ + - ]: 92 : while (i >= 0)
1385 : : {
1386 [ + + ]: 92 : if (fd_path[i] == '/')
1387 : : {
1388 : 8 : fd_path[i] = '\0';
1389 : 8 : break;
1390 : : }
1391 : 84 : --i;
1392 : : }
1393 : :
1394 : 8 : char suffix[NAME_MAX];
1395 : 8 : path_escape (section, suffix, sizeof(suffix));
1396 : 8 : rc = asprintf (&sec_path_tmp, "%s/section-%s.XXXXXX",
1397 : : fd_path, suffix);
1398 [ - + ]: 8 : if (rc == -1)
1399 : : {
1400 : 0 : rc = -ENOMEM;
1401 : 0 : goto out1;
1402 : : }
1403 : :
1404 : 8 : sec_fd = mkstemp (sec_path_tmp);
1405 [ - + ]: 8 : if (sec_fd < 0)
1406 : : {
1407 : 0 : rc = -EIO;
1408 : 8 : goto out2;
1409 : : }
1410 : :
1411 : 8 : ssize_t res = write_retry (sec_fd, data->d_buf, data->d_size);
1412 [ + - - + ]: 8 : if (res < 0 || (size_t) res != data->d_size)
1413 : : {
1414 : 0 : rc = -EIO;
1415 : 0 : goto out3;
1416 : : }
1417 : :
1418 : : /* Success. Rename tmp file and update USR_PATH. */
1419 : 8 : char *sec_path;
1420 [ - + ]: 8 : if (asprintf (&sec_path, "%s/section-%s", fd_path, section) == -1)
1421 : : {
1422 : 0 : rc = -ENOMEM;
1423 : 0 : goto out3;
1424 : : }
1425 : :
1426 : 8 : rc = rename (sec_path_tmp, sec_path);
1427 [ - + ]: 8 : if (rc < 0)
1428 : : {
1429 : 0 : free (sec_path);
1430 : 0 : rc = -EIO;
1431 : 0 : goto out3;
1432 : : }
1433 : :
1434 [ + - ]: 8 : if (usr_path != NULL)
1435 : 8 : *usr_path = sec_path;
1436 : : else
1437 : 0 : free (sec_path);
1438 : 8 : update_atime(fd);
1439 : 8 : rc = sec_fd;
1440 : 8 : goto out2;
1441 : : }
1442 : : }
1443 : :
1444 : 0 : out3:
1445 : 0 : close (sec_fd);
1446 : 0 : unlink (sec_path_tmp);
1447 : :
1448 : 8 : out2:
1449 : 8 : free (sec_path_tmp);
1450 : :
1451 : 12 : out1:
1452 : 12 : out:
1453 : 12 : elf_end (elf);
1454 : 12 : return rc;
1455 : : }
1456 : :
1457 : : /* Search TARGET_CACHE_DIR for a debuginfo or executable file containing
1458 : : an ELF/DWARF section with name SCN_NAME. If found, extract the section
1459 : : to a separate file in TARGET_CACHE_DIR and return a file descriptor
1460 : : for the section file. The path for this file will be stored in USR_PATH.
1461 : : Return a negative errno if unsuccessful. -ENOENT indicates that SCN_NAME
1462 : : is confirmed to not exist. */
1463 : :
1464 : : static int
1465 : 16 : cache_find_section (const char *scn_name, const char *target_cache_dir,
1466 : : char **usr_path)
1467 : : {
1468 : 16 : int debug_fd;
1469 : 16 : int rc = -EEXIST;
1470 : 16 : char parent_path[PATH_MAX];
1471 : :
1472 : : /* Check the debuginfo first. */
1473 : 16 : snprintf (parent_path, PATH_MAX, "%s/debuginfo", target_cache_dir);
1474 : 16 : debug_fd = open (parent_path, O_RDONLY);
1475 [ + + ]: 16 : if (debug_fd >= 0)
1476 : : {
1477 : 8 : rc = extract_section (debug_fd, scn_name, parent_path, usr_path);
1478 : 8 : close (debug_fd);
1479 : : }
1480 : :
1481 : : /* If the debuginfo file couldn't be found or the section type was
1482 : : SHT_NOBITS, check the executable. */
1483 [ + + ]: 8 : if (rc == -EEXIST)
1484 : : {
1485 : 12 : snprintf (parent_path, PATH_MAX, "%s/executable", target_cache_dir);
1486 : 12 : int exec_fd = open (parent_path, O_RDONLY);
1487 : :
1488 [ + + ]: 12 : if (exec_fd >= 0)
1489 : : {
1490 : 4 : rc = extract_section (exec_fd, scn_name, parent_path, usr_path);
1491 : 4 : close (exec_fd);
1492 : :
1493 : : /* Don't return -ENOENT if the debuginfo wasn't opened. The
1494 : : section may exist in the debuginfo but not the executable. */
1495 [ - + ]: 4 : if (debug_fd < 0 && rc == -ENOENT)
1496 : 0 : rc = -EREMOTE;
1497 : : }
1498 : : }
1499 : :
1500 : 16 : return rc;
1501 : : }
1502 : :
1503 : :
1504 : : #ifdef ENABLE_IMA_VERIFICATION
1505 : : /* Extract the hash algorithm name from the signature header, of which
1506 : : there are several types. The name will be used for openssl hashing
1507 : : of the file content. The header doesn't need to be super carefully
1508 : : parsed, because if any part of it is wrong, be it the hash
1509 : : algorithm number or hash value or whatever, it will fail
1510 : : computation or verification. Return NULL in case of error. */
1511 : : static const char*
1512 : : get_signature_params(debuginfod_client *c, unsigned char *bin_sig)
1513 : : {
1514 : : int hashalgo = 0;
1515 : :
1516 : : switch (bin_sig[0])
1517 : : {
1518 : : case EVM_IMA_XATTR_DIGSIG:
1519 : : #ifdef IMA_VERITY_DIGSIG /* missing on debian-i386 trybot */
1520 : : case IMA_VERITY_DIGSIG:
1521 : : #endif
1522 : : break;
1523 : : default:
1524 : : if (c->verbose_fd >= 0)
1525 : : dprintf (c->verbose_fd, "Unknown ima digsig %d\n", (int)bin_sig[0]);
1526 : : return NULL;
1527 : : }
1528 : :
1529 : : switch (bin_sig[1])
1530 : : {
1531 : : case DIGSIG_VERSION_2:
1532 : : {
1533 : : struct signature_v2_hdr hdr_v2;
1534 : : memcpy(& hdr_v2, & bin_sig[1], sizeof(struct signature_v2_hdr));
1535 : : hashalgo = hdr_v2.hash_algo;
1536 : : break;
1537 : : }
1538 : : default:
1539 : : if (c->verbose_fd >= 0)
1540 : : dprintf (c->verbose_fd, "Unknown ima signature version %d\n", (int)bin_sig[1]);
1541 : : return NULL;
1542 : : }
1543 : :
1544 : : switch (hashalgo)
1545 : : {
1546 : : case PKEY_HASH_SHA1: return "sha1";
1547 : : case PKEY_HASH_SHA256: return "sha256";
1548 : : // (could add many others from enum pkey_hash_algo)
1549 : : default:
1550 : : if (c->verbose_fd >= 0)
1551 : : dprintf (c->verbose_fd, "Unknown ima pkey hash %d\n", hashalgo);
1552 : : return NULL;
1553 : : }
1554 : : }
1555 : :
1556 : :
1557 : : /* Verify given hash against given signature blob. Return 0 on ok, -errno otherwise. */
1558 : : static int
1559 : : debuginfod_verify_hash(debuginfod_client *c, const unsigned char *hash, int size,
1560 : : const char *hash_algo, unsigned char *sig, int siglen)
1561 : : {
1562 : : int ret = -EBADMSG;
1563 : : struct public_key_entry *pkey;
1564 : : struct signature_v2_hdr hdr;
1565 : : EVP_PKEY_CTX *ctx;
1566 : : const EVP_MD *md;
1567 : :
1568 : : memcpy(&hdr, sig, sizeof(struct signature_v2_hdr)); /* avoid just aliasing */
1569 : :
1570 : : if (c->verbose_fd >= 0)
1571 : : dprintf (c->verbose_fd, "Searching for ima keyid %04x\n", ntohl(hdr.keyid));
1572 : :
1573 : : /* Find the matching public key. */
1574 : : for (pkey = c->ima_public_keys; pkey != NULL; pkey = pkey->next)
1575 : : if (pkey->keyid == ntohl(hdr.keyid)) break;
1576 : : if (!pkey)
1577 : : return -ENOKEY;
1578 : :
1579 : : if (!(ctx = EVP_PKEY_CTX_new(pkey->key, NULL)))
1580 : : goto err;
1581 : : if (!EVP_PKEY_verify_init(ctx))
1582 : : goto err;
1583 : : if (!(md = EVP_get_digestbyname(hash_algo)))
1584 : : goto err;
1585 : : if (!EVP_PKEY_CTX_set_signature_md(ctx, md))
1586 : : goto err;
1587 : : ret = EVP_PKEY_verify(ctx, sig + sizeof(hdr),
1588 : : siglen - sizeof(hdr), hash, size);
1589 : : if (ret == 1)
1590 : : ret = 0; // success!
1591 : : else if (ret == 0)
1592 : : ret = -EBADMSG;
1593 : : err:
1594 : : EVP_PKEY_CTX_free(ctx);
1595 : : return ret;
1596 : : }
1597 : :
1598 : :
1599 : :
1600 : : /* Validate an IMA file signature.
1601 : : * Returns 0 on signature validity, -EINVAL on signature invalidity, -ENOSYS on undefined imaevm machinery,
1602 : : * -ENOKEY on key issues, or other -errno.
1603 : : */
1604 : :
1605 : : static int
1606 : : debuginfod_validate_imasig (debuginfod_client *c, int fd)
1607 : : {
1608 : : int rc = ENOSYS;
1609 : :
1610 : : char* sig_buf = NULL;
1611 : : EVP_MD_CTX *ctx = NULL;
1612 : : if (!c || !c->winning_headers)
1613 : : {
1614 : : rc = -ENODATA;
1615 : : goto exit_validate;
1616 : : }
1617 : : // Extract the HEX IMA-signature from the header
1618 : : char* hdr_ima_sig = strcasestr(c->winning_headers, "x-debuginfod-imasignature");
1619 : : if (!hdr_ima_sig || 1 != sscanf(hdr_ima_sig + strlen("x-debuginfod-imasignature:"), "%ms", &sig_buf))
1620 : : {
1621 : : rc = -ENODATA;
1622 : : goto exit_validate;
1623 : : }
1624 : : if (strlen(sig_buf) > MAX_SIGNATURE_SIZE) // reject if too long
1625 : : {
1626 : : rc = -EBADMSG;
1627 : : goto exit_validate;
1628 : : }
1629 : : // Convert the hex signature to bin
1630 : : size_t bin_sig_len = strlen(sig_buf)/2;
1631 : : unsigned char bin_sig[MAX_SIGNATURE_SIZE/2];
1632 : : for (size_t b = 0; b < bin_sig_len; b++)
1633 : : bin_sig[b] = (hex2dec(sig_buf[2*b]) << 4) | hex2dec(sig_buf[2*b+1]);
1634 : :
1635 : : // Compute the binary digest of the cached file (with file descriptor fd)
1636 : : ctx = EVP_MD_CTX_new();
1637 : : const char* sighash_name = get_signature_params(c, bin_sig) ?: "";
1638 : : const EVP_MD *md = EVP_get_digestbyname(sighash_name);
1639 : : if (!ctx || !md || !EVP_DigestInit(ctx, md))
1640 : : {
1641 : : rc = -EBADMSG;
1642 : : goto exit_validate;
1643 : : }
1644 : :
1645 : : long data_len;
1646 : : char* hdr_data_len = strcasestr(c->winning_headers, "x-debuginfod-size");
1647 : : if (!hdr_data_len || 1 != sscanf(hdr_data_len + strlen("x-debuginfod-size:") , "%ld", &data_len))
1648 : : {
1649 : : rc = -ENODATA;
1650 : : goto exit_validate;
1651 : : }
1652 : :
1653 : : /* Don't trust the size the server sent us, double check against the
1654 : : file size that we actually got. That way we calculate the hash
1655 : : over the whole file and not a shorter (possibly empty) data size. */
1656 : : struct stat st;
1657 : : if (fstat (fd, &st) == -1)
1658 : : {
1659 : : rc = -errno;
1660 : : goto exit_validate;
1661 : : }
1662 : : if (data_len != st.st_size)
1663 : : {
1664 : : rc = -EBADMSG;
1665 : : goto exit_validate;
1666 : : }
1667 : :
1668 : : char file_data[DATA_SIZE]; // imaevm.h data chunk hash size
1669 : : ssize_t n;
1670 : : for(off_t k = 0; k < data_len; k += n)
1671 : : {
1672 : : if (-1 == (n = pread(fd, file_data, DATA_SIZE, k)))
1673 : : {
1674 : : rc = -errno;
1675 : : goto exit_validate;
1676 : : }
1677 : :
1678 : : if (!EVP_DigestUpdate(ctx, file_data, n))
1679 : : {
1680 : : rc = -EBADMSG;
1681 : : goto exit_validate;
1682 : : }
1683 : : }
1684 : :
1685 : : uint8_t bin_dig[MAX_DIGEST_SIZE];
1686 : : unsigned int bin_dig_len;
1687 : : if (!EVP_DigestFinal(ctx, bin_dig, &bin_dig_len))
1688 : : {
1689 : : rc = -EBADMSG;
1690 : : goto exit_validate;
1691 : : }
1692 : :
1693 : : // XXX: in case of DIGSIG_VERSION_3, need to hash the file hash, yo dawg
1694 : :
1695 : : int res = debuginfod_verify_hash(c,
1696 : : bin_dig, bin_dig_len,
1697 : : sighash_name,
1698 : : & bin_sig[1], bin_sig_len-1); // skip over first byte of signature
1699 : : if (c->verbose_fd >= 0)
1700 : : dprintf (c->verbose_fd, "Computed ima signature verification res=%d\n", res);
1701 : : rc = res;
1702 : :
1703 : : exit_validate:
1704 : : free (sig_buf);
1705 : : EVP_MD_CTX_free(ctx);
1706 : : return rc;
1707 : : }
1708 : : #endif /* ENABLE_IMA_VERIFICATION */
1709 : :
1710 : :
1711 : :
1712 : :
1713 : : /* Helper function to create client cache directory.
1714 : : $XDG_CACHE_HOME takes priority over $HOME/.cache.
1715 : : $DEBUGINFOD_CACHE_PATH takes priority over $HOME/.cache and $XDG_CACHE_HOME.
1716 : :
1717 : : Return resulting path name or NULL on error. Caller must free resulting string.
1718 : : */
1719 : : static char *
1720 : 2134 : make_cache_path(void)
1721 : : {
1722 : 2134 : char* cache_path = NULL;
1723 : 2134 : int rc = 0;
1724 : : /* Determine location of the cache. The path specified by the debuginfod
1725 : : cache environment variable takes priority. */
1726 : 2134 : char *cache_var = getenv(DEBUGINFOD_CACHE_PATH_ENV_VAR);
1727 [ + - + + ]: 2134 : if (cache_var != NULL && strlen (cache_var) > 0)
1728 [ - + ]: 2126 : xalloc_str (cache_path, "%s", cache_var);
1729 : : else
1730 : : {
1731 : : /* If a cache already exists in $HOME ('/' if $HOME isn't set), then use
1732 : : that. Otherwise use the XDG cache directory naming format. */
1733 [ - + - + ]: 16 : xalloc_str (cache_path, "%s/%s", getenv ("HOME") ?: "/", cache_default_name);
1734 : :
1735 : 8 : struct stat st;
1736 [ + + ]: 8 : if (stat (cache_path, &st) < 0)
1737 : : {
1738 : 6 : char cachedir[PATH_MAX];
1739 : 6 : char *xdg = getenv ("XDG_CACHE_HOME");
1740 : :
1741 [ + - + + ]: 6 : if (xdg != NULL && strlen (xdg) > 0)
1742 : 2 : snprintf (cachedir, PATH_MAX, "%s", xdg);
1743 : : else
1744 [ - + ]: 4 : snprintf (cachedir, PATH_MAX, "%s/.cache", getenv ("HOME") ?: "/");
1745 : :
1746 : : /* Create XDG cache directory if it doesn't exist. */
1747 [ + + ]: 6 : if (stat (cachedir, &st) == 0)
1748 : : {
1749 [ - + ]: 2 : if (! S_ISDIR (st.st_mode))
1750 : : {
1751 : 0 : rc = -EEXIST;
1752 : 0 : goto out1;
1753 : : }
1754 : : }
1755 : : else
1756 : : {
1757 : 4 : rc = mkdir (cachedir, 0700);
1758 : :
1759 : : /* Also check for EEXIST and S_ISDIR in case another client just
1760 : : happened to create the cache. */
1761 [ - + ]: 4 : if (rc < 0
1762 [ # # ]: 0 : && (errno != EEXIST
1763 [ # # ]: 0 : || stat (cachedir, &st) != 0
1764 [ # # ]: 0 : || ! S_ISDIR (st.st_mode)))
1765 : : {
1766 : 0 : rc = -errno;
1767 : 0 : goto out1;
1768 : : }
1769 : : }
1770 : :
1771 : 6 : free (cache_path);
1772 [ - + ]: 6 : xalloc_str (cache_path, "%s/%s", cachedir, cache_xdg_name);
1773 : : }
1774 : : }
1775 : :
1776 : 2134 : goto out;
1777 : :
1778 : 0 : out1:
1779 : 0 : (void) rc;
1780 : 0 : free (cache_path);
1781 : 0 : cache_path = NULL;
1782 : :
1783 : 2134 : out:
1784 [ + - ]: 2134 : if (cache_path != NULL)
1785 : 2134 : (void) mkdir (cache_path, 0700); // failures with this mkdir would be caught later too
1786 : 2134 : return cache_path;
1787 : : }
1788 : :
1789 : :
1790 : : /* Query each of the server URLs found in $DEBUGINFOD_URLS for the file
1791 : : with the specified build-id and type (debuginfo, executable, source or
1792 : : section). If type is source, then type_arg should be a filename. If
1793 : : type is section, then type_arg should be the name of an ELF/DWARF
1794 : : section. Otherwise type_arg may be NULL. Return a file descriptor
1795 : : for the target if successful, otherwise return an error code.
1796 : : */
1797 : : static int
1798 : 6518 : debuginfod_query_server_by_buildid (debuginfod_client *c,
1799 : : const unsigned char *build_id,
1800 : : int build_id_len,
1801 : : const char *type,
1802 : : const char *type_arg,
1803 : : char **path)
1804 : : {
1805 : 6518 : char *server_urls;
1806 : 6518 : char *urls_envvar;
1807 : 6518 : const char *section = NULL;
1808 : 6518 : const char *filename = NULL;
1809 : 6518 : char *cache_path = NULL;
1810 : 6518 : char *maxage_path = NULL;
1811 : 6518 : char *interval_path = NULL;
1812 : 6518 : char *cache_miss_path = NULL;
1813 : 6518 : char *target_cache_dir = NULL;
1814 : 6518 : char *target_cache_path = NULL;
1815 : 6518 : char *target_cachehdr_path = NULL;
1816 : 6518 : char *target_cache_tmppath = NULL;
1817 : 6518 : char *target_cachehdr_tmppath = NULL;
1818 : 6518 : char suffix[NAME_MAX];
1819 : 6518 : char build_id_bytes[MAX_BUILD_ID_BYTES * 2 + 1];
1820 : 6518 : int vfd = c->verbose_fd;
1821 : 6518 : int rc, r;
1822 : :
1823 : 6518 : c->progressfn_cancel = false;
1824 : :
1825 [ + + ]: 6518 : if (strcmp (type, "source") == 0)
1826 : : filename = type_arg;
1827 [ + + ]: 1052 : else if (strcmp (type, "section") == 0)
1828 : : {
1829 : 16 : section = type_arg;
1830 [ + - ]: 16 : if (section == NULL)
1831 : : return -EINVAL;
1832 : : }
1833 : :
1834 [ + + ]: 6518 : if (vfd >= 0)
1835 : : {
1836 : 1124 : dprintf (vfd, "debuginfod_find_%s ", type);
1837 [ + + ]: 1124 : if (build_id_len == 0) /* expect clean hexadecimal */
1838 : 32 : dprintf (vfd, "%s", (const char *) build_id);
1839 : : else
1840 [ + + ]: 22932 : for (int i = 0; i < build_id_len; i++)
1841 : 21840 : dprintf (vfd, "%02x", build_id[i]);
1842 [ + + ]: 1124 : if (filename != NULL)
1843 : 1088 : dprintf (vfd, " %s\n", filename);
1844 : 1124 : dprintf (vfd, "\n");
1845 : : }
1846 : :
1847 : : /* Is there any server we can query? If not, don't do any work,
1848 : : just return with ENOSYS. Don't even access the cache. */
1849 : 6518 : urls_envvar = getenv(DEBUGINFOD_URLS_ENV_VAR);
1850 [ + + ]: 6518 : if (vfd >= 0)
1851 [ - + ]: 1124 : dprintf (vfd, "server urls \"%s\"\n",
1852 : : urls_envvar != NULL ? urls_envvar : "");
1853 [ + + + + ]: 6518 : if (urls_envvar == NULL || urls_envvar[0] == '\0')
1854 : : {
1855 : 4418 : rc = -ENOSYS;
1856 : 4418 : goto out;
1857 : : }
1858 : :
1859 : : /* Clear the obsolete data from a previous _find operation. */
1860 : 2100 : free (c->url);
1861 : 2100 : c->url = NULL;
1862 : 2100 : free (c->winning_headers);
1863 : 2100 : c->winning_headers = NULL;
1864 : :
1865 : : /* PR 27982: Add max size if DEBUGINFOD_MAXSIZE is set. */
1866 : 2100 : long maxsize = 0;
1867 : 2100 : const char *maxsize_envvar;
1868 : 2100 : maxsize_envvar = getenv(DEBUGINFOD_MAXSIZE_ENV_VAR);
1869 [ + + ]: 2100 : if (maxsize_envvar != NULL)
1870 : 2 : maxsize = atol (maxsize_envvar);
1871 : :
1872 : : /* PR 27982: Add max time if DEBUGINFOD_MAXTIME is set. */
1873 : 2100 : long maxtime = 0;
1874 : 2100 : const char *maxtime_envvar;
1875 : 2100 : maxtime_envvar = getenv(DEBUGINFOD_MAXTIME_ENV_VAR);
1876 [ + + ]: 2100 : if (maxtime_envvar != NULL)
1877 : 2 : maxtime = atol (maxtime_envvar);
1878 [ + + ]: 2100 : if (maxtime && vfd >= 0)
1879 : 2 : dprintf(vfd, "using max time %lds\n", maxtime);
1880 : :
1881 : 2100 : const char *headers_file_envvar;
1882 : 2100 : headers_file_envvar = getenv(DEBUGINFOD_HEADERS_FILE_ENV_VAR);
1883 [ - + ]: 2100 : if (headers_file_envvar != NULL)
1884 : 0 : add_headers_from_file(c, headers_file_envvar);
1885 : :
1886 : : /* Maxsize is valid*/
1887 [ + + ]: 2100 : if (maxsize > 0)
1888 : : {
1889 [ + - ]: 2 : if (vfd)
1890 : 2 : dprintf (vfd, "using max size %ldB\n", maxsize);
1891 : 2 : char *size_header = NULL;
1892 : 2 : rc = asprintf (&size_header, "X-DEBUGINFOD-MAXSIZE: %ld", maxsize);
1893 [ - + ]: 2 : if (rc < 0)
1894 : : {
1895 : 0 : rc = -ENOMEM;
1896 : 0 : goto out;
1897 : : }
1898 : 2 : rc = debuginfod_add_http_header(c, size_header);
1899 : 2 : free(size_header);
1900 [ - + ]: 2 : if (rc < 0)
1901 : 0 : goto out;
1902 : : }
1903 : 2100 : add_default_headers(c);
1904 : :
1905 : : /* Copy lowercase hex representation of build_id into buf. */
1906 [ + + ]: 2100 : if (vfd >= 0)
1907 : 1124 : dprintf (vfd, "checking build-id\n");
1908 [ + - + + ]: 2100 : if ((build_id_len >= MAX_BUILD_ID_BYTES) ||
1909 : 1006 : (build_id_len == 0 &&
1910 [ - + ]: 1006 : strlen ((const char *) build_id) > MAX_BUILD_ID_BYTES*2))
1911 : : {
1912 : 0 : rc = -EINVAL;
1913 : 0 : goto out;
1914 : : }
1915 : :
1916 [ + + ]: 2100 : if (build_id_len == 0) /* expect clean hexadecimal */
1917 : 1006 : strcpy (build_id_bytes, (const char *) build_id);
1918 : : else
1919 [ + + ]: 22974 : for (int i = 0; i < build_id_len; i++)
1920 : 21880 : sprintf(build_id_bytes + (i * 2), "%02x", build_id[i]);
1921 : :
1922 [ + + ]: 2100 : if (filename != NULL)
1923 : : {
1924 [ + + ]: 1134 : if (vfd >= 0)
1925 : 1088 : dprintf (vfd, "checking filename\n");
1926 [ - + ]: 1134 : if (filename[0] != '/') // must start with /
1927 : : {
1928 : 0 : rc = -EINVAL;
1929 : 0 : goto out;
1930 : : }
1931 : :
1932 : 1134 : path_escape (filename, suffix, sizeof(suffix));
1933 : : /* If the DWARF filenames are super long, this could exceed
1934 : : PATH_MAX and truncate/collide. Oh well, that'll teach
1935 : : them! */
1936 : : }
1937 [ + + ]: 966 : else if (section != NULL)
1938 : 16 : path_escape (section, suffix, sizeof(suffix));
1939 : : else
1940 : 950 : suffix[0] = '\0';
1941 : :
1942 [ + + + + ]: 2100 : if (suffix[0] != '\0' && vfd >= 0)
1943 : 1104 : dprintf (vfd, "suffix %s\n", suffix);
1944 : :
1945 : : /* set paths needed to perform the query
1946 : : example format:
1947 : : cache_path: $HOME/.cache
1948 : : target_cache_dir: $HOME/.cache/0123abcd
1949 : : target_cache_path: $HOME/.cache/0123abcd/debuginfo
1950 : : target_cache_path: $HOME/.cache/0123abcd/executable-.debug_info
1951 : : target_cache_path: $HOME/.cache/0123abcd/source-HASH-#PATH#TO#SOURCE
1952 : : target_cachehdr_path: $HOME/.cache/0123abcd/hdr-debuginfo
1953 : : target_cachehdr_path: $HOME/.cache/0123abcd/hdr-executable...
1954 : : */
1955 : :
1956 : 2100 : cache_path = make_cache_path();
1957 [ - + ]: 2100 : if (!cache_path)
1958 : : {
1959 : 0 : rc = -ENOMEM;
1960 : 0 : goto out;
1961 : : }
1962 [ - + ]: 2100 : xalloc_str (target_cache_dir, "%s/%s", cache_path, build_id_bytes);
1963 : 2100 : (void) mkdir (target_cache_dir, 0700); // failures with this mkdir would be caught later too
1964 : :
1965 [ + + ]: 2100 : if (suffix[0] != '\0') { /* section, source queries */
1966 [ - + ]: 1150 : xalloc_str (target_cache_path, "%s/%s-%s", target_cache_dir, type, suffix);
1967 [ - + ]: 1150 : xalloc_str (target_cachehdr_path, "%s/hdr-%s-%s", target_cache_dir, type, suffix);
1968 : : } else {
1969 [ - + ]: 950 : xalloc_str (target_cache_path, "%s/%s", target_cache_dir, type);
1970 [ - + ]: 950 : xalloc_str (target_cachehdr_path, "%s/hdr-%s", target_cache_dir, type);
1971 : : }
1972 [ - + ]: 2100 : xalloc_str (target_cache_tmppath, "%s.XXXXXX", target_cache_path);
1973 [ - + ]: 2100 : xalloc_str (target_cachehdr_tmppath, "%s.XXXXXX", target_cachehdr_path);
1974 : :
1975 : : /* XXX combine these */
1976 [ - + ]: 2100 : xalloc_str (interval_path, "%s/%s", cache_path, cache_clean_interval_filename);
1977 [ - + ]: 2100 : xalloc_str (cache_miss_path, "%s/%s", cache_path, cache_miss_filename);
1978 [ - + ]: 2100 : xalloc_str (maxage_path, "%s/%s", cache_path, cache_max_unused_age_filename);
1979 : :
1980 [ + + ]: 2100 : if (vfd >= 0)
1981 : 1124 : dprintf (vfd, "checking cache dir %s\n", cache_path);
1982 : :
1983 : : /* Make sure cache dir exists. debuginfo_clean_cache will then make
1984 : : sure the interval, cache_miss and maxage files exist. */
1985 [ + - ]: 2100 : if (mkdir (cache_path, ACCESSPERMS) != 0
1986 [ - + ]: 2100 : && errno != EEXIST)
1987 : : {
1988 : 0 : rc = -errno;
1989 : 0 : goto out;
1990 : : }
1991 : :
1992 : 2100 : rc = debuginfod_clean_cache(c, cache_path, interval_path, maxage_path);
1993 [ - + ]: 2100 : if (rc != 0)
1994 : 0 : goto out;
1995 : :
1996 : : /* Check if the target is already in the cache. */
1997 : 2100 : int fd = open(target_cache_path, O_RDONLY);
1998 [ + + ]: 2100 : if (fd >= 0)
1999 : : {
2000 : 58 : struct stat st;
2001 [ - + ]: 58 : if (fstat(fd, &st) != 0)
2002 : : {
2003 : 0 : rc = -errno;
2004 : 0 : close (fd);
2005 : 56 : goto out;
2006 : : }
2007 : :
2008 : : /* If the file is non-empty, then we are done. */
2009 [ + + ]: 58 : if (st.st_size > 0)
2010 : : {
2011 [ + - ]: 54 : if (path != NULL)
2012 : : {
2013 : 54 : *path = strdup(target_cache_path);
2014 [ - + ]: 54 : if (*path == NULL)
2015 : : {
2016 : 0 : rc = -errno;
2017 : 0 : close (fd);
2018 : 0 : goto out;
2019 : : }
2020 : : }
2021 : : /* Success!!!! */
2022 : 54 : update_atime(fd);
2023 : 54 : rc = fd;
2024 : :
2025 : : /* Attempt to transcribe saved headers. */
2026 : 54 : int fdh = open (target_cachehdr_path, O_RDONLY);
2027 [ + + ]: 54 : if (fdh >= 0)
2028 : : {
2029 [ + - + - ]: 52 : if (fstat (fdh, &st) == 0 && st.st_size > 0)
2030 : : {
2031 : 52 : c->winning_headers = malloc(st.st_size);
2032 [ + - ]: 52 : if (NULL != c->winning_headers)
2033 : : {
2034 : 52 : ssize_t bytes_read = pread_retry(fdh, c->winning_headers, st.st_size, 0);
2035 [ - + ]: 52 : if (bytes_read <= 0)
2036 : : {
2037 : 0 : free (c->winning_headers);
2038 : 0 : c->winning_headers = NULL;
2039 : 0 : (void) unlink (target_cachehdr_path);
2040 : : }
2041 [ + + ]: 52 : if (vfd >= 0)
2042 : 2 : dprintf (vfd, "found %s (bytes=%ld)\n", target_cachehdr_path, (long)bytes_read);
2043 : : }
2044 : : }
2045 : :
2046 : 52 : update_atime (fdh);
2047 : 52 : close (fdh);
2048 : : }
2049 : :
2050 : 54 : goto out;
2051 : : }
2052 : : else
2053 : : {
2054 : : /* The file is empty. Attempt to download only if enough time
2055 : : has passed since the last attempt. */
2056 : 4 : time_t cache_miss;
2057 : 4 : time_t target_mtime = st.st_mtime;
2058 : :
2059 : 4 : close(fd); /* no need to hold onto the negative-hit file descriptor */
2060 : :
2061 : 4 : rc = debuginfod_config_cache(c, cache_miss_path,
2062 : : cache_miss_default_s, &st);
2063 [ - + ]: 4 : if (rc < 0)
2064 : 0 : goto out;
2065 : :
2066 : 4 : cache_miss = (time_t)rc;
2067 [ + + ]: 4 : if (time(NULL) - target_mtime <= cache_miss)
2068 : : {
2069 : 2 : rc = -ENOENT;
2070 : 2 : goto out;
2071 : : }
2072 : : else
2073 : : /* TOCTOU non-problem: if another task races, puts a working
2074 : : download or an empty file in its place, unlinking here just
2075 : : means WE will try to download again as uncached. */
2076 : 2 : (void) unlink(target_cache_path);
2077 : : }
2078 : : }
2079 [ - + ]: 2042 : else if (errno == EACCES)
2080 : : /* Ensure old 000-permission files are not lingering in the cache. */
2081 : 0 : (void) unlink(target_cache_path);
2082 : :
2083 [ + + ]: 2044 : if (section != NULL)
2084 : : {
2085 : : /* Try to extract the section from a cached file before querying
2086 : : any servers. */
2087 : 16 : rc = cache_find_section (section, target_cache_dir, path);
2088 : :
2089 : : /* If the section was found or confirmed to not exist, then we
2090 : : are done. */
2091 [ + + ]: 16 : if (rc >= 0 || rc == -ENOENT)
2092 : 8 : goto out;
2093 : : }
2094 : :
2095 : 2036 : long timeout = default_timeout;
2096 : 2036 : const char* timeout_envvar = getenv(DEBUGINFOD_TIMEOUT_ENV_VAR);
2097 [ - + ]: 2036 : if (timeout_envvar != NULL)
2098 : 0 : timeout = atoi (timeout_envvar);
2099 : :
2100 [ + + ]: 2036 : if (vfd >= 0)
2101 : 1114 : dprintf (vfd, "using timeout %ld\n", timeout);
2102 : :
2103 : : /* make a copy of the envvar so it can be safely modified. */
2104 : 2036 : server_urls = strdup(urls_envvar);
2105 [ - + ]: 2036 : if (server_urls == NULL)
2106 : : {
2107 : 0 : rc = -ENOMEM;
2108 : 0 : goto out;
2109 : : }
2110 : : /* thereafter, goto out0 on error*/
2111 : :
2112 : : /* Because of a race with cache cleanup / rmdir, try to mkdir/mkstemp up to twice. */
2113 [ + - ]: 2036 : for(int i=0; i<2; i++)
2114 : : {
2115 : : /* (re)create target directory in cache */
2116 : 2036 : (void) mkdir(target_cache_dir, 0700); /* files will be 0400 later */
2117 : :
2118 : : /* NB: write to a temporary file first, to avoid race condition of
2119 : : multiple clients checking the cache, while a partially-written or empty
2120 : : file is in there, being written from libcurl. */
2121 : 2036 : fd = mkstemp (target_cache_tmppath);
2122 [ - + ]: 2036 : if (fd >= 0) break;
2123 : : }
2124 [ - + ]: 2036 : if (fd < 0) /* Still failed after two iterations. */
2125 : : {
2126 : 0 : rc = -errno;
2127 : 0 : goto out0;
2128 : : }
2129 : :
2130 : 2036 : char **server_url_list = NULL;
2131 : 2036 : ima_policy_t* url_ima_policies = NULL;
2132 : 2036 : char *server_url;
2133 : 2036 : int num_urls;
2134 : 2036 : r = init_server_urls("buildid", type, server_urls, &server_url_list, &url_ima_policies, &num_urls, vfd);
2135 [ - + ]: 2036 : if (0 != r)
2136 : : {
2137 : 0 : rc = r;
2138 : 0 : goto out1;
2139 : : }
2140 : :
2141 : : /* No URLs survived parsing / filtering? Abort abort abort. */
2142 [ - + ]: 2036 : if (num_urls == 0)
2143 : : {
2144 : 0 : rc = -ENOSYS;
2145 : 0 : goto out1;
2146 : : }
2147 : :
2148 : 2036 : int retry_limit = default_retry_limit;
2149 : 2036 : const char* retry_limit_envvar = getenv(DEBUGINFOD_RETRY_LIMIT_ENV_VAR);
2150 [ + + ]: 2036 : if (retry_limit_envvar != NULL)
2151 : 4 : retry_limit = atoi (retry_limit_envvar);
2152 : :
2153 : 2036 : CURLM *curlm = c->server_mhandle;
2154 : :
2155 : : /* Tracks which handle should write to fd. Set to the first
2156 : : handle that is ready to write the target file to the cache. */
2157 : 2036 : CURL *target_handle = NULL;
2158 : 2036 : struct handle_data *data = malloc(sizeof(struct handle_data) * num_urls);
2159 [ - + ]: 2036 : if (data == NULL)
2160 : : {
2161 : 0 : rc = -ENOMEM;
2162 : 0 : goto out1;
2163 : : }
2164 : :
2165 : : /* thereafter, goto out2 on error. */
2166 : :
2167 : : /*The beginning of goto block query_in_parallel.*/
2168 : 2036 : query_in_parallel:
2169 : 3124 : rc = -ENOENT; /* Reset rc to default.*/
2170 : :
2171 : : /* Initialize handle_data with default values. */
2172 [ + + ]: 6302 : for (int i = 0; i < num_urls; i++)
2173 : : {
2174 : 3178 : data[i].handle = NULL;
2175 : 3178 : data[i].fd = -1;
2176 : 3178 : data[i].errbuf[0] = '\0';
2177 : 3178 : data[i].response_data = NULL;
2178 : 3178 : data[i].response_data_size = 0;
2179 : : }
2180 : :
2181 : 3124 : char *escaped_string = NULL;
2182 : 3124 : size_t escaped_strlen = 0;
2183 [ + + ]: 3124 : if (filename)
2184 : : {
2185 : 1134 : escaped_string = curl_easy_escape(&target_handle, filename+1, 0);
2186 [ - + ]: 1134 : if (!escaped_string)
2187 : : {
2188 : 0 : rc = -ENOMEM;
2189 : 0 : goto out2;
2190 : : }
2191 : 1134 : char *loc = escaped_string;
2192 : 1134 : escaped_strlen = strlen(escaped_string);
2193 [ + + ]: 8828 : while ((loc = strstr(loc, "%2F")))
2194 : : {
2195 : 7694 : loc[0] = '/';
2196 : : //pull the string back after replacement
2197 : : // loc-escaped_string finds the distance from the origin to the new location
2198 : : // - 2 accounts for the 2F which remain and don't need to be measured.
2199 : : // The two above subtracted from escaped_strlen yields the remaining characters
2200 : : // in the string which we want to pull back
2201 : 7694 : memmove(loc+1, loc+3,escaped_strlen - (loc-escaped_string) - 2);
2202 : : //Because the 2F was overwritten in the memmove (as desired) escaped_strlen is
2203 : : // now two shorter.
2204 : 7694 : escaped_strlen -= 2;
2205 : : }
2206 : : }
2207 : : /* Initialize each handle. */
2208 [ + + ]: 6302 : for (int i = 0; i < num_urls; i++)
2209 : : {
2210 [ + - ]: 3178 : if ((server_url = server_url_list[i]) == NULL)
2211 : : break;
2212 [ + + ]: 3178 : if (vfd >= 0)
2213 : : #ifdef ENABLE_IMA_VERIFICATION
2214 : : dprintf (vfd, "init server %d %s [IMA verification policy: %s]\n", i, server_url, ima_policy_enum2str(url_ima_policies[i]));
2215 : : #else
2216 : 1144 : dprintf (vfd, "init server %d %s\n", i, server_url);
2217 : : #endif
2218 : :
2219 : 3178 : data[i].fd = fd;
2220 : 3178 : data[i].target_handle = &target_handle;
2221 : 3178 : data[i].client = c;
2222 : :
2223 [ + + ]: 3178 : if (filename) /* must start with / */
2224 : : {
2225 : : /* PR28034 escape characters in completed url to %hh format. */
2226 : 1134 : snprintf(data[i].url, PATH_MAX, "%s/%s/%s/%s", server_url,
2227 : : build_id_bytes, type, escaped_string);
2228 : : }
2229 [ + + ]: 2044 : else if (section)
2230 : 8 : snprintf(data[i].url, PATH_MAX, "%s/%s/%s/%s", server_url,
2231 : : build_id_bytes, type, section);
2232 : : else
2233 : 2036 : snprintf(data[i].url, PATH_MAX, "%s/%s/%s", server_url, build_id_bytes, type);
2234 : :
2235 : 3178 : r = init_handle(c, debuginfod_write_callback, header_callback, &data[i], i, timeout, vfd);
2236 [ - + ]: 3178 : if (0 != r)
2237 : : {
2238 : 0 : rc = r;
2239 [ # # ]: 0 : if (filename) curl_free (escaped_string);
2240 : 0 : goto out2;
2241 : : }
2242 : :
2243 : 3178 : curl_multi_add_handle(curlm, data[i].handle);
2244 : : }
2245 : :
2246 [ + + ]: 3124 : if (filename) curl_free(escaped_string);
2247 : :
2248 : : /* Query servers in parallel. */
2249 [ + + ]: 3124 : if (vfd >= 0)
2250 : 1138 : dprintf (vfd, "query %d urls in parallel\n", num_urls);
2251 : 3124 : int committed_to;
2252 : 3124 : r = perform_queries(curlm, &target_handle, data, c, num_urls, maxtime, maxsize, true, vfd, &committed_to);
2253 [ - + ]: 3124 : if (0 != r)
2254 : : {
2255 : 0 : rc = r;
2256 : 0 : goto out2;
2257 : : }
2258 : :
2259 : : /* Check whether a query was successful. If so, assign its handle
2260 : : to verified_handle. */
2261 : : int num_msg;
2262 : : rc = -ENOENT;
2263 : 3130 : CURL *verified_handle = NULL;
2264 : 3130 : do
2265 : : {
2266 : 3130 : CURLMsg *msg;
2267 : :
2268 : 3130 : msg = curl_multi_info_read(curlm, &num_msg);
2269 [ + - + - ]: 3130 : if (msg != NULL && msg->msg == CURLMSG_DONE)
2270 : : {
2271 [ + + ]: 3130 : if (vfd >= 0)
2272 : : {
2273 : 2288 : bool pnl = (c->default_progressfn_printed_p
2274 [ - + - - ]: 1144 : && vfd == STDERR_FILENO);
2275 [ + - ]: 1144 : dprintf (vfd, "%sserver response %s\n", pnl ? "\n" : "",
2276 : : curl_easy_strerror (msg->data.result));
2277 [ - + ]: 1144 : if (pnl)
2278 : 0 : c->default_progressfn_printed_p = 0;
2279 [ + - ]: 1150 : for (int i = 0; i < num_urls; i++)
2280 [ + + ]: 1150 : if (msg->easy_handle == data[i].handle)
2281 : : {
2282 [ + + ]: 1144 : if (strlen (data[i].errbuf) > 0)
2283 : 40 : dprintf (vfd, "url %d %s\n", i, data[i].errbuf);
2284 : : break;
2285 : : }
2286 : : }
2287 : :
2288 [ + + ]: 3130 : if (msg->data.result != CURLE_OK)
2289 : : {
2290 : 1670 : long resp_code;
2291 : 1670 : CURLcode ok0;
2292 : : /* Unsuccessful query, determine error code. */
2293 [ - + - - : 1670 : switch (msg->data.result)
- - - - +
+ - ]
2294 : : {
2295 : : case CURLE_COULDNT_RESOLVE_HOST: rc = -EHOSTUNREACH; break; // no NXDOMAIN
2296 : 0 : case CURLE_URL_MALFORMAT: rc = -EINVAL; break;
2297 : 1624 : case CURLE_COULDNT_CONNECT: rc = -ECONNREFUSED; break;
2298 : 1624 : case CURLE_PEER_FAILED_VERIFICATION: rc = -ECONNREFUSED; break;
2299 : 0 : case CURLE_REMOTE_ACCESS_DENIED: rc = -EACCES; break;
2300 : 0 : case CURLE_WRITE_ERROR: rc = -EIO; break;
2301 : 0 : case CURLE_OUT_OF_MEMORY: rc = -ENOMEM; break;
2302 : 0 : case CURLE_TOO_MANY_REDIRECTS: rc = -EMLINK; break;
2303 : 0 : case CURLE_SEND_ERROR: rc = -ECONNRESET; break;
2304 : 0 : case CURLE_RECV_ERROR: rc = -ECONNRESET; break;
2305 : 0 : case CURLE_OPERATION_TIMEDOUT: rc = -ETIME; break;
2306 : : case CURLE_HTTP_RETURNED_ERROR:
2307 : 44 : ok0 = curl_easy_getinfo (msg->easy_handle,
2308 : : CURLINFO_RESPONSE_CODE,
2309 : : &resp_code);
2310 : : /* 406 signals that the requested file was too large */
2311 [ + - + + ]: 44 : if ( ok0 == CURLE_OK && resp_code == 406)
2312 : : rc = -EFBIG;
2313 [ - + - - ]: 42 : else if (section != NULL && resp_code == 503)
2314 : : rc = -EINVAL;
2315 : : else
2316 : 44 : rc = -ENOENT;
2317 : : break;
2318 : 44 : default: rc = -ENOENT; break;
2319 : : }
2320 : : }
2321 : : else
2322 : : {
2323 : : /* Query completed without an error. Confirm that the
2324 : : response code is 200 when using HTTP/HTTPS and 0 when
2325 : : using file:// and set verified_handle. */
2326 : :
2327 [ + - ]: 1460 : if (msg->easy_handle != NULL)
2328 : : {
2329 : 1460 : char *effective_url = NULL;
2330 : 1460 : long resp_code = 500;
2331 : 1460 : CURLcode ok1 = curl_easy_getinfo (target_handle,
2332 : : CURLINFO_EFFECTIVE_URL,
2333 : : &effective_url);
2334 : 1460 : CURLcode ok2 = curl_easy_getinfo (target_handle,
2335 : : CURLINFO_RESPONSE_CODE,
2336 : : &resp_code);
2337 [ + - + - ]: 1460 : if(ok1 == CURLE_OK && ok2 == CURLE_OK && effective_url)
2338 : : {
2339 [ + + ]: 1460 : if (strncasecmp (effective_url, "HTTP", 4) == 0)
2340 [ + - ]: 1458 : if (resp_code == 200)
2341 : : {
2342 : 1458 : verified_handle = msg->easy_handle;
2343 : 1460 : break;
2344 : : }
2345 [ + - ]: 2 : if (strncasecmp (effective_url, "FILE", 4) == 0)
2346 [ + - ]: 2 : if (resp_code == 0)
2347 : : {
2348 : 2 : verified_handle = msg->easy_handle;
2349 : 2 : break;
2350 : : }
2351 : : }
2352 : : /* - libcurl since 7.52.0 version start to support
2353 : : CURLINFO_SCHEME;
2354 : : - before 7.61.0, effective_url would give us a
2355 : : url with upper case SCHEME added in the front;
2356 : : - effective_url between 7.61 and 7.69 can be lack
2357 : : of scheme if the original url doesn't include one;
2358 : : - since version 7.69 effective_url will be provide
2359 : : a scheme in lower case. */
2360 : : #if LIBCURL_VERSION_NUM >= 0x073d00 /* 7.61.0 */
2361 : : #if LIBCURL_VERSION_NUM <= 0x074500 /* 7.69.0 */
2362 : : char *scheme = NULL;
2363 : : CURLcode ok3 = curl_easy_getinfo (target_handle,
2364 : : CURLINFO_SCHEME,
2365 : : &scheme);
2366 : : if(ok3 == CURLE_OK && scheme)
2367 : : {
2368 : : if (startswith (scheme, "HTTP"))
2369 : : if (resp_code == 200)
2370 : : {
2371 : : verified_handle = msg->easy_handle;
2372 : : break;
2373 : : }
2374 : : }
2375 : : #endif
2376 : : #endif
2377 : : }
2378 : : }
2379 : : }
2380 [ + + ]: 1670 : } while (num_msg > 0);
2381 : :
2382 : : /* Create an empty file in the cache if the query fails with ENOENT and
2383 : : it wasn't cancelled early. */
2384 [ + + + - ]: 3124 : if (rc == -ENOENT && !c->progressfn_cancel)
2385 : : {
2386 : 1500 : int efd = open (target_cache_path, O_CREAT|O_EXCL, DEFFILEMODE);
2387 [ + + ]: 1500 : if (efd >= 0)
2388 : 1498 : close(efd);
2389 : : }
2390 [ + + ]: 1624 : else if (rc == -EFBIG)
2391 : 2 : goto out2;
2392 : :
2393 : : /* If the verified_handle is NULL and rc != -ENOENT, the query fails with
2394 : : * an error code other than 404, then do several retry within the retry_limit.
2395 : : * Clean up all old handles and jump back to the beginning of query_in_parallel,
2396 : : * reinitialize handles and query again.*/
2397 [ + + ]: 3122 : if (verified_handle == NULL)
2398 : : {
2399 [ + + + + ]: 1662 : if (rc != -ENOENT && retry_limit-- > 0)
2400 : : {
2401 [ + + ]: 1088 : if (vfd >= 0)
2402 : 24 : dprintf (vfd, "Retry failed query, %d attempt(s) remaining\n", retry_limit);
2403 : : /* remove all handles from multi */
2404 [ + + ]: 2180 : for (int i = 0; i < num_urls; i++)
2405 : : {
2406 : 1092 : curl_multi_remove_handle(curlm, data[i].handle); /* ok to repeat */
2407 : 1092 : curl_easy_cleanup (data[i].handle);
2408 : 1092 : free(data[i].response_data);
2409 : 1092 : data[i].response_data = NULL;
2410 : : }
2411 : 1088 : free(c->winning_headers);
2412 : 1088 : c->winning_headers = NULL;
2413 : 1088 : goto query_in_parallel;
2414 : : }
2415 : : else
2416 : 574 : goto out2;
2417 : : }
2418 : :
2419 [ + + ]: 1460 : if (vfd >= 0)
2420 : : {
2421 [ - + - - ]: 2208 : bool pnl = c->default_progressfn_printed_p && vfd == STDERR_FILENO;
2422 : 1104 : dprintf (vfd, "%sgot file from server\n", pnl ? "\n" : "");
2423 [ - + ]: 1104 : if (pnl)
2424 : 0 : c->default_progressfn_printed_p = 0;
2425 : : }
2426 : :
2427 : : /* we've got one!!!! */
2428 : 1460 : time_t mtime;
2429 : : #if defined(_TIME_BITS) && _TIME_BITS == 64
2430 : : CURLcode curl_res = curl_easy_getinfo(verified_handle, CURLINFO_FILETIME_T, (void*) &mtime);
2431 : : #else
2432 : 1460 : CURLcode curl_res = curl_easy_getinfo(verified_handle, CURLINFO_FILETIME, (void*) &mtime);
2433 : : #endif
2434 [ + - ]: 1460 : if (curl_res == CURLE_OK)
2435 : : {
2436 : 1460 : struct timespec tvs[2];
2437 : 1460 : tvs[0].tv_sec = 0;
2438 : 1460 : tvs[0].tv_nsec = UTIME_OMIT;
2439 : 1460 : tvs[1].tv_sec = mtime;
2440 : 1460 : tvs[1].tv_nsec = 0;
2441 : 1460 : (void) futimens (fd, tvs); /* best effort */
2442 : : }
2443 : :
2444 : : /* PR27571: make cache files casually unwriteable; dirs are already 0700 */
2445 : 1460 : (void) fchmod(fd, 0400);
2446 : : /* PR31248: lseek back to beginning */
2447 : 1460 : (void) lseek(fd, 0, SEEK_SET);
2448 : :
2449 [ + - - + ]: 1460 : if(NULL != url_ima_policies && ignore != url_ima_policies[committed_to])
2450 : : {
2451 : : #ifdef ENABLE_IMA_VERIFICATION
2452 : : int result = debuginfod_validate_imasig(c, fd);
2453 : : #else
2454 : 0 : int result = -ENOSYS;
2455 : : #endif
2456 : 0 : if(0 == result)
2457 : : {
2458 : : if (vfd >= 0) dprintf (vfd, "valid signature\n");
2459 : : }
2460 [ # # ]: 0 : else if (enforcing == url_ima_policies[committed_to])
2461 : : {
2462 : : // All invalid signatures are rejected.
2463 : : // Additionally in enforcing mode any non-valid signature is rejected, so by reaching
2464 : : // this case we do so since we know it is not valid. Note - this not just invalid signatures
2465 : : // but also signatures that cannot be validated
2466 [ # # ]: 0 : if (vfd >= 0) dprintf (vfd, "error: invalid or missing signature (%d)\n", result);
2467 : 0 : rc = result;
2468 : 0 : goto out2;
2469 : : }
2470 : : }
2471 : :
2472 : : /* rename tmp->real */
2473 : 1460 : rc = rename (target_cache_tmppath, target_cache_path);
2474 [ - + ]: 1460 : if (rc < 0)
2475 : : {
2476 : 0 : rc = -errno;
2477 : 0 : goto out2;
2478 : : /* Perhaps we need not give up right away; could retry or something ... */
2479 : : }
2480 : :
2481 : : /* write out the headers, best effort basis */
2482 [ + + ]: 1460 : if (c->winning_headers) {
2483 : 1458 : int fdh = mkstemp (target_cachehdr_tmppath);
2484 [ + - ]: 1458 : if (fdh >= 0) {
2485 : 1458 : size_t bytes_to_write = strlen(c->winning_headers)+1; // include \0
2486 : 1458 : size_t bytes = pwrite_retry (fdh, c->winning_headers, bytes_to_write, 0);
2487 : 1458 : (void) close (fdh);
2488 [ + - ]: 1458 : if (bytes == bytes_to_write)
2489 : 1458 : (void) rename (target_cachehdr_tmppath, target_cachehdr_path);
2490 : : else
2491 : 0 : (void) unlink (target_cachehdr_tmppath);
2492 [ + + ]: 1458 : if (vfd >= 0)
2493 : 1104 : dprintf (vfd, "saved %ld bytes of headers to %s\n", (long)bytes, target_cachehdr_path);
2494 : : }
2495 : : }
2496 : :
2497 : : /* remove all handles from multi */
2498 [ + + ]: 2968 : for (int i = 0; i < num_urls; i++)
2499 : : {
2500 : 1508 : curl_multi_remove_handle(curlm, data[i].handle); /* ok to repeat */
2501 : 1508 : curl_easy_cleanup (data[i].handle);
2502 : 1508 : free (data[i].response_data);
2503 : : }
2504 : :
2505 [ + + ]: 2968 : for (int i = 0; i < num_urls; ++i)
2506 : 1508 : free(server_url_list[i]);
2507 : 1460 : free(server_url_list);
2508 : 1460 : free(url_ima_policies);
2509 : 1460 : free (data);
2510 : 1460 : free (server_urls);
2511 : :
2512 : : /* don't close fd - we're returning it */
2513 : : /* don't unlink the tmppath; it's already been renamed. */
2514 [ + + ]: 1460 : if (path != NULL)
2515 : 374 : *path = strdup(target_cache_path);
2516 : :
2517 : 1460 : rc = fd;
2518 : 1460 : goto out;
2519 : :
2520 : : /* error exits */
2521 : 576 : out2:
2522 : : /* remove all handles from multi */
2523 [ + + ]: 1154 : for (int i = 0; i < num_urls; i++)
2524 : : {
2525 [ + - ]: 578 : if (data[i].handle != NULL)
2526 : : {
2527 : 578 : curl_multi_remove_handle(curlm, data[i].handle); /* ok to repeat */
2528 : 578 : curl_easy_cleanup (data[i].handle);
2529 : 578 : free (data[i].response_data);
2530 : : }
2531 : : }
2532 : :
2533 : 576 : unlink (target_cache_tmppath);
2534 : 576 : close (fd); /* before the rmdir, otherwise it'll fail */
2535 : 576 : (void) rmdir (target_cache_dir); /* nop if not empty */
2536 : 576 : free(data);
2537 : :
2538 : 576 : out1:
2539 [ + + ]: 1154 : for (int i = 0; i < num_urls; ++i)
2540 : 578 : free(server_url_list[i]);
2541 : 576 : free(server_url_list);
2542 : 576 : free(url_ima_policies);
2543 : :
2544 : 576 : out0:
2545 : 576 : free (server_urls);
2546 : :
2547 : : /* general purpose exit */
2548 : 6518 : out:
2549 : : /* Reset sent headers */
2550 : 6518 : curl_slist_free_all (c->headers);
2551 : 6518 : c->headers = NULL;
2552 : 6518 : c->user_agent_set_p = 0;
2553 : :
2554 : : /* Conclude the last \r status line */
2555 : : /* Another possibility is to use the ANSI CSI n K EL "Erase in Line"
2556 : : code. That way, the previously printed messages would be erased,
2557 : : and without a newline. */
2558 [ + + ]: 6518 : if (c->default_progressfn_printed_p)
2559 : 2 : dprintf(STDERR_FILENO, "\n");
2560 : :
2561 [ + + ]: 6518 : if (vfd >= 0)
2562 : : {
2563 [ + + ]: 1124 : if (rc < 0)
2564 : 10 : dprintf (vfd, "not found %s (err=%d)\n", strerror (-rc), rc);
2565 : : else
2566 : 1114 : dprintf (vfd, "found %s (fd=%d)\n", target_cache_path, rc);
2567 : : }
2568 : :
2569 : 6518 : free (cache_path);
2570 : 6518 : free (maxage_path);
2571 : 6518 : free (interval_path);
2572 : 6518 : free (cache_miss_path);
2573 : 6518 : free (target_cache_dir);
2574 : 6518 : free (target_cache_path);
2575 [ + + + + ]: 6518 : if (rc < 0 && target_cache_tmppath != NULL)
2576 : 578 : (void)unlink (target_cache_tmppath);
2577 : 6518 : free (target_cache_tmppath);
2578 [ + + + + ]: 6518 : if (rc < 0 && target_cachehdr_tmppath != NULL)
2579 : 578 : (void)unlink (target_cachehdr_tmppath);
2580 : 6518 : free (target_cachehdr_tmppath);
2581 : 6518 : free (target_cachehdr_path);
2582 : :
2583 : 6518 : return rc;
2584 : : }
2585 : :
2586 : :
2587 : :
2588 : : /* See debuginfod.h */
2589 : : debuginfod_client *
2590 : 528 : debuginfod_begin (void)
2591 : : {
2592 : : /* Initialize libcurl lazily, but only once. */
2593 : 528 : pthread_once (&init_control, libcurl_init);
2594 : :
2595 : 528 : debuginfod_client *client;
2596 : 528 : size_t size = sizeof (struct debuginfod_client);
2597 : 528 : client = calloc (1, size);
2598 : :
2599 [ + - ]: 528 : if (client != NULL)
2600 : : {
2601 [ + + ]: 528 : if (getenv(DEBUGINFOD_PROGRESS_ENV_VAR))
2602 : 4 : client->progressfn = debuginfod_default_progressfn;
2603 [ + + ]: 528 : if (getenv(DEBUGINFOD_VERBOSE_ENV_VAR))
2604 : 6 : client->verbose_fd = STDERR_FILENO;
2605 : : else
2606 : 522 : client->verbose_fd = -1;
2607 : :
2608 : : // allocate 1 curl multi handle
2609 : 528 : client->server_mhandle = curl_multi_init ();
2610 [ - + ]: 528 : if (client->server_mhandle == NULL)
2611 : 0 : goto out1;
2612 : : }
2613 : :
2614 : : #ifdef ENABLE_IMA_VERIFICATION
2615 : : load_ima_public_keys (client);
2616 : : #endif
2617 : :
2618 : : // extra future initialization
2619 : :
2620 : 528 : goto out;
2621 : :
2622 : 0 : out1:
2623 : 0 : free (client);
2624 : 0 : client = NULL;
2625 : :
2626 : 528 : out:
2627 : 528 : return client;
2628 : : }
2629 : :
2630 : : void
2631 : 486 : debuginfod_set_user_data(debuginfod_client *client,
2632 : : void *data)
2633 : : {
2634 : 486 : client->user_data = data;
2635 : 486 : }
2636 : :
2637 : : void *
2638 : 0 : debuginfod_get_user_data(debuginfod_client *client)
2639 : : {
2640 : 0 : return client->user_data;
2641 : : }
2642 : :
2643 : : const char *
2644 : 46 : debuginfod_get_url(debuginfod_client *client)
2645 : : {
2646 [ + - ]: 2 : return client->url;
2647 : : }
2648 : :
2649 : : const char *
2650 : 48 : debuginfod_get_headers(debuginfod_client *client)
2651 : : {
2652 : 48 : return client->winning_headers;
2653 : : }
2654 : :
2655 : : void
2656 : 526 : debuginfod_end (debuginfod_client *client)
2657 : : {
2658 [ + - ]: 526 : if (client == NULL)
2659 : : return;
2660 : :
2661 : 526 : curl_multi_cleanup (client->server_mhandle);
2662 : 526 : curl_slist_free_all (client->headers);
2663 : 526 : free (client->winning_headers);
2664 : 526 : free (client->url);
2665 : : #ifdef ENABLE_IMA_VERIFICATION
2666 : : free_ima_public_keys (client);
2667 : : #endif
2668 : 526 : free (client);
2669 : : }
2670 : :
2671 : : int
2672 : 300 : debuginfod_find_debuginfo (debuginfod_client *client,
2673 : : const unsigned char *build_id, int build_id_len,
2674 : : char **path)
2675 : : {
2676 : 300 : return debuginfod_query_server_by_buildid(client, build_id, build_id_len,
2677 : : "debuginfo", NULL, path);
2678 : : }
2679 : :
2680 : :
2681 : : /* See debuginfod.h */
2682 : : int
2683 : 736 : debuginfod_find_executable(debuginfod_client *client,
2684 : : const unsigned char *build_id, int build_id_len,
2685 : : char **path)
2686 : : {
2687 : 736 : return debuginfod_query_server_by_buildid(client, build_id, build_id_len,
2688 : : "executable", NULL, path);
2689 : : }
2690 : :
2691 : : /* See debuginfod.h */
2692 : 5466 : int debuginfod_find_source(debuginfod_client *client,
2693 : : const unsigned char *build_id, int build_id_len,
2694 : : const char *filename, char **path)
2695 : : {
2696 : 5466 : return debuginfod_query_server_by_buildid(client, build_id, build_id_len,
2697 : : "source", filename, path);
2698 : : }
2699 : :
2700 : : int
2701 : 16 : debuginfod_find_section (debuginfod_client *client,
2702 : : const unsigned char *build_id, int build_id_len,
2703 : : const char *section, char **path)
2704 : : {
2705 : 16 : int rc = debuginfod_query_server_by_buildid(client, build_id, build_id_len,
2706 : : "section", section, path);
2707 [ - + ]: 16 : if (rc != -EINVAL && rc != -ENOSYS)
2708 : : return rc;
2709 : : /* NB: we fall through in case of ima:enforcing-filtered DEBUGINFOD_URLS servers,
2710 : : so we can download the entire file, verify it locally, then slice it. */
2711 : :
2712 : : /* The servers may have lacked support for section queries. Attempt to
2713 : : download the debuginfo or executable containing the section in order
2714 : : to extract it. */
2715 : 0 : rc = -EEXIST;
2716 : 0 : int fd = -1;
2717 : 0 : char *tmp_path = NULL;
2718 : :
2719 : 0 : fd = debuginfod_find_debuginfo (client, build_id, build_id_len, &tmp_path);
2720 [ # # ]: 0 : if (client->progressfn_cancel)
2721 : : {
2722 [ # # ]: 0 : if (fd >= 0)
2723 : : {
2724 : : /* This shouldn't happen, but we'll check this condition
2725 : : just in case. */
2726 : 0 : close (fd);
2727 : 0 : free (tmp_path);
2728 : : }
2729 : 0 : return -ENOENT;
2730 : : }
2731 [ # # ]: 0 : if (fd >= 0)
2732 : : {
2733 : 0 : rc = extract_section (fd, section, tmp_path, path);
2734 : 0 : close (fd);
2735 : : }
2736 : :
2737 [ # # ]: 0 : if (rc == -EEXIST)
2738 : : {
2739 : : /* Either the debuginfo couldn't be found or the section should
2740 : : be in the executable. */
2741 : 0 : fd = debuginfod_find_executable (client, build_id,
2742 : : build_id_len, &tmp_path);
2743 [ # # ]: 0 : if (fd >= 0)
2744 : : {
2745 : 0 : rc = extract_section (fd, section, tmp_path, path);
2746 : 0 : close (fd);
2747 : : }
2748 : : else
2749 : : /* Update rc so that we return the most recent error code. */
2750 : : rc = fd;
2751 : : }
2752 : :
2753 : 0 : free (tmp_path);
2754 : 0 : return rc;
2755 : : }
2756 : :
2757 : :
2758 : 52 : int debuginfod_find_metadata (debuginfod_client *client,
2759 : : const char* key, const char* value, char **path)
2760 : : {
2761 : 52 : char *server_urls = NULL;
2762 : 52 : char *urls_envvar = NULL;
2763 : 52 : char *cache_path = NULL;
2764 : 52 : char *target_cache_dir = NULL;
2765 : 52 : char *target_cache_path = NULL;
2766 : 52 : char *target_cache_tmppath = NULL;
2767 : 52 : char *target_file_name = NULL;
2768 : 52 : char *key_and_value = NULL;
2769 : 52 : int rc = 0, r;
2770 : 52 : int vfd = client->verbose_fd;
2771 : 52 : struct handle_data *data = NULL;
2772 : :
2773 : 52 : client->progressfn_cancel = false;
2774 : :
2775 : 52 : json_object *json_metadata = json_object_new_object();
2776 : 52 : json_bool json_metadata_complete = true;
2777 : 52 : json_object *json_metadata_arr = json_object_new_array();
2778 [ - + ]: 52 : if (NULL == json_metadata)
2779 : : {
2780 : 0 : rc = -ENOMEM;
2781 : 0 : goto out;
2782 : : }
2783 [ - + ]: 52 : json_object_object_add(json_metadata, "results",
2784 : 0 : json_metadata_arr ?: json_object_new_array() /* Empty array */);
2785 : :
2786 [ - + ]: 52 : if (NULL == value || NULL == key)
2787 : : {
2788 : 0 : rc = -EINVAL;
2789 : 0 : goto out;
2790 : : }
2791 : :
2792 [ - + ]: 52 : if (vfd >= 0)
2793 : 0 : dprintf (vfd, "debuginfod_find_metadata %s %s\n", key, value);
2794 : :
2795 : : /* Without query-able URL, we can stop here*/
2796 : 52 : urls_envvar = getenv(DEBUGINFOD_URLS_ENV_VAR);
2797 [ - + ]: 52 : if (vfd >= 0)
2798 [ # # ]: 0 : dprintf (vfd, "server urls \"%s\"\n",
2799 : : urls_envvar != NULL ? urls_envvar : "");
2800 [ + - + + ]: 52 : if (urls_envvar == NULL || urls_envvar[0] == '\0')
2801 : : {
2802 : 18 : rc = -ENOSYS;
2803 : 18 : goto out;
2804 : : }
2805 : :
2806 : : /* set paths needed to perform the query
2807 : : example format:
2808 : : cache_path: $HOME/.cache
2809 : : target_cache_dir: $HOME/.cache/metadata
2810 : : target_cache_path: $HOME/.cache/metadata/KEYENCODED_VALUEENCODED
2811 : : target_cache_path: $HOME/.cache/metadata/KEYENCODED_VALUEENCODED.XXXXXX
2812 : : */
2813 : :
2814 : : // libcurl > 7.62ish has curl_url_set()/etc. to construct these things more properly.
2815 : : // curl_easy_escape() is older
2816 : : {
2817 : 34 : CURL *c = curl_easy_init();
2818 [ - + ]: 34 : if (!c)
2819 : : {
2820 : 0 : rc = -ENOMEM;
2821 : 0 : goto out;
2822 : : }
2823 : 34 : char *key_escaped = curl_easy_escape(c, key, 0);
2824 : 34 : char *value_escaped = curl_easy_escape(c, value, 0);
2825 : :
2826 : : // fallback to unescaped values in unlikely case of error
2827 [ - + - + : 68 : xalloc_str (key_and_value, "key=%s&value=%s", key_escaped ?: key, value_escaped ?: value);
- + ]
2828 [ - + ]: 34 : xalloc_str (target_file_name, "%s_%s", key_escaped ?: key, value_escaped ?: value);
2829 : 34 : curl_free(value_escaped);
2830 : 34 : curl_free(key_escaped);
2831 : 34 : curl_easy_cleanup(c);
2832 : : }
2833 : :
2834 : : /* Check if we have a recent result already in the cache. */
2835 : 34 : cache_path = make_cache_path();
2836 [ - + ]: 34 : if (! cache_path)
2837 : : {
2838 : 0 : rc = -ENOMEM;
2839 : 0 : goto out;
2840 : : }
2841 [ - + ]: 34 : xalloc_str (target_cache_dir, "%s/metadata", cache_path);
2842 : 34 : (void) mkdir (target_cache_dir, 0700);
2843 [ - + ]: 34 : xalloc_str (target_cache_path, "%s/%s", target_cache_dir, target_file_name);
2844 [ - + ]: 34 : xalloc_str (target_cache_tmppath, "%s/%s.XXXXXX", target_cache_dir, target_file_name);
2845 : :
2846 : 34 : int fd = open(target_cache_path, O_RDONLY);
2847 [ + + ]: 34 : if (fd >= 0)
2848 : : {
2849 : 12 : struct stat st;
2850 : 12 : int metadata_retention = 0;
2851 : 12 : time_t now = time(NULL);
2852 : 12 : char *metadata_retention_path = 0;
2853 : :
2854 [ - + ]: 22 : xalloc_str (metadata_retention_path, "%s/%s", cache_path, metadata_retention_filename);
2855 [ + - ]: 12 : if (metadata_retention_path)
2856 : : {
2857 : 12 : rc = debuginfod_config_cache(client, metadata_retention_path,
2858 : : metadata_retention_default_s, &st);
2859 : 12 : free (metadata_retention_path);
2860 [ - + ]: 12 : if (rc < 0)
2861 : 0 : rc = 0;
2862 : : }
2863 : : else
2864 : : rc = 0;
2865 : 12 : metadata_retention = rc;
2866 : :
2867 [ - + ]: 12 : if (fstat(fd, &st) != 0)
2868 : : {
2869 : 0 : rc = -errno;
2870 : 0 : close (fd);
2871 : 0 : goto out;
2872 : : }
2873 : :
2874 [ + + + - ]: 12 : if (metadata_retention > 0 && (now - st.st_mtime <= metadata_retention))
2875 : : {
2876 [ - + ]: 10 : if (client && client->verbose_fd >= 0)
2877 : 0 : dprintf (client->verbose_fd, "cached metadata %s", target_file_name);
2878 : :
2879 [ + - ]: 10 : if (path != NULL)
2880 : : {
2881 : 10 : *path = target_cache_path; // pass over the pointer
2882 : 10 : target_cache_path = NULL; // prevent free() in our own cleanup
2883 : : }
2884 : :
2885 : : /* Success!!!! */
2886 : 10 : rc = fd;
2887 : 10 : goto out;
2888 : : }
2889 : :
2890 : : /* We don't have to clear the likely-expired cached object here
2891 : : by unlinking. We will shortly make a new request and save
2892 : : results right on top. Erasing here could trigger a TOCTOU
2893 : : race with another thread just finishing a query and passing
2894 : : its results back.
2895 : : */
2896 : : // (void) unlink (target_cache_path);
2897 : :
2898 : 2 : close (fd);
2899 : : }
2900 : :
2901 : : /* No valid cached metadata found: time to make the queries. */
2902 : :
2903 : 24 : free (client->url);
2904 : 24 : client->url = NULL;
2905 : :
2906 : 24 : long maxtime = 0;
2907 : 24 : const char *maxtime_envvar;
2908 : 24 : maxtime_envvar = getenv(DEBUGINFOD_MAXTIME_ENV_VAR);
2909 [ - + ]: 24 : if (maxtime_envvar != NULL)
2910 : 0 : maxtime = atol (maxtime_envvar);
2911 [ - + ]: 24 : if (maxtime && vfd >= 0)
2912 : 0 : dprintf(vfd, "using max time %lds\n", maxtime);
2913 : :
2914 : 24 : long timeout = default_timeout;
2915 : 24 : const char* timeout_envvar = getenv(DEBUGINFOD_TIMEOUT_ENV_VAR);
2916 [ - + ]: 24 : if (timeout_envvar != NULL)
2917 : 0 : timeout = atoi (timeout_envvar);
2918 [ - + ]: 24 : if (vfd >= 0)
2919 : 0 : dprintf (vfd, "using timeout %ld\n", timeout);
2920 : :
2921 : 24 : add_default_headers(client);
2922 : :
2923 : : /* Make a copy of the envvar so it can be safely modified. */
2924 : 24 : server_urls = strdup(urls_envvar);
2925 [ - + ]: 24 : if (server_urls == NULL)
2926 : : {
2927 : 0 : rc = -ENOMEM;
2928 : 0 : goto out;
2929 : : }
2930 : :
2931 : : /* Thereafter, goto out1 on error*/
2932 : :
2933 : 24 : char **server_url_list = NULL;
2934 : 24 : ima_policy_t* url_ima_policies = NULL;
2935 : 24 : char *server_url;
2936 : 24 : int num_urls = 0;
2937 : 24 : r = init_server_urls("metadata", NULL, server_urls, &server_url_list, &url_ima_policies, &num_urls, vfd);
2938 [ - + ]: 24 : if (0 != r)
2939 : : {
2940 : 0 : rc = r;
2941 : 0 : goto out1;
2942 : : }
2943 : :
2944 : 24 : CURLM *curlm = client->server_mhandle;
2945 : :
2946 : 24 : CURL *target_handle = NULL;
2947 : 24 : data = malloc(sizeof(struct handle_data) * num_urls);
2948 [ - + ]: 24 : if (data == NULL)
2949 : : {
2950 : 0 : rc = -ENOMEM;
2951 : 0 : goto out1;
2952 : : }
2953 : :
2954 : : /* thereafter, goto out2 on error. */
2955 : :
2956 : : /* Initialize handle_data */
2957 [ + + ]: 60 : for (int i = 0; i < num_urls; i++)
2958 : : {
2959 [ + - ]: 36 : if ((server_url = server_url_list[i]) == NULL)
2960 : : break;
2961 [ - + ]: 36 : if (vfd >= 0)
2962 : 0 : dprintf (vfd, "init server %d %s\n", i, server_url);
2963 : :
2964 : 36 : data[i].errbuf[0] = '\0';
2965 : 36 : data[i].target_handle = &target_handle;
2966 : 36 : data[i].client = client;
2967 : 36 : data[i].metadata = NULL;
2968 : 36 : data[i].metadata_size = 0;
2969 : 36 : data[i].response_data = NULL;
2970 : 36 : data[i].response_data_size = 0;
2971 : :
2972 : 36 : snprintf(data[i].url, PATH_MAX, "%s?%s", server_url, key_and_value);
2973 : :
2974 : 36 : r = init_handle(client, metadata_callback, header_callback, &data[i], i, timeout, vfd);
2975 [ - + ]: 36 : if (0 != r)
2976 : : {
2977 : 0 : rc = r;
2978 : 0 : goto out2;
2979 : : }
2980 : 36 : curl_multi_add_handle(curlm, data[i].handle);
2981 : : }
2982 : :
2983 : : /* Query servers */
2984 [ - + ]: 24 : if (vfd >= 0)
2985 : 0 : dprintf (vfd, "Starting %d queries\n",num_urls);
2986 : 24 : int committed_to;
2987 : 24 : r = perform_queries(curlm, NULL, data, client, num_urls, maxtime, 0, false, vfd, &committed_to);
2988 [ - + ]: 24 : if (0 != r)
2989 : : {
2990 : 0 : rc = r;
2991 : 0 : goto out2;
2992 : : }
2993 : :
2994 : : /* NOTE: We don't check the return codes of the curl messages since
2995 : : a metadata query failing silently is just fine. We want to know what's
2996 : : available from servers which can be connected with no issues.
2997 : : If running with additional verbosity, the failure will be noted in stderr */
2998 : :
2999 : : /* Building the new json array from all the upstream data and
3000 : : cleanup while at it.
3001 : : */
3002 [ + + ]: 60 : for (int i = 0; i < num_urls; i++)
3003 : : {
3004 : 36 : curl_multi_remove_handle(curlm, data[i].handle); /* ok to repeat */
3005 : 36 : curl_easy_cleanup (data[i].handle);
3006 : 36 : free (data[i].response_data);
3007 : :
3008 [ + + ]: 36 : if (NULL == data[i].metadata)
3009 : : {
3010 [ - + ]: 16 : if (vfd >= 0)
3011 : 0 : dprintf (vfd, "Query to %s failed with error message:\n\t\"%s\"\n",
3012 : 0 : data[i].url, data[i].errbuf);
3013 : 16 : json_metadata_complete = false;
3014 : 16 : continue;
3015 : : }
3016 : :
3017 : 20 : json_object *upstream_metadata = json_tokener_parse(data[i].metadata);
3018 : 20 : json_object *upstream_complete;
3019 : 20 : json_object *upstream_metadata_arr;
3020 [ + - + - ]: 40 : if (NULL == upstream_metadata ||
3021 [ - + ]: 40 : !json_object_object_get_ex(upstream_metadata, "results", &upstream_metadata_arr) ||
3022 : 20 : !json_object_object_get_ex(upstream_metadata, "complete", &upstream_complete))
3023 : 0 : continue;
3024 : 20 : json_metadata_complete &= json_object_get_boolean(upstream_complete);
3025 : : // Combine the upstream metadata into the json array
3026 [ + + ]: 38 : for (int j = 0, n = json_object_array_length(upstream_metadata_arr); j < n; j++)
3027 : : {
3028 : 18 : json_object *entry = json_object_array_get_idx(upstream_metadata_arr, j);
3029 : 18 : json_object_get(entry); // increment reference count
3030 : 18 : json_object_array_add(json_metadata_arr, entry);
3031 : : }
3032 : 20 : json_object_put(upstream_metadata);
3033 : :
3034 : 20 : free (data[i].metadata);
3035 : : }
3036 : :
3037 : : /* Because of race with cache cleanup / rmdir, try to mkdir/mkstemp up to twice. */
3038 [ + - ]: 24 : for (int i=0; i<2; i++)
3039 : : {
3040 : : /* (re)create target directory in cache */
3041 : 24 : (void) mkdir(target_cache_dir, 0700); /* files will be 0400 later */
3042 : :
3043 : : /* NB: write to a temporary file first, to avoid race condition of
3044 : : multiple clients checking the cache, while a partially-written or empty
3045 : : file is in there, being written from libcurl. */
3046 : 24 : fd = mkstemp (target_cache_tmppath);
3047 [ - + ]: 24 : if (fd >= 0) break;
3048 : : }
3049 [ - + ]: 24 : if (fd < 0) /* Still failed after two iterations. */
3050 : : {
3051 : 0 : rc = -errno;
3052 : 0 : goto out1;
3053 : : }
3054 : :
3055 : : /* Plop the complete json_metadata object into the cache. */
3056 : 24 : json_object_object_add(json_metadata, "complete", json_object_new_boolean(json_metadata_complete));
3057 : 24 : const char* json_string = json_object_to_json_string_ext(json_metadata, JSON_C_TO_STRING_PRETTY);
3058 [ - + ]: 24 : if (json_string == NULL)
3059 : : {
3060 : 0 : rc = -ENOMEM;
3061 : 0 : goto out1;
3062 : : }
3063 : 24 : ssize_t res = write_retry (fd, json_string, strlen(json_string));
3064 : 24 : (void) lseek(fd, 0, SEEK_SET); // rewind file so client can read it from the top
3065 : :
3066 : : /* NB: json_string is auto deleted when json_metadata object is nuked */
3067 [ + - - + ]: 24 : if (res < 0 || (size_t) res != strlen(json_string))
3068 : : {
3069 : 0 : rc = -EIO;
3070 : 0 : goto out1;
3071 : : }
3072 : : /* PR27571: make cache files casually unwriteable; dirs are already 0700 */
3073 : 24 : (void) fchmod(fd, 0400);
3074 : :
3075 : : /* rename tmp->real */
3076 : 24 : rc = rename (target_cache_tmppath, target_cache_path);
3077 [ - + ]: 24 : if (rc < 0)
3078 : : {
3079 : 0 : rc = -errno;
3080 : 0 : goto out1;
3081 : : /* Perhaps we need not give up right away; could retry or something ... */
3082 : : }
3083 : :
3084 : : /* don't close fd - we're returning it */
3085 : : /* don't unlink the tmppath; it's already been renamed. */
3086 [ + - ]: 24 : if (path != NULL)
3087 : 24 : *path = strdup(target_cache_path);
3088 : :
3089 : 24 : rc = fd;
3090 : 24 : goto out1;
3091 : :
3092 : : /* error exits */
3093 : 0 : out2:
3094 : : /* remove all handles from multi */
3095 [ # # ]: 0 : for (int i = 0; i < num_urls; i++)
3096 : : {
3097 [ # # ]: 0 : if (data[i].handle != NULL)
3098 : : {
3099 : 0 : curl_multi_remove_handle(curlm, data[i].handle); /* ok to repeat */
3100 : 0 : curl_easy_cleanup (data[i].handle);
3101 : 0 : free (data[i].response_data);
3102 : 0 : free (data[i].metadata);
3103 : : }
3104 : : }
3105 : :
3106 : 0 : out1:
3107 : 24 : free(data);
3108 : :
3109 [ + + ]: 60 : for (int i = 0; i < num_urls; ++i)
3110 : 36 : free(server_url_list[i]);
3111 : 24 : free(server_url_list);
3112 : 24 : free(url_ima_policies);
3113 : :
3114 : 52 : out:
3115 : 52 : free (server_urls);
3116 : 52 : json_object_put(json_metadata);
3117 : : /* Reset sent headers */
3118 : 52 : curl_slist_free_all (client->headers);
3119 : 52 : client->headers = NULL;
3120 : 52 : client->user_agent_set_p = 0;
3121 : :
3122 : 52 : free (target_cache_dir);
3123 : 52 : free (target_cache_path);
3124 : 52 : free (target_cache_tmppath);
3125 : 52 : free (key_and_value);
3126 : 52 : free (target_file_name);
3127 : 52 : free (cache_path);
3128 : :
3129 : 52 : return rc;
3130 : : }
3131 : :
3132 : :
3133 : : /* Add an outgoing HTTP header. */
3134 : 2882 : int debuginfod_add_http_header (debuginfod_client *client, const char* header)
3135 : : {
3136 : : /* Sanity check header value is of the form Header: Value.
3137 : : It should contain at least one colon that isn't the first or
3138 : : last character. */
3139 : 2882 : const char *colon = strchr (header, ':'); /* first colon */
3140 : 2882 : if (colon == NULL /* present */
3141 [ + - ]: 2882 : || colon == header /* not at beginning - i.e., have a header name */
3142 [ + - ]: 2882 : || *(colon + 1) == '\0') /* not at end - i.e., have a value */
3143 : : /* NB: but it's okay for a value to contain other colons! */
3144 : : return -EINVAL;
3145 : :
3146 : 2882 : struct curl_slist *temp = curl_slist_append (client->headers, header);
3147 [ + - ]: 2882 : if (temp == NULL)
3148 : : return -ENOMEM;
3149 : :
3150 : : /* Track if User-Agent: is being set. If so, signal not to add the
3151 : : default one. */
3152 [ + + ]: 2882 : if (startswith (header, "User-Agent:"))
3153 : 2224 : client->user_agent_set_p = 1;
3154 : :
3155 : 2882 : client->headers = temp;
3156 : 2882 : return 0;
3157 : : }
3158 : :
3159 : :
3160 : : void
3161 : 1126 : debuginfod_set_progressfn(debuginfod_client *client,
3162 : : debuginfod_progressfn_t fn)
3163 : : {
3164 : 1126 : client->progressfn = fn;
3165 : 1126 : }
3166 : :
3167 : : void
3168 : 72 : debuginfod_set_verbose_fd(debuginfod_client *client, int fd)
3169 : : {
3170 : 72 : client->verbose_fd = fd;
3171 : 72 : }
3172 : :
3173 : : #endif /* DUMMY_LIBDEBUGINFOD */
|