Coverage Report

Created: 2026-02-25 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/src/svr-authpubkey.c
Line
Count
Source
1
/*
2
 * Dropbear - a SSH2 server
3
 *
4
 * Copyright (c) 2002,2003 Matt Johnston
5
 * All rights reserved.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE. */
24
/*
25
 * This file incorporates work covered by the following copyright and
26
 * permission notice:
27
 *
28
 *  Copyright (c) 2000 Markus Friedl.  All rights reserved.
29
 *
30
 *  Redistribution and use in source and binary forms, with or without
31
 *  modification, are permitted provided that the following conditions
32
 *  are met:
33
 *  1. Redistributions of source code must retain the above copyright
34
 *     notice, this list of conditions and the following disclaimer.
35
 *  2. Redistributions in binary form must reproduce the above copyright
36
 *     notice, this list of conditions and the following disclaimer in the
37
 *     documentation and/or other materials provided with the distribution.
38
 *
39
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
40
 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41
 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
43
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48
 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49
 *
50
 * This copyright and permission notice applies to the code parsing public keys
51
 * options string which can also be found in OpenSSH auth2-pubkey.c file
52
 * (user_key_allowed2). It has been adapted to work with buffers.
53
 *
54
 */
55
56
/* Process a pubkey auth request */
57
58
#include "includes.h"
59
#include "session.h"
60
#include "dbutil.h"
61
#include "buffer.h"
62
#include "signkey.h"
63
#include "auth.h"
64
#include "ssh.h"
65
#include "packet.h"
66
#include "algo.h"
67
#include "runopts.h"
68
69
#if DROPBEAR_SVR_PUBKEY_AUTH
70
71
0
#define MIN_AUTHKEYS_LINE 10 /* "ssh-rsa AB" - short but doesn't matter */
72
0
#define MAX_AUTHKEYS_LINE 4200 /* max length of a line in authkeys */
73
74
static char * authorized_keys_filepath(void);
75
static int checkpubkey(const char* keyalgo, unsigned int keyalgolen,
76
    const unsigned char* keyblob, unsigned int keybloblen);
77
static int checkpubkeyperms(void);
78
static void send_msg_userauth_pk_ok(const char* sigalgo, unsigned int sigalgolen,
79
    const unsigned char* keyblob, unsigned int keybloblen);
80
static int checkfileperm(char * filename);
81
82
/* process a pubkey auth request, sending success or failure message as
83
 * appropriate */
