Coverage Report

Created: 2025-10-13 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/src/svr-auth.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 (auth.c) handles authentication requests, passing it to the
26
 * particular type (auth-passwd, auth-pubkey). */
27
28
29
#include "includes.h"
30
#include "dbutil.h"
31
#include "session.h"
32
#include "buffer.h"
33
#include "ssh.h"
34
#include "packet.h"
35
#include "auth.h"
36
#include "runopts.h"
37
#include "dbrandom.h"
38
39
static int checkusername(const char *username, unsigned int userlen);
40
41
/* initialise the first time for a session, resetting all parameters */
42
8.03k
void svr_authinitialise() {
43
8.03k
  memset(&ses.authstate, 0, sizeof(ses.authstate));
44
8.03k
#if DROPBEAR_SVR_PUBKEY_AUTH
45
8.03k
  ses.authstate.authtypes |= AUTH_TYPE_PUBKEY;
46
8.03k
#endif
47
8.03k
#if DROPBEAR_SVR_PASSWORD_AUTH || DROPBEAR_SVR_PAM_AUTH
48
8.03k
  if (!svr_opts.noauthpass) {
49
8.03k
    ses.authstate.authtypes |= AUTH_TYPE_PASSWORD;
50
8.03k
  }
51
8.03k
#endif
52
8.03k
}
53
54
/* Send a banner message if specified to the client. The client might
55
 * ignore this, but possibly serves as a legal "no trespassing" sign */
56
0
void send_msg_userauth_banner(const buffer *banner) {
57
58
0
  TRACE(("enter send_msg_userauth_banner"))
59
60
0
  CHECKCLEARTOWRITE();
61
62
0
  buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_BANNER);
63
0
  buf_putbufstring(ses.writepayload, banner);
64
0
  buf_putstring(ses.writepayload, "en", 2);
65
66
0
  encrypt_packet();
67
68
0
  TRACE(("leave send_msg_userauth_banner"))
69
0
}
70
71
/* handle a userauth request, check validity, pass to password or pubkey
72
 * checking, and handle success or failure */
