Coverage Report

Created: 2025-11-20 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/src/buffer.c
Line
Count
Source
1
/*
2
 * Dropbear SSH
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
/* Buffer handling routines, designed to avoid overflows/using invalid data */
26
27
#include "includes.h"
28
#include "dbutil.h"
29
#include "buffer.h"
30
31
/* Prevent integer overflows when incrementing buffer position/length.
32
 * Calling functions should check arguments first, but this provides a
33
 * backstop */
34
41.3k
#define BUF_MAX_INCR 1000000000
35
2.11k
#define BUF_MAX_SIZE 1000000000
36
37
/* avoid excessively large numbers, > ~8192 bits */
38
684
#define BUF_MAX_MPINT (8240 / 8)
39
40
/* Create (malloc) a new buffer of size */
41
2.11k
buffer* buf_new(unsigned int size) {
42
2.11k
  buffer* buf;
43
2.11k
  if (size > BUF_MAX_SIZE) {
44
0
    dropbear_exit("buf->size too big");
45
0
  }
46
47
2.11k
  buf = (buffer*)m_malloc(sizeof(buffer)+size);
48
2.11k
  buf->data = (unsigned char*)buf + sizeof(buffer);
49
2.11k
  buf->size = size;
50
2.11k
  return buf;
51
2.11k
}
52
53
/* free the buffer's data and the buffer itself */
54
1.55k
void buf_free(buffer* buf) {
55
1.55k
  m_free(buf);
56
1.55k
}
57
58
/* overwrite the contents of the buffer then free it */
59
518
void buf_burn_free(buffer* buf) {
60
518
  m_burn(buf->data, buf->size);
61
518
  m_free(buf);
62
518
}
63
64
65
/* resize a buffer, pos and len will be repositioned if required when
66
 * downsizing */
67
0
buffer* buf_resize(buffer *buf, unsigned int newsize) {
68
0
  if (newsize > BUF_MAX_SIZE) {
69
0
    dropbear_exit("buf->size too big");
70
0
  }
71
72
0
  buf = m_realloc(buf, sizeof(buffer)+newsize);
73
0
  buf->data = (unsigned char*)buf + sizeof(buffer);
74
0
  buf->size = newsize;
75
0
  buf->len = MIN(newsize, buf->len);
76
0
  buf->pos = MIN(newsize, buf->pos);
77
0
  return buf;
78
0
}
79
80
/* Create a copy of buf, allocating required memory etc. */
81
/* The new buffer is sized the same as the length of the source buffer. */
82
518
buffer* buf_newcopy(const buffer* buf) {
83
  
84
518
  buffer* ret;
85
86
518
  ret = buf_new(buf->len);
87
518
  ret->len = buf->len;
88
518
  if (buf->len > 0) {
89
518
    memcpy(ret->data, buf->data, buf->len);
90
518
  }
91
518
  return ret;
92
518
}
93
94
/* Set the length of the buffer */
95
526
void buf_setlen(buffer* buf, unsigned int len) {
96
526
  if (len > buf->size) {
97
0
    dropbear_exit("Bad buf_setlen");
98
0
  }
99
526
  buf->len = len;
100
526
  buf->pos = MIN(buf->pos, buf->len);
101
526
}
102
103
/* Increment the length of the buffer */
104
553
void buf_incrlen(buffer* buf, unsigned int incr) {
105
553
  if (incr > BUF_MAX_INCR || buf->len + incr > buf->size) {
106
0
    dropbear_exit("Bad buf_incrlen");
107
0
  }
108
553
  buf->len += incr;
109
553
}
110
/* Set the position of the buffer */
111
530
void buf_setpos(buffer* buf, unsigned int pos) {
112
113
530
  if (pos > buf->len) {
114
0
    dropbear_exit("Bad buf_setpos");
115
0
  }
116
530
  buf->pos = pos;
117
530
}
118
119
/* increment the position by incr, increasing the buffer length if required */
120
7.26k
void buf_incrwritepos(buffer* buf, unsigned int incr) {
121
7.26k
  if (incr > BUF_MAX_INCR || buf->pos + incr > buf->size) {
122
0
    dropbear_exit("Bad buf_incrwritepos");
123
0
  }
124
7.26k
  buf->pos += incr;
125
7.26k
  if (buf->pos > buf->len) {
126
7.26k
    buf->len = buf->pos;
127
7.26k
  }
128
7.26k
}
129
130
/* increment the position by incr */
131
1.97k
void buf_incrpos(buffer* buf, unsigned int incr) {
132
1.97k
  if (incr > BUF_MAX_INCR 
133
1.97k
    || (buf->pos + incr) > buf->len) {
134
0
    dropbear_exit("Bad buf_incrpos");
135
0
  }
136
1.97k
  buf->pos += incr;
137
1.97k
}
138
139
/* decrement the position by decr */
140
8
void buf_decrpos(buffer* buf, unsigned int decr) {
141
8
  if (decr > buf->pos) {
142
0
    dropbear_exit("Bad buf_decrpos");
143
0
  }
144
8
  buf->pos -= decr;
145
8
}
146
147
/* Get a byte from the buffer and increment the pos */
148
2
unsigned char buf_getbyte(buffer* buf) {
149
150
  /* This check is really just ==, but the >= allows us to check for the
151
   * bad case of pos > len, which should _never_ happen. */
152
2
  if (buf->pos >= buf->len) {
153
0
    dropbear_exit("Bad buf_getbyte");
154
0
  }
155
2
  return buf->data[buf->pos++];
156
2
}
157
158
/* Get a bool from the buffer and increment the pos */
159
0
unsigned char buf_getbool(buffer* buf) {
160
161
0
  unsigned char b;
162
0
  b = buf_getbyte(buf);
163
0
  if (b != 0)
164
0
    b = 1;
165
0
  return b;
166
0
}
167
168
/* put a byte, incrementing the length if required */
169
551
void buf_putbyte(buffer* buf, unsigned char val) {
170
171
551
  if (buf->pos >= buf->len) {
172
551
    buf_incrlen(buf, 1);
173
551
  }
174
551
  buf->data[buf->pos] = val;
175
551
  buf->pos++;
176
551
}
177
178
/* returns an in-place pointer to the buffer, checking that
179
 * the next len bytes from that position can be used */