84
0
void svr_auth_pubkey(int valid_user) {
85
86
0
  unsigned char testkey; /* whether we're just checking if a key is usable */
87
0
  char* sigalgo = NULL;
88
0
  unsigned int sigalgolen;
89
0
  const char* keyalgo;
90
0
  unsigned int keyalgolen;
91
0
  unsigned char* keyblob = NULL;
92
0
  unsigned int keybloblen;
93
0
  unsigned int sign_payload_length;
94
0
  buffer * signbuf = NULL;
95
0
  sign_key * key = NULL;
96
0
  char* fp = NULL;
97
0
  enum signature_type sigtype;
98
0
  enum signkey_type keytype;
99
0
    int auth_failure = 1;
100
101
0
  TRACE(("enter pubkeyauth"))
102
103
  /* 0 indicates user just wants to check if key can be used, 1 is an
104
   * actual attempt*/
105
0
  testkey = (buf_getbool(ses.payload) == 0);
106
107
0
  sigalgo = buf_getstring(ses.payload, &sigalgolen);
108
0
  keybloblen = buf_getint(ses.payload);
109
0
  keyblob = buf_getptr(ses.payload, keybloblen);
110
111
0
  if (!valid_user) {
112
    /* Return failure once we have read the contents of the packet
113
    required to validate a public key.
114
    Avoids blind user enumeration though it isn't possible to prevent
115
    testing for user existence if the public key is known */
116
0
    send_msg_userauth_failure(0, 0);
117
0
    goto out;
118
0
  }
119
120
0
  sigtype = signature_type_from_name(sigalgo, sigalgolen);
121
0
  if (sigtype == DROPBEAR_SIGNATURE_NONE) {
122
0
    send_msg_userauth_failure(0, 0);
123
0
    goto out;
124
0
  }
125
126
0
  keytype = signkey_type_from_signature(sigtype);
127
0
  keyalgo = signkey_name_from_type(keytype, &keyalgolen);
128
129
#if DROPBEAR_PLUGIN
130
    if (svr_ses.plugin_instance != NULL) {
131
        char *options_buf;
132
        if (svr_ses.plugin_instance->checkpubkey(
133
                    svr_ses.plugin_instance,
134
                    &ses.plugin_session,
135
                    keyalgo,
136
                    keyalgolen,
137
                    keyblob,
138
                    keybloblen,
139
                    ses.authstate.username) == DROPBEAR_SUCCESS) {
140
            /* Success */
141
            auth_failure = 0;
142
143
            /* Options provided? */
144
            options_buf = ses.plugin_session->get_options(ses.plugin_session);
145
            if (options_buf) {
146
                struct buf temp_buf = {
147
                    .data = (unsigned char *)options_buf,
148
                    .len = strlen(options_buf),
149
                    .pos = 0,
150
                    .size = 0
151
                };
152
                int ret = svr_add_pubkey_options(&temp_buf, 0, "N/A");
153
                if (ret == DROPBEAR_FAILURE) {
154
                    /* Fail immediately as the plugin provided wrong options */
155
                    send_msg_userauth_failure(0, 0);
156
                    goto out;
157
                }
158
            }
159
        }
160
    }
161
#endif
162
  /* check if the key is valid */
163
0
  if (auth_failure) {
164
0
      auth_failure = checkpubkey(keyalgo, keyalgolen, keyblob, keybloblen) == DROPBEAR_FAILURE;
165
0
  }
166
167
0
  if (auth_failure) {
168
    /* MAX_PUBKEY_QUERIES allows a greater limit of pubkey queries
169
     * than the standard maxauthtries.
170
     * Start counting failures (incrfail) only when it's reaching
171
     * the limit.
172
     */
173
0
    unsigned int free_query_limit = 0;
174
0
      MAX(0, (int)svr_opts.maxauthtries - MAX_PUBKEY_QUERIES);
175
0
    int incrfail = ses.authstate.serv_pubkey_query_count > free_query_limit;
176
0
    send_msg_userauth_failure(0, incrfail);
177
0
    ses.authstate.serv_pubkey_query_count++;
178
0
    goto out;
179
0
  }
180
181
  /* let them know that the key is ok to use */
182
0
  if (testkey) {
183
0
    send_msg_userauth_pk_ok(sigalgo, sigalgolen, keyblob, keybloblen);
184
0
    goto out;
185
0
  }
186
187
  /* now we can actually verify the signature */
188
189
  /* get the key */
190
0
  key = new_sign_key();
191
0
  if (buf_get_pub_key(ses.payload, key, &keytype) == DROPBEAR_FAILURE) {
192
0
    send_msg_userauth_failure(0, 1);
193
0
    goto out;
194
0
  }
195
196
0
#if DROPBEAR_SK_ECDSA || DROPBEAR_SK_ED25519
197
0
  key->sk_flags_mask = SSH_SK_USER_PRESENCE_REQD;
198
0
#if DROPBEAR_SVR_PUBKEY_OPTIONS_BUILT
199
0
  if (ses.authstate.pubkey_options && ses.authstate.pubkey_options->no_touch_required_flag) {
200
0
    key->sk_flags_mask &= ~SSH_SK_USER_PRESENCE_REQD;
201
0
  }
202
0
  if (ses.authstate.pubkey_options && ses.authstate.pubkey_options->verify_required_flag) {
203
0
    key->sk_flags_mask |= SSH_SK_USER_VERIFICATION_REQD;
204
0
  }
205
0
#endif /* DROPBEAR_SVR_PUBKEY_OPTIONS */
206
0
#endif
207
208
  /* create the data which has been signed - this a string containing
209
   * session_id, concatenated with the payload packet up to the signature */
210
0
  assert(ses.payload_beginning <= ses.payload->pos);
211
0
  sign_payload_length = ses.payload->pos - ses.payload_beginning;
212
0
  signbuf = buf_new(ses.payload->pos + 4 + ses.session_id->len);
213
0
  buf_putbufstring(signbuf, ses.session_id);
214
215
  /* The entire contents of the payload prior. */
216
0
  buf_setpos(ses.payload, ses.payload_beginning);
217
0
  buf_putbytes(signbuf,
218
0
    buf_getptr(ses.payload, sign_payload_length),
219
0
    sign_payload_length);
220
0
  buf_incrpos(ses.payload, sign_payload_length);
221
222
0
  buf_setpos(signbuf, 0);
223
224
  /* ... and finally verify the signature */
225
0
  fp = sign_key_fingerprint(keyblob, keybloblen);
226
0
  if (buf_verify(ses.payload, key, sigtype, signbuf) == DROPBEAR_SUCCESS) {
227
0
    if (svr_opts.multiauthmethod && (ses.authstate.authtypes & ~AUTH_TYPE_PUBKEY)) {
228
      /* successful pubkey authentication, but extra auth required */
229
0
      dropbear_log(LOG_NOTICE,
230
0
          "Pubkey auth succeeded for '%s' with %s key %s from %s, extra auth required",
231
0
          ses.authstate.pw_name,
232
0
          signkey_name_from_type(keytype, NULL), fp,
233
0
          svr_ses.addrstring);
234
0
      ses.authstate.authtypes &= ~AUTH_TYPE_PUBKEY; /* pubkey auth ok, delete the method flag */
235
0
      send_msg_userauth_failure(1, 0); /* Send partial success */
236
0
    } else {
237
      /* successful authentication */
238
0
      dropbear_log(LOG_NOTICE,
239
0
          "Pubkey auth succeeded for '%s' with %s key %s from %s",
240
0
          ses.authstate.pw_name,
241
0
          signkey_name_from_type(keytype, NULL), fp,
242
0
          svr_ses.addrstring);
243
0
      send_msg_userauth_success();
244
0
    }
245
#if DROPBEAR_PLUGIN
246
    if ((ses.plugin_session != NULL) && (svr_ses.plugin_instance->auth_success != NULL)) {
247
        /* Was authenticated through the external plugin. tell plugin that signature verification was ok */
248
        svr_ses.plugin_instance->auth_success(ses.plugin_session);
249
    }
250
#endif
251
0
  } else {
252
0
    dropbear_log(LOG_WARNING,
253
0
        "Pubkey auth bad signature for '%s' with key %s from %s",
254
0
        ses.authstate.pw_name, fp, svr_ses.addrstring);
255
0
    send_msg_userauth_failure(0, 1);
256
0
  }
257
0
  m_free(fp);
258
259
0
out:
260
  /* cleanup stuff */
261
0
  if (signbuf) {
262
0
    buf_free(signbuf);
263
0
  }
264
0
  if (sigalgo) {
265
0
    m_free(sigalgo);
266
0
  }
267
0
  if (key) {
268
0
    sign_key_free(key);
269
0
    key = NULL;
270
0
  }
271
  /* Retain pubkey options only if auth succeeded */
272
0
  if (!ses.authstate.authdone) {
273
0
    svr_pubkey_options_cleanup();
274
0
  }
275
0
  TRACE(("leave pubkeyauth"))
276
0
}
277
278
/* Reply that the key is valid for auth, this is sent when the user sends
279
 * a straight copy of their pubkey to test, to avoid having to perform
280
 * expensive signing operations with a worthless key */