73
3.48k
void recv_msg_userauth_request() {
74
75
3.48k
  char *username = NULL, *servicename = NULL, *methodname = NULL;
76
3.48k
  unsigned int userlen, servicelen, methodlen;
77
3.48k
  int valid_user = 0;
78
79
3.48k
  TRACE(("enter recv_msg_userauth_request"))
80
81
  /* for compensating failure delay */
82
3.48k
  gettime_wrapper(&ses.authstate.auth_starttime);
83
84
  /* ignore packets if auth is already done */
85
3.48k
  if (ses.authstate.authdone == 1) {
86
56
    TRACE(("leave recv_msg_userauth_request: authdone already"))
87
56
    return;
88
56
  }
89
90
  /* send the banner if it exists, it will only exist once */
91
3.42k
  if (svr_opts.banner) {
92
0
    send_msg_userauth_banner(svr_opts.banner);
93
0
    buf_free(svr_opts.banner);
94
0
    svr_opts.banner = NULL;
95
0
  }
96
97
3.42k
  username = buf_getstring(ses.payload, &userlen);
98
3.42k
  servicename = buf_getstring(ses.payload, &servicelen);
99
3.42k
  methodname = buf_getstring(ses.payload, &methodlen);
100
101
  /* only handle 'ssh-connection' currently */
102
3.42k
  if (servicelen != SSH_SERVICE_CONNECTION_LEN
103
301
      && (strncmp(servicename, SSH_SERVICE_CONNECTION,
104
301
          SSH_SERVICE_CONNECTION_LEN) != 0)) {
105
    
106
    /* TODO - disconnect here */
107
255
    m_free(username);
108
255
    m_free(servicename);
109
255
    m_free(methodname);
110
255
    dropbear_exit("unknown service in auth");
111
255
  }
112
113
  /* check username is good before continuing. 
114
   * the 'incrfail' varies depending on the auth method to
115
   * avoid giving away which users exist on the system through
116
   * the time delay. */
117
3.17k
  if (checkusername(username, userlen) == DROPBEAR_SUCCESS) {
118
179
    valid_user = 1;
119
179
  }
120
121
  /* user wants to know what methods are supported */
122
3.17k
  if (methodlen == AUTH_METHOD_NONE_LEN &&
123
804
      strncmp(methodname, AUTH_METHOD_NONE,
124
804
        AUTH_METHOD_NONE_LEN) == 0) {
125
524
    TRACE(("recv_msg_userauth_request: 'none' request"))
126
524
    if (valid_user
127
0
        && svr_opts.allowblankpass
128
0
        && !svr_opts.noauthpass
129
0
        && !(svr_opts.norootpass && ses.authstate.pw_uid == 0) 
130
0
        && ses.authstate.pw_passwd[0] == '\0') 
131
0
    {
132
0
      dropbear_log(LOG_NOTICE, 
133
0
          "Auth succeeded with blank password for '%s' from %s",
134
0
          ses.authstate.pw_name,
135
0
          svr_ses.addrstring);
136
0
      send_msg_userauth_success();
137
0
      goto out;
138
0
    }
139
524
    else
140
524
    {
141
      /* 'none' has no failure delay */
142
524
      send_msg_userauth_failure(0, 0);
143
524
      goto out;
144
524
    }
145
524
  }
146
  
147
2.64k
#if DROPBEAR_SVR_PASSWORD_AUTH
148
2.64k
  if (!svr_opts.noauthpass &&
149
2.35k
      !(svr_opts.norootpass && ses.authstate.pw_uid == 0) ) {
150
    /* user wants to try password auth */
151
2.35k
    if (methodlen == AUTH_METHOD_PASSWORD_LEN &&
152
354
        strncmp(methodname, AUTH_METHOD_PASSWORD,
153
354
          AUTH_METHOD_PASSWORD_LEN) == 0) {
154
117
      svr_auth_password(valid_user);
155
117
      goto out;
156
117
    }
157
2.35k
  }
158
2.53k
#endif
159
160
#if DROPBEAR_SVR_PAM_AUTH
161
  if (!svr_opts.noauthpass &&
162
      !(svr_opts.norootpass && ses.authstate.pw_uid == 0) ) {
163
    /* user wants to try password auth */
164
    if (methodlen == AUTH_METHOD_PASSWORD_LEN &&
165
        strncmp(methodname, AUTH_METHOD_PASSWORD,
166
          AUTH_METHOD_PASSWORD_LEN) == 0) {
167
      svr_auth_pam(valid_user);
168
      goto out;
169
    }
170
  }
171
#endif
172
173
2.53k
#if DROPBEAR_SVR_PUBKEY_AUTH
174
  /* user wants to try pubkey auth */
175
2.53k
  if (methodlen == AUTH_METHOD_PUBKEY_LEN &&
176
1.33k
      strncmp(methodname, AUTH_METHOD_PUBKEY,
177
1.33k
        AUTH_METHOD_PUBKEY_LEN) == 0) {
178
1.13k
    svr_auth_pubkey(valid_user);
179
1.13k
    goto out;
180
1.13k
  }
181
1.40k
#endif
182
183
  /* nothing matched, we just fail with a delay */
184
1.40k
  send_msg_userauth_failure(0, 1);
185
186
2.73k
out:
187
188
2.73k
  m_free(username);
189
2.73k
  m_free(servicename);
190
2.73k
  m_free(methodname);
191
2.73k
}
192
193
#ifdef HAVE_GETGROUPLIST
194
/* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
195
0
static int check_group_membership(gid_t check_gid, const char* username, gid_t user_gid) {
196
0
  int ngroups, i, ret;
197
0
  gid_t *grouplist = NULL;
198
0
  int match = DROPBEAR_FAILURE;
199
200
0
  for (ngroups = 32; ngroups <= DROPBEAR_NGROUP_MAX; ngroups *= 2) {
201
0
    grouplist = m_malloc(sizeof(gid_t) * ngroups);
202
203
    /* BSD returns ret==0 on success. Linux returns ret==ngroups on success */
