~ubuntu-branches/ubuntu/quantal/libssh/quantal-updates

« back to all changes in this revision

Viewing changes to .pc/CVE-2012-4562.patch/src/string.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-11-22 13:34:54 UTC
  • Revision ID: package-import@ubuntu.com-20121122133454-dx5fq7yrdhqpv7ad
Tags: 0.5.2-1ubuntu0.12.10.1
* SECURITY UPDATE: denial of service and possible code execution via
  multiple double free flaws
  - debian/patches/CVE-2012-4559.patch: properly do frees in src/agent.c,
    src/channels.c, src/sftp.c.
  - CVE-2012-4559
* SECURITY UPDATE: denial of service and possible code execution via
  multiple buffer overflows
  - debian/patches/CVE-2012-4560.patch: properly calculate sizes in
    src/misc.c.
  - CVE-2012-4560
* SECURITY UPDATE: denial of service and possible code execution via
  multiple invalid free flaws
  - debian/patches/CVE-2012-4561.patch: don't use after free in
    src/keyfiles.c, properly zero structs in src/keys.c.
  - CVE-2012-4561
* SECURITY UPDATE: denial of service and possible code execution via
  multiple improper overflow checks
  - debian/patches/CVE-2012-4562.patch: do proper overflow checks in
    src/buffer.c, src/dh.c, src/string.c.
  - CVE-2012-4562

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * string.c - ssh string functions
 
3
 *
 
4
 * This file is part of the SSH Library
 
5
 *
 
6
 * Copyright (c) 2003-2008 by Aris Adamantiadis
 
7
 *
 
8
 * The SSH Library is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU Lesser General Public License as published by
 
10
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 
11
 * option) any later version.
 
12
 *
 
13
 * The SSH Library is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
15
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
16
 * License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public License
 
19
 * along with the SSH Library; see the file COPYING.  If not, write to
 
20
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
21
 * MA 02111-1307, USA.
 
22
 */
 
23
 
 
24
#include <errno.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
 
 
28
#ifndef _WIN32
 
29
#include <arpa/inet.h>
 
30
#endif
 
31
 
 
32
#include "libssh/priv.h"
 
33
#include "libssh/string.h"
 
34
 
 
35
/**
 
36
 * @defgroup libssh_string The SSH string functions
 
37
 * @ingroup libssh
 
38
 *
 
39
 * @brief String manipulations used in libssh.
 
40
 *
 
41
 * @{
 
42
 */
 
43
 
 
44
/**
 
45
 * @brief Create a new SSH String object.
 
46
 *
 
47
 * @param[in] size       The size of the string.
 
48
 *
 
49
 * @return               The newly allocated string, NULL on error.
 
50
 */
 
51
struct ssh_string_struct *ssh_string_new(size_t size) {
 
52
  struct ssh_string_struct *str = NULL;
 
53
 
 
54
  str = malloc(size + 4);
 
55
  if (str == NULL) {
 
56
    return NULL;
 
57
  }
 
58
 
 
59
  str->size = htonl(size);
 
60
  return str;
 
61
}
 
62
 
 
63
/**
 
64
 * @brief Fill a string with given data. The string should be big enough.
 
65
 *
 
66
 * @param s        An allocated string to fill with data.
 
67
 *
 
68
 * @param data     The data to fill the string with.
 
69
 *
 
70
 * @param len      Size of data.
 
71
 *
 
72
 * @return         0 on success, < 0 on error.
 
73
 */
 
74
int ssh_string_fill(struct ssh_string_struct *s, const void *data, size_t len) {
 
75
  if ((s == NULL) || (data == NULL) ||
 
76
      (len == 0) || (len > s->size)) {
 
77
    return -1;
 
78
  }
 
79
 
 
80
  memcpy(s->string, data, len);
 
81
  return 0;
 
82
}
 
83
 
 
84
/**
 
85
 * @brief Create a ssh string using a C string
 
86
 *
 
87
 * @param[in] what      The source 0-terminated C string.
 
88
 *
 
89
 * @return              The newly allocated string, NULL on error with errno
 
90
 *                      set.
 
91
 *
 
92
 * @note The nul byte is not copied nor counted in the ouput string.
 
93
 */
 