281
static void send_msg_userauth_pk_ok(const char* sigalgo, unsigned int sigalgolen,
282
0
    const unsigned char* keyblob, unsigned int keybloblen) {
283
284
0
  TRACE(("enter send_msg_userauth_pk_ok"))
285
0
  CHECKCLEARTOWRITE();
286
287
0
  buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_PK_OK);
288
0
  buf_putstring(ses.writepayload, sigalgo, sigalgolen);
289
0
  buf_putstring(ses.writepayload, (const char*)keyblob, keybloblen);
290
291
0
  encrypt_packet();
292
0
  TRACE(("leave send_msg_userauth_pk_ok"))
293
294
0
}
295
296
/* Content for SSH_PUBKEYINFO is optionally returned malloced in ret_info (will be
297
   freed if already set */
298
static int checkpubkey_line(buffer* line, int line_num, const char* filename,
299
    const char* algo, unsigned int algolen,
300
    const unsigned char* keyblob, unsigned int keybloblen,
301
0
    char ** ret_info) {
302
0
  buffer *options_buf = NULL;
303
0
  char *info_str = NULL;
304
0
  unsigned int pos, len, infopos, infolen;
305
0
  int ret = DROPBEAR_FAILURE;
306
307
0
  if (line->len < MIN_AUTHKEYS_LINE || line->len > MAX_AUTHKEYS_LINE) {
308
0
    TRACE(("checkpubkey_line: bad line length %d", line->len))
309
0
    goto out;
310
0
  }
311
312
0
  if (memchr(line->data, 0x0, line->len) != NULL) {
313
0
    TRACE(("checkpubkey_line: bad line has null char"))
314
0
    goto out;
315
0
  }
316
317
  /* compare the algorithm. +3 so we have enough bytes to read a space and some base64 characters too. */
318
0
  if (line->pos + algolen+3 > line->len) {
319
0
    goto out;
320
0
  }
321
  /* check the key type */
322
0
  if (strncmp((const char *) buf_getptr(line, algolen), algo, algolen) != 0) {
323
0
    int is_comment = 0;
324
0
    unsigned char *options_start = NULL;
325
0
    int options_len = 0;
326
0
    int escape, quoted;
327
328
    /* skip over any comments or leading whitespace */
329
0
    while (line->pos < line->len) {
330
0
      const char c = buf_getbyte(line);
331
0
      if (c == ' ' || c == '\t') {
332
0
        continue;
333
0
      } else if (c == '#') {
334
0
        is_comment = 1;
335
0
        break;
336
0
      }
337
0
      buf_decrpos(line, 1);
338
0
      break;
339
0
    }
340
0
    if (is_comment) {
341
      /* next line */
342
0
      goto out;
343
0
    }
344
345
    /* remember start of options */
346
0
    options_start = buf_getptr(line, 1);
347
0
    quoted = 0;
348
0
    escape = 0;
349
0
    options_len = 0;
350
351
    /* figure out where the options are */
352
0
    while (line->pos < line->len) {
353
0
      const char c = buf_getbyte(line);
354
0
      if (!quoted && (c == ' ' || c == '\t')) {
355
0
        break;
356
0
      }
357
0
      escape = (!escape && c == '\\');
358
0
      if (!escape && c == '"') {
359
0
        quoted = !quoted;
360
0
      }
361
0
      options_len++;
362
0
    }
363
0
    options_buf = buf_new(options_len);
364
0
    buf_putbytes(options_buf, options_start, options_len);
365
366
    /* compare the algorithm. +3 so we have enough bytes to read a space and some base64 characters too. */
367
0
    if (line->pos + algolen+3 > line->len) {
368
0
      goto out;
369
0
    }
370
0
    if (strncmp((const char *) buf_getptr(line, algolen), algo, algolen) != 0) {
371
0
      goto out;
372
0
    }
373
0
  }
374
0
  buf_incrpos(line, algolen);
375
376
  /* check for space (' ') character */
377
0
  if (buf_getbyte(line) != ' ') {
378
0
    TRACE(("checkpubkey_line: space character expected, isn't there"))
379
0
    goto out;
380
0
  }
381
382
  /* find the length of base64 data */
383
0
  pos = line->pos;
384
0
  for (len = 0; line->pos < line->len; len++) {
385
0
    if (buf_getbyte(line) == ' ') {
386
0
      break;
387
0
    }
388
0
  }
389
390
  /* find out the length of the public key info, stop at the first space */
391
0
  infopos = line->pos;
392
0
  for (infolen = 0; line->pos < line->len; infolen++) {
393
0
    const char c = buf_getbyte(line);
394
0
    if (c == ' ') {
395
0
      break;
396
0
    }
397
    /* We have an allowlist - authorized_keys lines can't be fully trusted,
398
    some shell scripts may do unsafe things with env var values */
399
0
    if (!(isalnum(c) || strchr(".,_-+@", c))) {
400
0
      TRACE(("Not setting SSH_PUBKEYINFO, special characters"))
401
0
      infolen = 0;
402
0
      break;
403
0
    }
404
0
  }
405
0
  if (infolen > 0) {
406
0
    info_str = m_malloc(infolen + 1);
407
0
    buf_setpos(line, infopos);
408
0
  strncpy(info_str, buf_getptr(line, infolen), infolen);
409
0
  }
410
411
  /* truncate to base64 data length */
412
0
  buf_setpos(line, pos);
413
0
  buf_setlen(line, line->pos + len);
414
415
0
  TRACE(("checkpubkey_line: line pos = %d len = %d", line->pos, line->len))
416
417
0
  ret = cmp_base64_key(keyblob, keybloblen, (const unsigned char *) algo, algolen, line, NULL);
418
419
  /* free pubkey_info if it is filled */
420
0
  if (ret_info && *ret_info) {
421
0
    m_free(*ret_info);
422
0
    *ret_info = NULL;
423
0
  }
424
425
0
  if (ret == DROPBEAR_SUCCESS) {
426
0
    if (options_buf) {
427
0
      ret = svr_add_pubkey_options(options_buf, line_num, filename);
428
0
    }
429
0
    if (ret_info) {
430
      /* take the (optional) public key information */
431
0
      *ret_info = info_str;
432
0
      info_str = NULL;
433
0
    }
434
0
  }
435
436
0
out:
437
0
  if (options_buf) {
438
0
    buf_free(options_buf);
439
0
  }
440
0
  if (info_str) {
441
0
    m_free(info_str);
442
0
  }
443
0
  return ret;
444
0
}
445
446
/* Returns the full path to the user's authorized_keys file in an
447
 * allocated string which caller must free. */