204
0
    ret = getgrouplist(username, user_gid, grouplist, &ngroups);
205
0
    if (ret >= 0) {
206
0
      break;
207
0
    }
208
0
    m_free(grouplist);
209
0
    grouplist = NULL;
210
0
  }
211
212
0
  if (!grouplist) {
213
0
    dropbear_log(LOG_ERR, "Too many groups for user '%s'", username);
214
0
    return DROPBEAR_FAILURE;
215
0
  }
216
217
0
  for (i = 0; i < ngroups; i++) {
218
0
    if (grouplist[i] == check_gid) {
219
0
      match = DROPBEAR_SUCCESS;
220
0
      break;
221
0
    }
222
0
  }
223
0
  m_free(grouplist);
224
225
0
  return match;
226
0
}
227
#endif
228
229
/* Check that the username exists and isn't disallowed (root), and has a valid shell.
230
 * returns DROPBEAR_SUCCESS on valid username, DROPBEAR_FAILURE on failure */
231
3.15k
static int checkusername(const char *username, unsigned int userlen) {
232
233
3.15k
  char* listshell = NULL;
234
3.15k
  char* usershell = NULL;
235
3.15k
  uid_t uid;
236
237
3.15k
  TRACE(("enter checkusername"))
238
3.15k
  if (userlen > MAX_USERNAME_LEN) {
239
84
    return DROPBEAR_FAILURE;
240
84
  }
241
242
3.07k
  if (strlen(username) != userlen) {
243
16
    dropbear_exit("Attempted username with a null byte");
244
16
  }
245
246
3.05k
  if (ses.authstate.username == NULL) {
247
    /* first request */
248
995
    fill_passwd(username);
249
995
    ses.authstate.username = m_strdup(username);
250
2.06k
  } else {
251
    /* check username hasn't changed */
252
2.06k
    if (strcmp(username, ses.authstate.username) != 0) {
253
265
      dropbear_exit("Client trying multiple usernames");
254
265
    }
255
2.06k
  }
256
257
  /* avoids cluttering logs with repeated failure messages from
258
  consecutive authentication requests in a sesssion */
259
2.79k
  if (ses.authstate.checkusername_failed) {
260
1.65k
    TRACE(("checkusername: returning cached failure"))
261
1.65k
    return DROPBEAR_FAILURE;
262
1.65k
  }
263
264
  /* check that user exists */
265
1.14k
  if (!ses.authstate.pw_name) {
266
962
    TRACE(("leave checkusername: user '%s' doesn't exist", username))
267
962
    dropbear_log(LOG_WARNING,
268
962
        "Login attempt for nonexistent user from %s",
269
962
        svr_ses.addrstring);
270
962
    ses.authstate.checkusername_failed = 1;
271
962
    return DROPBEAR_FAILURE;
272
962
  }
273
274
  /* check if we are running as non-root, and login user is different from the server */
275
179
  uid = geteuid();
276
179
  if (!(DROPBEAR_SVR_MULTIUSER && uid == 0) && uid != ses.authstate.pw_uid) {
277
0
    TRACE(("running as nonroot, only server uid is allowed"))
278
0
    dropbear_log(LOG_WARNING,
279
0
        "Login attempt with wrong user %s from %s",
280
0
        ses.authstate.pw_name,
281
0
        svr_ses.addrstring);
282
0
    ses.authstate.checkusername_failed = 1;
283
0
    return DROPBEAR_FAILURE;
284
0
  }
285
286
  /* check for non-root if desired */
287
179
  if (svr_opts.norootlogin && ses.authstate.pw_uid == 0) {
288
0
    TRACE(("leave checkusername: root login disabled"))
289
0
    dropbear_log(LOG_WARNING, "root login rejected");
290
0
    ses.authstate.checkusername_failed = 1;
291
0
    return DROPBEAR_FAILURE;
292
0
  }
293
294
  /* check for login restricted to certain group if desired */
295
179
#ifdef HAVE_GETGROUPLIST
296
179
  if (svr_opts.restrict_group) {
297
0
    if (check_group_membership(svr_opts.restrict_group_gid,
298
0
        ses.authstate.pw_name, ses.authstate.pw_gid) == DROPBEAR_FAILURE) {
299
0
      dropbear_log(LOG_WARNING,
300
0
        "Logins are restricted to the group %s but user '%s' is not a member",
301
0
        svr_opts.restrict_group, ses.authstate.pw_name);
302
0
      ses.authstate.checkusername_failed = 1;
303
0
      return DROPBEAR_FAILURE;
304
0
    }
305
0
  }
306
179
#endif /* HAVE_GETGROUPLIST */
307
308
179
  TRACE(("shell is %s", ses.authstate.pw_shell))
309
310
  /* check that the shell is set */
311
179
  usershell = ses.authstate.pw_shell;
312
179
  if (usershell[0] == '\0') {
313
    /* empty shell in /etc/passwd means /bin/sh according to passwd(5) */
314
0
    usershell = "/bin/sh";
315
0
  }
316
317
  /* check the shell is valid. If /etc/shells doesn't exist, getusershell()
318
   * should return some standard shells like "/bin/sh" and "/bin/csh" (this
319
   * is platform-specific) */
320
179
  setusershell();
321
179
  while ((listshell = getusershell()) != NULL) {
322
179
    TRACE(("test shell is '%s'", listshell))
323
179
    if (strcmp(listshell, usershell) == 0) {
324
      /* have a match */
325
179
      goto goodshell;
326
179
    }
327
179
  }
328
  /* no matching shell */
329
0
  endusershell();
330
0
  TRACE(("no matching shell"))
331
0
  ses.authstate.checkusername_failed = 1;
332
0
  dropbear_log(LOG_WARNING, "User '%s' has invalid shell, rejected",
333
0
        ses.authstate.pw_name);
334
0
  return DROPBEAR_FAILURE;
335
  
336
179
goodshell:
337
179
  endusershell();
338
179
  TRACE(("matching shell"))
339
340
179
  TRACE(("uid = %d", ses.authstate.pw_uid))
341
179
  TRACE(("leave checkusername"))
342
179
  return DROPBEAR_SUCCESS;
343
179
}
344
345
/* Send a failure message to the client, in responds to a userauth_request.
346
 * Partial indicates whether to set the "partial success" flag,
347
 * incrfail is whether to count this failure in the failure count (which
348
 * is limited. This function also handles disconnection after too many
349
 * failures */
