Coverage Report

Created: 2026-01-05 07:13

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
11.7M
#define BUF_MAX_INCR 1000000000
35
173k
#define BUF_MAX_SIZE 1000000000
36
37
/* avoid excessively large numbers, > ~8192 bits */
38
5.92k
#define BUF_MAX_MPINT (8240 / 8)
39
40
/* Create (malloc) a new buffer of size */
41
164k
buffer* buf_new(unsigned int size) {
42
164k
  buffer* buf;
43
164k
  if (size > BUF_MAX_SIZE) {
44
0
    dropbear_exit("buf->size too big");
45
0
  }
46
47
164k
  buf = (buffer*)m_malloc(sizeof(buffer)+size);
48
164k
  buf->data = (unsigned char*)buf + sizeof(buffer);
49
164k
  buf->size = size;
50
164k
  return buf;
51
164k
}
52
53
/* free the buffer's data and the buffer itself */
54
129k
void buf_free(buffer* buf) {
55
129k
  m_free(buf);
56
129k
}
57
58
/* overwrite the contents of the buffer then free it */
59
32.8k
void buf_burn_free(buffer* buf) {
60
32.8k
  m_burn(buf->data, buf->size);
61
32.8k
  m_free(buf);
62
32.8k
}
63
64
65
/* resize a buffer, pos and len will be repositioned if required when
66
 * downsizing */
67
9.10k
buffer* buf_resize(buffer *buf, unsigned int newsize) {
68
9.10k
  if (newsize > BUF_MAX_SIZE) {
69
0
    dropbear_exit("buf->size too big");
70
0
  }
71
72
9.10k
  buf = m_realloc(buf, sizeof(buffer)+newsize);
73
9.10k
  buf->data = (unsigned char*)buf + sizeof(buffer);
74
9.10k
  buf->size = newsize;
75
9.10k
  buf->len = MIN(newsize, buf->len);
76
9.10k
  buf->pos = MIN(newsize, buf->pos);
77
9.10k
  return buf;
78
9.10k
}
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
10.9k
buffer* buf_newcopy(const buffer* buf) {
83
  
84
10.9k
  buffer* ret;
85
86
10.9k
  ret = buf_new(buf->len);
87
10.9k
  ret->len = buf->len;
88
10.9k
  if (buf->len > 0) {
89
10.9k
    memcpy(ret->data, buf->data, buf->len);
90
10.9k
  }
91
10.9k
  return ret;
92
10.9k
}
93
94
/* Set the length of the buffer */
95
183k
void buf_setlen(buffer* buf, unsigned int len) {
96
183k
  if (len > buf->size) {
97
0
    dropbear_exit("Bad buf_setlen");
98
0
  }
99
183k
  buf->len = len;
100
183k
  buf->pos = MIN(buf->pos, buf->len);
101
183k
}
102
103
/* Increment the length of the buffer */
104
1.87M
void buf_incrlen(buffer* buf, unsigned int incr) {
105
1.87M
  if (incr > BUF_MAX_INCR || buf->len + incr > buf->size) {
106
0
    dropbear_exit("Bad buf_incrlen");
107
0
  }
108
1.87M
  buf->len += incr;
109
1.87M
}
110
/* Set the position of the buffer */
111
752k
void buf_setpos(buffer* buf, unsigned int pos) {
112
113
752k
  if (pos > buf->len) {
114
0
    dropbear_exit("Bad buf_setpos");
115
0
  }
116
752k
  buf->pos = pos;
117
752k
}
118
119
/* increment the position by incr, increasing the buffer length if required */
120
1.07M
void buf_incrwritepos(buffer* buf, unsigned int incr) {
121
1.07M
  if (incr > BUF_MAX_INCR || buf->pos + incr > buf->size) {
122
0
    dropbear_exit("Bad buf_incrwritepos");
123
0
  }
124
1.07M
  buf->pos += incr;
125
1.07M
  if (buf->pos > buf->len) {
126
852k
    buf->len = buf->pos;
127
852k
  }
128
1.07M
}
129
130
/* increment the position by incr */
131
788k
void buf_incrpos(buffer* buf, unsigned int incr) {
132
788k
  if (incr > BUF_MAX_INCR 
133
788k
    || (buf->pos + incr) > buf->len) {
134
118
    dropbear_exit("Bad buf_incrpos");
135
118
  }
136
788k
  buf->pos += incr;
137
788k
}
138
139
/* decrement the position by decr */
140
4
void buf_decrpos(buffer* buf, unsigned int decr) {
141
4
  if (decr > buf->pos) {
142
0
    dropbear_exit("Bad buf_decrpos");
143
0
  }
144
4
  buf->pos -= decr;
145
4
}
146
147
/* Get a byte from the buffer and increment the pos */
148
141k
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
141k
  if (buf->pos >= buf->len) {
153
4
    dropbear_exit("Bad buf_getbyte");
154
4
  }