448
0
static char *authorized_keys_filepath() {
449
0
  size_t len = 0;
450
0
  char *pathname = NULL, *dir = NULL;
451
0
  const char *filename = "authorized_keys";
452
453
0
  dir = expand_homedir_path_home(svr_opts.authorized_keys_dir,
454
0
               ses.authstate.pw_dir);
455
456
  /* allocate max required pathname storage,
457
   * = dir + "/" + "authorized_keys" + '\0' */;
458
0
  len = strlen(dir) + strlen(filename) + 2;
459
0
  pathname = m_malloc(len);
460
0
  snprintf(pathname, len, "%s/%s", dir, filename);
461
0
  m_free(dir);
462
0
  return pathname;
463
0
}
464
465
/* Checks whether a specified publickey (and associated algorithm) is an
466
 * acceptable key for authentication */
467
/* Returns DROPBEAR_SUCCESS if key is ok for auth, DROPBEAR_FAILURE otherwise */
468
static int checkpubkey(const char* keyalgo, unsigned int keyalgolen,
469
0
    const unsigned char* keyblob, unsigned int keybloblen) {
470
471
0
  FILE * authfile = NULL;
472
0
  char * filename = NULL;
473
0
  int ret = DROPBEAR_FAILURE;
474
0
  buffer * line = NULL;
475
0
  int line_num;
476
0
  uid_t origuid;
477
0
  gid_t origgid;
478
479
0
  TRACE(("enter checkpubkey"))
480
481
0
#if DROPBEAR_SVR_MULTIUSER
482
  /* access the file as the authenticating user. */
483
0
  origuid = getuid();
484
0
  origgid = getgid();
485
0
  if ((setegid(ses.authstate.pw_gid)) < 0 ||
486
0
    (seteuid(ses.authstate.pw_uid)) < 0) {
487
0
    dropbear_exit("Failed to set euid");
488
0
  }
489
0
#endif
490
  /* check file permissions, also whether file exists */
491
0
  if (checkpubkeyperms() == DROPBEAR_FAILURE) {
492
0
    TRACE(("bad authorized_keys permissions, or file doesn't exist"))
493
0
  } else {
494
    /* we don't need to check pw and pw_dir for validity, since
495
     * its been done in checkpubkeyperms. */
496
0
    filename = authorized_keys_filepath();
497
0
    authfile = fopen(filename, "r");
498
0
    if (!authfile) {
499
0
      TRACE(("checkpubkey: failed opening %s: %s", filename, strerror(errno)))
500
0
    }
501
0
  }
502
0
#if DROPBEAR_SVR_MULTIUSER
503
0
  if ((seteuid(origuid)) < 0 ||
504
0
    (setegid(origgid)) < 0) {
505
0
    dropbear_exit("Failed to revert euid");
506
0
  }
507
0
#endif
508
509
0
  if (authfile == NULL) {
510
0
    goto out;
511
0
  }
512
0
  TRACE(("checkpubkey: opened authorized_keys OK"))
513
514
0
  line = buf_new(MAX_AUTHKEYS_LINE);
515
0
  line_num = 0;
516
517
  /* iterate through the lines */
518
0
  do {
519
0
    if (buf_getline(line, authfile) == DROPBEAR_FAILURE) {
520
      /* EOF reached */
521
0
      TRACE(("checkpubkey: authorized_keys EOF reached"))
522
0
      break;
523
0
    }
524
0
    line_num++;
525
526
0
    ret = checkpubkey_line(line, line_num, filename, keyalgo, keyalgolen,
527
0
      keyblob, keybloblen,
528
0
#if DROPBEAR_SVR_PUBKEY_OPTIONS_BUILT
529
0
      &ses.authstate.pubkey_info
530
#else
531
      NULL
532
#endif
533
0
    );
534
0
    if (ret == DROPBEAR_SUCCESS) {
535
0
      break;
536
0
    }
537
538
    /* We continue to the next line otherwise */
539
0
  } while (1);
540
541
0
out:
542
0
  if (authfile) {
543
0
    fclose(authfile);
544
0
  }
545
0
  if (line) {
546
0
    buf_free(line);
547
0
  }
548
0
  m_free(filename);
549
0
  TRACE(("leave checkpubkey: ret=%d", ret))
550
0
  return ret;
551
0
}
552
553
554
/* Returns DROPBEAR_SUCCESS if file permissions for pubkeys are ok,
555
 * DROPBEAR_FAILURE otherwise.
556
 * Checks that the authorized_keys path permissions are all owned by either
557
 * root or the user, and are g-w, o-w.
558
 * When this path is inside the user's home dir it checks up to and including
559
 * the home dir, otherwise it checks every path component. */