350
2.74k
void send_msg_userauth_failure(int partial, int incrfail) {
351
352
2.74k
  buffer *typebuf = NULL;
353
354
2.74k
  TRACE(("enter send_msg_userauth_failure"))
355
356
2.74k
  CHECKCLEARTOWRITE();
357
  
358
2.74k
  buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_FAILURE);
359
360
  /* put a list of allowed types */
361
2.74k
  typebuf = buf_new(30); /* long enough for PUBKEY and PASSWORD */
362
363
2.74k
  if (ses.authstate.authtypes & AUTH_TYPE_PUBKEY) {
364
2.74k
    buf_putbytes(typebuf, (const unsigned char *)AUTH_METHOD_PUBKEY, AUTH_METHOD_PUBKEY_LEN);
365
2.74k
    if (ses.authstate.authtypes & AUTH_TYPE_PASSWORD) {
366
2.74k
      buf_putbyte(typebuf, ',');
367
2.74k
    }
368
2.74k
  }
369
  
370
2.74k
  if (ses.authstate.authtypes & AUTH_TYPE_PASSWORD) {
371
2.74k
    buf_putbytes(typebuf, (const unsigned char *)AUTH_METHOD_PASSWORD, AUTH_METHOD_PASSWORD_LEN);
372
2.74k
  }
373
374
2.74k
  buf_putbufstring(ses.writepayload, typebuf);
375
376
2.74k
  TRACE(("auth fail: methods %d, '%.*s'", ses.authstate.authtypes,
377
2.74k
        typebuf->len, typebuf->data))