94
struct ssh_string_struct *ssh_string_from_char(const char *what) {
 
95
  struct ssh_string_struct *ptr;
 
96
  size_t len;
 
97
 
 
98
  if(what == NULL) {
 
99
      errno = EINVAL;
 
100
      return NULL;
 
101
  }
 
102
 
 
103
  len = strlen(what);
 
104
 
 
105
  ptr = malloc(4 + len);
 
106
  if (ptr == NULL) {
 
107
    return NULL;
 
108
  }
 
109
  ptr->size = htonl(len);
 
110
  memcpy(ptr->string, what, len);
 
111
 
 
112
  return ptr;
 
113
}
 
114
 
 
115
/**
 
116
 * @brief Return the size of a SSH string.
 
117
 *
 
118
 * @param[in] s         The the input SSH string.
 
119
 *
 
120
 * @return The size of the content of the string, 0 on error.
 
121
 */
 
122
size_t ssh_string_len(struct ssh_string_struct *s) {
 
123
  if (s == NULL) {
 
124
    return ntohl(0);
 
125
  }
 
126
 
 
127
  return ntohl(s->size);
 
128
}
 
129
 
 
130
/**
 
131
 * @brief Convert a SSH string to a C nul-terminated string.
 
132
 *
 
133
 * @param[in] s         The SSH input string.
 
134
 *
 
135
 * @return              An allocated string pointer, NULL on error with errno
 
136
 *                      set.
 
137
 *
 
138
 * @note If the input SSH string contains zeroes, some parts of the output
 
139
 * string may not be readable with regular libc functions.
 
140
 */
 
141
char *ssh_string_to_char(struct ssh_string_struct *s) {
 
142
        size_t len;
 
143
        char *new;
 
144
        if(s==NULL || s->string == NULL)
 
145
                return NULL;
 
146
  len = ntohl(s->size) + 1;
 
147
  new = malloc(len);
 
148
 
 
149
  if (new == NULL) {
 
150
    return NULL;
 
151
  }
 
152
  memcpy(new, s->string, len - 1);
 
153
  new[len - 1] = '\0';
 
154
  return new;
 
155
}
 
156
 
 
157
/**
 
158
 * @brief Deallocate a char string object.
 
159
 *
 
160
 * @param[in] s         The string to delete.
 
161
 */
 
162
void ssh_string_free_char(char *s) {
 
163
    SAFE_FREE(s);
 
164
}
 
165
 
 
166
/**
 
167
 * @brief Copy a string, return a newly allocated string. The caller has to
 
168
 *        free the string.
 
169
 *
 
170
 * @param[in] s         String to copy.
 
171
 *
 
172
 * @return              Newly allocated copy of the string, NULL on error.
 
173
 */
 
174
struct ssh_string_struct *ssh_string_copy(struct ssh_string_struct *s) {
 
175
  struct ssh_string_struct *new;
 
176
  
 
177
  if(s == NULL || s->string == NULL) {
 
178
      return NULL;
 
179
  }
 
180
  new = malloc(ntohl(s->size) + 4);
 
181
 
 
182
  if (new == NULL) {
 
183
    return NULL;
 
184
  }
 
185
  new->size = s->size;
 
186
  memcpy(new->string, s->string, ntohl(s->size));
 
187
 
 
188
  return new;
 
189
}
 
190
 
 
191
/**
 
192
 * @brief Destroy the data in a string so it couldn't appear in a core dump.
 
193
 *
 
194
 * @param[in] s         The string to burn.
 
195
 */
 
196
void ssh_string_burn(struct ssh_string_struct *s) {
 
197
  if (s == NULL) {
 
198
    return;
 
199
  }
 
200
  memset(s->string, 'X', ssh_string_len(s));
 
201
}
 
202
 
 
203
/**
 
204
 * @brief Get the payload of the string.
 
205
 *
 
206
 * @param s             The string to get the data from.
 
207
 *
 
208
 * @return              Return the data of the string or NULL on error.
 
209
 */
 
210
void *ssh_string_data(struct ssh_string_struct *s) {
 
211
  if (s == NULL) {
 
212
    return NULL;
 
213
  }
 
214
 
 
215
  return s->string;
 
216
}
 
217
 
 
218
/**
 
219
 * @brief Deallocate a SSH string object.
 
220
 *
 
221
 * \param[in] s         The SSH string to delete.
 
222
 */
 
223
void ssh_string_free(struct ssh_string_struct *s) {
 
224
  SAFE_FREE(s);
 
225
}
 
226
 
 
227
/** @} */
 
228
 
 
229
/* vim: set ts=4 sw=4 et cindent: */