560
0
static int checkpubkeyperms() {
561
0
  char *path = authorized_keys_filepath(), *sep = NULL;
562
0
  int ret = DROPBEAR_SUCCESS;
563
564
0
  TRACE(("enter checkpubkeyperms"))
565
566
  /* Walk back up path checking permissions, stopping at either homedir,
567
   * or root if the path is outside of the homedir. */
568
0
  while ((sep = strrchr(path, '/')) != NULL) {
569
0
    if (sep == path) { /* root directory */
570
0
      sep++;
571
0
    }
572
0
    *sep = '\0';
573
0
    if (checkfileperm(path) != DROPBEAR_SUCCESS) {
574
0
      TRACE(("checkpubkeyperms: bad perm on %s", path))
575
0
      ret = DROPBEAR_FAILURE;
576
0
    }
577
0
    if (strcmp(path, ses.authstate.pw_dir) == 0 || strcmp(path, "/") == 0) {
578
0
      break;
579
0
    }
580
0
  }
581
582
  /* all looks ok, return success */
583
0
  m_free(path);
584
585
0
  TRACE(("leave checkpubkeyperms"))
586
0
  return ret;
587
0
}
588
589
/* Checks that a file is owned by the user or root, and isn't writable by
590
 * group or other */
591
/* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
592
0
static int checkfileperm(char * filename) {
593
0
  struct stat filestat;
594
0
  int badperm = 0;
595
596
0
  TRACE(("enter checkfileperm(%s)", filename))
597
598
0
  if (stat(filename, &filestat) != 0) {
599
0
    TRACE(("leave checkfileperm: stat() != 0"))
600
0
    return DROPBEAR_FAILURE;
601
0
  }
602
  /* check ownership - user or root only*/