378
379
2.74k
  buf_free(typebuf);
380
381
2.74k
  buf_putbyte(ses.writepayload, partial ? 1 : 0);
382
2.74k
  encrypt_packet();
383
384
2.74k
  if (incrfail) {
385
    /* The SSH_MSG_AUTH_FAILURE response is delayed to attempt to
386
    avoid user enumeration and slow brute force attempts.
387
    The delay is adjusted by the time already spent in processing
388
    authentication (ses.authstate.auth_starttime timestamp). */
389
390
    /* Desired total delay 300ms +-50ms (in nanoseconds).
391
    Beware of integer overflow if increasing these values */
392
1.21k
    const int mindelay = 250000000;
393
1.21k
    const unsigned int vardelay = 100000000;
394
1.21k
    suseconds_t rand_delay;
395
1.21k
    struct timespec delay;
396
397
1.21k
    gettime_wrapper(&delay);
398
1.21k
    delay.tv_sec -= ses.authstate.auth_starttime.tv_sec;
399
1.21k
    delay.tv_nsec -= ses.authstate.auth_starttime.tv_nsec;
400
401
    /* carry */
402
1.21k
    if (delay.tv_nsec < 0) {
403
0
      delay.tv_nsec += 1000000000;
404
0
      delay.tv_sec -= 1;
405
0
    }
406
407
1.21k
    genrandom((unsigned char*)&rand_delay, sizeof(rand_delay));
408
1.21k
    rand_delay = mindelay + (rand_delay % vardelay);
409
410
1.21k
    if (delay.tv_sec == 0 && delay.tv_nsec <= mindelay) {
411
      /* Compensate for elapsed time */
412
1.21k
      delay.tv_nsec = rand_delay - delay.tv_nsec;
413
1.21k
    } else {
414
      /* No time left or time went backwards, just delay anyway */
415
0
      delay.tv_sec = 0;
416
0
      delay.tv_nsec = rand_delay;
417
0
    }
418
419
420
1.21k
#if DROPBEAR_FUZZ
421
1.21k
    if (!fuzz.fuzzing)
422
0
#endif
423
0
    {
424
0
      while (nanosleep(&delay, &delay) == -1 && errno == EINTR) { /* Go back to sleep */ }
425
0
    }
426
427
1.21k
    ses.authstate.failcount++;
428
1.21k
  }
429
430
2.74k
  if (ses.authstate.failcount >= svr_opts.maxauthtries) {
431
5
    char * userstr;
432
    /* XXX - send disconnect ? */
433
5
    TRACE(("Max auth tries reached, exiting"))
434
435
5
    if (ses.authstate.pw_name == NULL) {
436
3
      userstr = "is invalid";
437
3
    } else {
438
2
      userstr = ses.authstate.pw_name;
439
2
    }
440
5
    dropbear_exit("Max auth tries reached - user '%s'",
441
5
        userstr);
442
5
  }
443
  
444
2.74k
  TRACE(("leave send_msg_userauth_failure"))
445
2.74k
}
446
447
/* Send a success message to the user, and set the "authdone" flag */
448
0
void send_msg_userauth_success() {
449
450
0
  TRACE(("enter send_msg_userauth_success"))
451
452
0
  CHECKCLEARTOWRITE();
453
454
0
  buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_SUCCESS);
455
0
  encrypt_packet();
456
457
  /* authdone must be set after encrypt_packet() for 
458
   * delayed-zlib mode */
459
0
  ses.authstate.authdone = 1;
460
0
  ses.connect_time = 0;
461
462
463
0
  if (ses.authstate.pw_uid == 0) {
464
0
    ses.allowprivport = 1;
465
0
  }
466
467
  /* Remove from the list of pre-auth sockets. Should be m_close(), since if
468
   * we fail, we might end up leaking connection slots, and disallow new
469
   * logins - a nasty situation. */              
470
0
  m_close(svr_ses.childpipe);
471
472
0
  TRACE(("leave send_msg_userauth_success"))
473
474
0
}