155
141k
  return buf->data[buf->pos++];
156
141k
}
157
158
/* Get a bool from the buffer and increment the pos */
159
8.48k
unsigned char buf_getbool(buffer* buf) {
160
161
8.48k
  unsigned char b;
162
8.48k
  b = buf_getbyte(buf);
163
8.48k
  if (b != 0)
164
395
    b = 1;
165
8.48k
  return b;
166
8.48k
}
167
168
/* put a byte, incrementing the length if required */
169
1.86M
void buf_putbyte(buffer* buf, unsigned char val) {
170
171
1.86M
  if (buf->pos >= buf->len) {
172
1.82M
    buf_incrlen(buf, 1);
173
1.82M
  }
174
1.86M
  buf->data[buf->pos] = val;
175
1.86M
  buf->pos++;
176
1.86M
}
177
178
/* returns an in-place pointer to the buffer, checking that
179
 * the next len bytes from that position can be used */
180
1.00M
unsigned char* buf_getptr(const buffer* buf, unsigned int len) {
181
182
1.00M
  if (len > BUF_MAX_INCR || buf->pos + len > buf->len) {
183
181
    dropbear_exit("Bad buf_getptr");
184
181
  }
185
1.00M
  return &buf->data[buf->pos];
186
1.00M
}
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
1.14M
unsigned char* buf_getwriteptr(const buffer* buf, unsigned int len) {
191
192
1.14M
  if (len > BUF_MAX_INCR || buf->pos + len > buf->size) {
193
0
    dropbear_exit("Bad buf_getwriteptr");
194
0
  }
195
1.14M
  return &buf->data[buf->pos];
196
1.14M
}
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
83.9k
char* buf_getstring(buffer* buf, unsigned int *retlen) {
202
203
83.9k
  unsigned int len;
204
83.9k
  char* ret;
205
83.9k
  void* src = NULL;
206
83.9k
  len = buf_getint(buf);
207
83.9k
  if (len > MAX_STRING_LEN) {
208
71
    dropbear_exit("String too long");
209
71
  }
210
211
83.9k
  if (retlen != NULL) {
212
83.8k
    *retlen = len;
213
83.8k
  }
214
83.9k
  src = buf_getptr(buf, len);
215
83.9k
  ret = m_malloc(len+1);
216
83.9k
  memcpy(ret, src, len);
217
83.9k
  buf_incrpos(buf, len);
218
83.9k
  ret[len] = '\0';
219
220
83.9k
  return ret;
221
83.9k
}
222
223
/* Return a string as a newly allocated buffer */
224
1.91k
static buffer * buf_getstringbuf_int(buffer *buf, int incllen) {
225
1.91k
  buffer *ret = NULL;
226
1.91k
  unsigned int len = buf_getint(buf);
227
1.91k
  int extra = 0;
228
1.91k
  if (len > MAX_STRING_LEN) {
229
34
    dropbear_exit("String too long");
230
34
  }
231
1.87k
  if (incllen) {
232
0
    extra = 4;
233
0
  }
234
1.87k
  ret = buf_new(len+extra);
235
1.87k
  if (incllen) {
236
0
    buf_putint(ret, len);
237
0
  }
238
1.87k
  memcpy(buf_getwriteptr(ret, len), buf_getptr(buf, len), len);
239
1.87k
  buf_incrpos(buf, len);
240
1.87k
  buf_incrlen(ret, len);
241
1.87k
  buf_setpos(ret, 0);
242
1.87k
  return ret;
243
1.91k
}
244
245
/* Return a string as a newly allocated buffer */
246
1.91k
buffer * buf_getstringbuf(buffer *buf) {
247
1.91k
  return buf_getstringbuf_int(buf, 0);
248
1.91k
}
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
16.0k
void buf_eatstring(buffer *buf) {
267
268
16.0k
  buf_incrpos( buf, buf_getint(buf) );
269
16.0k
}
270
271
/* Get an uint32 from the buffer and increment the pos */
272
161k
unsigned int buf_getint(buffer* buf) {
273
161k
  unsigned int ret;
274
275
161k
  LOAD32H(ret, buf_getptr(buf, 4));
276
161k
  buf_incrpos(buf, 4);
277
161k
  return ret;
278
161k
}
279
280
/* put a 32bit uint into the buffer, incr bufferlen & pos if required */
281
386k
void buf_putint(buffer* buf, int unsigned val) {
282
283
386k
  STORE32H(val, buf_getwriteptr(buf, 4));
284
386k
  buf_incrwritepos(buf, 4);
285
286
386k
}
287
288
/* put a SSH style string into the buffer, increasing buffer len if required */
289
100k
void buf_putstring(buffer* buf, const char* str, unsigned int len) {
290
  
291
100k
  buf_putint(buf, len);
292
100k
  buf_putbytes(buf, (const unsigned char*)str, len);
293
294
100k
}
295
296
/* puts an entire buffer as a SSH string. ignore pos of buf_str. */
297
23.1k
void buf_putbufstring(buffer *buf, const buffer* buf_str) {
298
23.1k
  buf_putstring(buf, (const char*)buf_str->data, buf_str->len);
299
23.1k
}
300
301
/* put the set of len bytes into the buffer, incrementing the pos, increasing
302
 * len if required */