603
0
  if (filestat.st_uid != ses.authstate.pw_uid
604
0
      && filestat.st_uid != 0) {
605
0
    badperm = 1;
606
0
    TRACE(("wrong ownership"))
607
0
  }
608
  /* check permissions - don't want group or others +w */
609
0
  if (filestat.st_mode & (S_IWGRP | S_IWOTH)) {
610
0
    badperm = 1;
611
0
    TRACE(("wrong perms"))
612
0
  }
613
0
  if (badperm) {
614
0
    if (!ses.authstate.perm_warn) {
615
0
      ses.authstate.perm_warn = 1;
616
0
      dropbear_log(LOG_INFO, "%s must be owned by user or root, and not writable by group or others", filename);
617
0
    }
618
0
    TRACE(("leave checkfileperm: failure perms/owner"))
619
0
    return DROPBEAR_FAILURE;
620
0
  }
621
622
0
  TRACE(("leave checkfileperm: success"))
623
0
  return DROPBEAR_SUCCESS;
624
0
}
625
626
#if DROPBEAR_FUZZ
627
int fuzz_checkpubkey_line(buffer* line, int line_num, char* filename,
628
    const char* algo, unsigned int algolen,
629
0
    const unsigned char* keyblob, unsigned int keybloblen) {
630
  return checkpubkey_line(line, line_num, filename, algo, algolen, keyblob, keybloblen, NULL);
631
0
}
632
#endif
633
634
#endif