180
3.13k
unsigned char* buf_getptr(const buffer* buf, unsigned int len) {
181
182
3.13k
  if (len > BUF_MAX_INCR || buf->pos + len > buf->len) {
183
39
    dropbear_exit("Bad buf_getptr");
184
39
  }
185
3.09k
  return &buf->data[buf->pos];
186
3.13k
}
187
188
/* like buf_getptr, but checks against total size, not used length.
189
 * This allows writing past the used length, but not past the size */
190
7.78k
unsigned char* buf_getwriteptr(const buffer* buf, unsigned int len) {
191
192
7.78k
  if (len > BUF_MAX_INCR || buf->pos + len > buf->size) {
193
0
    dropbear_exit("Bad buf_getwriteptr");
194
0
  }
195
7.78k
  return &buf->data[buf->pos];
196
7.78k
}
197
198
/* Return a null-terminated string, it is malloced, so must be free()ed
199
 * Note that the string isn't checked for null bytes, hence the retlen
200
 * may be longer than what is returned by strlen */
201
12
char* buf_getstring(buffer* buf, unsigned int *retlen) {
202
203
12
  unsigned int len;
204
12
  char* ret;
205
12
  void* src = NULL;
206
12
  len = buf_getint(buf);
207
12
  if (len > MAX_STRING_LEN) {
208
0
    dropbear_exit("String too long");
209
0
  }
210
211
12
  if (retlen != NULL) {
212
12
    *retlen = len;
213
12
  }
214
12
  src = buf_getptr(buf, len);
215
12
  ret = m_malloc(len+1);
216
12
  memcpy(ret, src, len);
217
12
  buf_incrpos(buf, len);
218
12
  ret[len] = '\0';
219
220
12
  return ret;
221
12
}
222
223
/* Return a string as a newly allocated buffer */
224
2
static buffer * buf_getstringbuf_int(buffer *buf, int incllen) {
225
2
  buffer *ret = NULL;
226
2
  unsigned int len = buf_getint(buf);
227
2
  int extra = 0;
228
2
  if (len > MAX_STRING_LEN) {
229
0
    dropbear_exit("String too long");
230
0
  }
231
2
  if (incllen) {
232
0
    extra = 4;
233
0
  }
234
2
  ret = buf_new(len+extra);
235
2
  if (incllen) {
236
0
    buf_putint(ret, len);
237
0
  }
238
2
  memcpy(buf_getwriteptr(ret, len), buf_getptr(buf, len), len);
239
2
  buf_incrpos(buf, len);
240
2
  buf_incrlen(ret, len);
241
2
  buf_setpos(ret, 0);
242
2
  return ret;
243
2
}
244
245
/* Return a string as a newly allocated buffer */
246
2
buffer * buf_getstringbuf(buffer *buf) {
247
2
  return buf_getstringbuf_int(buf, 0);
248
2
}
249
250
/* Returns a string in a new buffer, including the length */
251
0
buffer * buf_getbuf(buffer *buf) {
252
0
  return buf_getstringbuf_int(buf, 1);
253
0
}
254
255
/* Returns the equivalent of buf_getptr() as a new buffer. */
256
0
buffer * buf_getptrcopy(const buffer* buf, unsigned int len) {
257
0
  unsigned char *src = buf_getptr(buf, len);
258
0
  buffer *ret = buf_new(len);
259
0
  buf_putbytes(ret, src, len);
260
0
  buf_setpos(ret, 0);
261
0
  return ret;
262
0
}
263
264
/* Just increment the buffer position the same as if we'd used buf_getstring,
265
 * but don't bother copying/malloc()ing for it */