303
362k
void buf_putbytes(buffer *buf, const unsigned char *bytes, unsigned int len) {
304
362k
  memcpy(buf_getwriteptr(buf, len), bytes, len);
305
362k
  buf_incrwritepos(buf, len);
306
362k
}
307
  
308
309
/* for our purposes we only need positive (or 0) numbers, so will
310
 * fail if we get negative numbers */
311
61.5k
void buf_putmpint(buffer* buf, const mp_int * mp) {
312
61.5k
  size_t written;
313
61.5k
  unsigned int len, pad = 0;
314
61.5k
  TRACE2(("enter buf_putmpint"))
315
316
61.5k
  dropbear_assert(mp != NULL);
317
318
61.5k
  if (mp_isneg(mp)) {
319
0
    dropbear_exit("negative bignum");
320
0
  }
321
322
  /* zero check */
323
61.5k
  if (mp_iszero(mp)) {
324
0
    len = 0;
325
61.5k
  } else {
326
    /* SSH spec requires padding for mpints with the MSB set, this code
327
     * implements it */
328
61.5k
    len = mp_count_bits(mp);
329
    /* if the top bit of MSB is set, we need to pad */
330
61.5k
    pad = (len%8 == 0) ? 1 : 0;
331
61.5k
    len = len / 8 + 1; /* don't worry about rounding, we need it for
332
                padding anyway when len%8 == 0 */
333
334
61.5k
  }
335
336
  /* store the length */
337
61.5k
  buf_putint(buf, len);
338
  
339
  /* store the actual value */
340
61.5k
  if (len > 0) {
341
61.5k
    if (pad) {
342
28.7k
      buf_putbyte(buf, 0x00);
343
28.7k
    }
344
61.5k
    if (mp_to_ubin(mp, buf_getwriteptr(buf, len-pad), len-pad, &written) != MP_OKAY) {
345
0
      dropbear_exit("mpint error");
346
0
    }
347
61.5k
    buf_incrwritepos(buf, written);
348
61.5k
  }
349
350
61.5k
  TRACE2(("leave buf_putmpint"))
351
61.5k
}
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
5.93k
int buf_getmpint(buffer* buf, mp_int* mp) {
357
358
5.93k
  unsigned int len;
359
5.93k
  len = buf_getint(buf);
360
  
361
5.93k
  if (len == 0) {
362
4
    mp_zero(mp);
363
4
    return DROPBEAR_SUCCESS;
364
4
  }
365
366
5.92k
  if (len > BUF_MAX_MPINT) {
367
49
    return DROPBEAR_FAILURE;
368
49
  }
369
370
  /* check for negative */
371
5.87k
  if (*buf_getptr(buf, 1) & (1 << (CHAR_BIT-1))) {
372
12
    return DROPBEAR_FAILURE;
373
12
  }
374
375
5.86k
  if (mp_from_ubin(mp, buf_getptr(buf, len), len) != MP_OKAY) {
376
0
    return DROPBEAR_FAILURE;
377
0
  }
378
379
5.86k
  buf_incrpos(buf, len);
380
5.86k
  return DROPBEAR_SUCCESS;
381
5.86k
}