266
0
void buf_eatstring(buffer *buf) {
267
268
0
  buf_incrpos( buf, buf_getint(buf) );
269
0
}
270
271
/* Get an uint32 from the buffer and increment the pos */
272
1.37k
unsigned int buf_getint(buffer* buf) {
273
1.37k
  unsigned int ret;
274
275
1.37k
  LOAD32H(ret, buf_getptr(buf, 4));
276
1.37k
  buf_incrpos(buf, 4);
277
1.37k
  return ret;
278
1.37k
}
279
280
/* put a 32bit uint into the buffer, incr bufferlen & pos if required */
281
3.62k
void buf_putint(buffer* buf, int unsigned val) {
282
283
3.62k
  STORE32H(val, buf_getwriteptr(buf, 4));
284
3.62k
  buf_incrwritepos(buf, 4);
285
286
3.62k
}
287
288
/* put a SSH style string into the buffer, increasing buffer len if required */
289
1.55k
void buf_putstring(buffer* buf, const char* str, unsigned int len) {
290
  
291
1.55k
  buf_putint(buf, len);
292
1.55k
  buf_putbytes(buf, (const unsigned char*)str, len);
293
294
1.55k
}
295
296
/* puts an entire buffer as a SSH string. ignore pos of buf_str. */
297
518
void buf_putbufstring(buffer *buf, const buffer* buf_str) {
298
518
  buf_putstring(buf, (const char*)buf_str->data, buf_str->len);
299
518
}
300
301
/* put the set of len bytes into the buffer, incrementing the pos, increasing
302
 * len if required */
303
1.56k
void buf_putbytes(buffer *buf, const unsigned char *bytes, unsigned int len) {
304
1.56k
  memcpy(buf_getwriteptr(buf, len), bytes, len);
305
1.56k
  buf_incrwritepos(buf, len);
306
1.56k
}
307
  
308
309
/* for our purposes we only need positive (or 0) numbers, so will
310
 * fail if we get negative numbers */
311
1.55k
void buf_putmpint(buffer* buf, const mp_int * mp) {
312
1.55k
  size_t written;
313
1.55k
  unsigned int len, pad = 0;
314
1.55k
  TRACE2(("enter buf_putmpint"))
315
316
1.55k
  dropbear_assert(mp != NULL);
317
318
1.55k
  if (mp_isneg(mp)) {
319
0
    dropbear_exit("negative bignum");
320
0
  }
321
322
  /* zero check */
323
1.55k
  if (mp_iszero(mp)) {
324
0
    len = 0;
325
1.55k
  } else {
326
    /* SSH spec requires padding for mpints with the MSB set, this code
327
     * implements it */
328
1.55k
    len = mp_count_bits(mp);
329
    /* if the top bit of MSB is set, we need to pad */
330
1.55k
    pad = (len%8 == 0) ? 1 : 0;
331
1.55k
    len = len / 8 + 1; /* don't worry about rounding, we need it for
332
                padding anyway when len%8 == 0 */
333
334
1.55k
  }
335
336
  /* store the length */
337
1.55k
  buf_putint(buf, len);
338
  
339
  /* store the actual value */
340
1.55k
  if (len > 0) {
341
1.55k
    if (pad) {
342
551
      buf_putbyte(buf, 0x00);
343
551
    }
344
1.55k
    if (mp_to_ubin(mp, buf_getwriteptr(buf, len-pad), len-pad, &written) != MP_OKAY) {
345
0
      dropbear_exit("mpint error");
346
0
    }
347
1.55k
    buf_incrwritepos(buf, written);
348
1.55k
  }
349
350
1.55k
  TRACE2(("leave buf_putmpint"))
351
1.55k
}
352
353
/* Retrieve an mp_int from the buffer.
354
 * Will fail for -ve since they shouldn't be required here.
355
 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
356
688
int buf_getmpint(buffer* buf, mp_int* mp) {
357
358
688
  unsigned int len;
359
688
  len = buf_getint(buf);
360
  
361
688
  if (len == 0) {
362
4
    mp_zero(mp);
363
4
    return DROPBEAR_SUCCESS;
364
4
  }
365
366
684
  if (len > BUF_MAX_MPINT) {
367
62
    return DROPBEAR_FAILURE;
368
62
  }
369
370
  /* check for negative */
371
622
  if (*buf_getptr(buf, 1) & (1 << (CHAR_BIT-1))) {
372
8
    return DROPBEAR_FAILURE;
373
8
  }
374
375
614
  if (mp_from_ubin(mp, buf_getptr(buf, len), len) != MP_OKAY) {
376
0
    return DROPBEAR_FAILURE;
377
0
  }
378
379
614
  buf_incrpos(buf, len);
380
614
  return DROPBEAR_SUCCESS;
381
614
}