summaryrefslogtreecommitdiff
path: root/recipes-multimedia/lame/lame/CVE-2017-13712.patch
blob: 2c2c4d5da2d3ebd7f6d50cd8c47b48dd7becc899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
Upstream-Status: Backport [http://lame.cvs.sourceforge.net/viewvc/lame/lame/libmp3lame/id3tag.c?r1=1.79&r2=1.80]

Backport patch to fix CVE-2017-13712 for lame.

Signed-off-by: Kai Kang <kai.kang@windriver.com>
Signed-off-by: Stefan Agner <stefan.agner@toradex.com>
---
--- a/libmp3lame/id3tag.c	(revision 6426)
+++ b/libmp3lame/id3tag.c	(working copy)
@@ -195,8 +195,12 @@
 }
 #endif
 
+static int
+is_lame_internal_flags_null(lame_t gfp)
+{
+    return (gfp && gfp->internal_flags) ? 0 : 1;
+}
 
-
 static int
 id3v2_add_ucs2(lame_t gfp, uint32_t frame_id, char const *lang, unsigned short const *desc, unsigned short const *text);
 static int
@@ -238,8 +242,7 @@
 static void
 id3v2AddAudioDuration(lame_t gfp, double ms)
 {
-    lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0;
-    SessionConfig_t const *const cfg = &gfc->cfg;
+    SessionConfig_t const *const cfg = &gfp->internal_flags->cfg; /* caller checked pointers */
     char    buffer[1024];
     double const max_ulong = MAX_U_32_NUM;
     unsigned long playlength_ms;
@@ -280,7 +283,12 @@
 void
 id3tag_init(lame_t gfp)
 {
-    lame_internal_flags *gfc = gfp->internal_flags;
+    lame_internal_flags *gfc = 0;
+
+    if (is_lame_internal_flags_null(gfp)) {
+        return;
+    }
+    gfc = gfp->internal_flags;
     free_id3tag(gfc);
     memset(&gfc->tag_spec, 0, sizeof gfc->tag_spec);
     gfc->tag_spec.genre_id3v1 = GENRE_NUM_UNKNOWN;
@@ -293,7 +301,12 @@
 void
 id3tag_add_v2(lame_t gfp)
 {
-    lame_internal_flags *gfc = gfp->internal_flags;
+    lame_internal_flags *gfc = 0;
+
+    if (is_lame_internal_flags_null(gfp)) {
+        return;
+    }
+    gfc = gfp->internal_flags;
     gfc->tag_spec.flags &= ~V1_ONLY_FLAG;
     gfc->tag_spec.flags |= ADD_V2_FLAG;
 }
@@ -301,7 +314,12 @@
 void
 id3tag_v1_only(lame_t gfp)
 {
-    lame_internal_flags *gfc = gfp->internal_flags;
+    lame_internal_flags *gfc = 0;
+
+    if (is_lame_internal_flags_null(gfp)) {
+        return;
+    }
+    gfc = gfp->internal_flags;
     gfc->tag_spec.flags &= ~(ADD_V2_FLAG | V2_ONLY_FLAG);
     gfc->tag_spec.flags |= V1_ONLY_FLAG;
 }
@@ -309,7 +327,12 @@
 void
 id3tag_v2_only(lame_t gfp)
 {
-    lame_internal_flags *gfc = gfp->internal_flags;
+    lame_internal_flags *gfc = 0;
+
+    if (is_lame_internal_flags_null(gfp)) {
+        return;
+    }
+    gfc = gfp->internal_flags;
     gfc->tag_spec.flags &= ~V1_ONLY_FLAG;
     gfc->tag_spec.flags |= V2_ONLY_FLAG;
 }
@@ -317,7 +340,12 @@
 void
 id3tag_space_v1(lame_t gfp)
 {
-    lame_internal_flags *gfc = gfp->internal_flags;
+    lame_internal_flags *gfc = 0;
+
+    if (is_lame_internal_flags_null(gfp)) {
+        return;
+    }
+    gfc = gfp->internal_flags;
     gfc->tag_spec.flags &= ~V2_ONLY_FLAG;
     gfc->tag_spec.flags |= SPACE_V1_FLAG;
 }
@@ -331,7 +359,12 @@
 void
 id3tag_set_pad(lame_t gfp, size_t n)
 {
-    lame_internal_flags *gfc = gfp->internal_flags;
+    lame_internal_flags *gfc = 0;
+
+    if (is_lame_internal_flags_null(gfp)) {
+        return;
+    }
+    gfc = gfp->internal_flags;
     gfc->tag_spec.flags &= ~V1_ONLY_FLAG;
     gfc->tag_spec.flags |= PAD_V2_FLAG;
     gfc->tag_spec.flags |= ADD_V2_FLAG;
@@ -583,23 +616,30 @@
 int
 id3tag_set_albumart(lame_t gfp, const char *image, size_t size)
 {
-    int     mimetype = 0;
-    unsigned char const *data = (unsigned char const *) image;
-    lame_internal_flags *gfc = gfp->internal_flags;
+    int     mimetype = MIMETYPE_NONE;
+    lame_internal_flags *gfc = 0;
 
-    /* determine MIME type from the actual image data */
-    if (2 < size && data[0] == 0xFF && data[1] == 0xD8) {
-        mimetype = MIMETYPE_JPEG;
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
     }
-    else if (4 < size && data[0] == 0x89 && strncmp((const char *) &data[1], "PNG", 3) == 0) {
-        mimetype = MIMETYPE_PNG;
+    gfc = gfp->internal_flags;
+
+    if (image != 0) {
+        unsigned char const *data = (unsigned char const *) image;
+        /* determine MIME type from the actual image data */
+        if (2 < size && data[0] == 0xFF && data[1] == 0xD8) {
+            mimetype = MIMETYPE_JPEG;
+        }
+        else if (4 < size && data[0] == 0x89 && strncmp((const char *) &data[1], "PNG", 3) == 0) {
+            mimetype = MIMETYPE_PNG;
+        }
+        else if (4 < size && strncmp((const char *) data, "GIF8", 4) == 0) {
+            mimetype = MIMETYPE_GIF;
+        }
+        else {
+            return -1;
+        }
     }
-    else if (4 < size && strncmp((const char *) data, "GIF8", 4) == 0) {
-        mimetype = MIMETYPE_GIF;
-    }
-    else {
-        return -1;
-    }
     if (gfc->tag_spec.albumart != 0) {
         free(gfc->tag_spec.albumart);
         gfc->tag_spec.albumart = 0;
@@ -606,7 +646,7 @@
         gfc->tag_spec.albumart_size = 0;
         gfc->tag_spec.albumart_mimetype = MIMETYPE_NONE;
     }
-    if (size < 1) {
+    if (size < 1 || mimetype == MIMETYPE_NONE) {
         return 0;
     }
     gfc->tag_spec.albumart = lame_calloc(unsigned char, size);
@@ -934,6 +974,9 @@
     if (frame_id == 0) {
         return -1;
     }
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
     if (text == 0) {
         return 0;
     }
@@ -983,6 +1026,9 @@
     if (frame_id == 0) {
         return -1;
     }
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
     if (text == 0) {
         return 0;
     }
@@ -1012,6 +1058,9 @@
 int
 id3tag_set_comment_latin1(lame_t gfp, char const *lang, char const *desc, char const *text)
 {
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
     return id3v2_add_latin1(gfp, ID_COMMENT, lang, desc, text);
 }
 
@@ -1019,6 +1068,9 @@
 int
 id3tag_set_comment_utf16(lame_t gfp, char const *lang, unsigned short const *desc, unsigned short const *text)
 {
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
     return id3v2_add_ucs2(gfp, ID_COMMENT, lang, desc, text);
 }
 
@@ -1029,6 +1081,9 @@
 int
 id3tag_set_comment_ucs2(lame_t gfp, char const *lang, unsigned short const *desc, unsigned short const *text)
 {
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
     return id3tag_set_comment_utf16(gfp, lang, desc, text);
 }
 
@@ -1219,9 +1274,9 @@
 int
 id3tag_set_genre(lame_t gfp, const char *genre)
 {
-    lame_internal_flags *gfc = gfp->internal_flags;
+    lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0;
     int     ret = 0;
-    if (genre && *genre) {
+    if (gfc && genre && *genre) {
         int const num = lookupGenre(genre);
         if (num == -1) return num;
         gfc->tag_spec.flags |= CHANGED_FLAG;
@@ -1514,6 +1569,9 @@
 int
 id3tag_set_fieldvalue(lame_t gfp, const char *fieldvalue)
 {
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
     if (fieldvalue && *fieldvalue) {
         if (strlen(fieldvalue) < 5 || fieldvalue[4] != '=') {
             return -1;
@@ -1526,6 +1584,9 @@
 int
 id3tag_set_fieldvalue_utf16(lame_t gfp, const unsigned short *fieldvalue)
 {
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
     if (fieldvalue && *fieldvalue) {
         size_t dx = hasUcs2ByteOrderMarker(fieldvalue[0]);
         unsigned short const separator = fromLatin1Char(fieldvalue, '=');
@@ -1556,6 +1617,9 @@
 int
 id3tag_set_fieldvalue_ucs2(lame_t gfp, const unsigned short *fieldvalue)
 {
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
     return id3tag_set_fieldvalue_utf16(gfp, fieldvalue);
 }
 
@@ -1562,14 +1626,12 @@
 size_t
 lame_get_id3v2_tag(lame_t gfp, unsigned char *buffer, size_t size)
 {
-    lame_internal_flags *gfc;
-    if (gfp == 0) {
+    lame_internal_flags *gfc = 0;
+
+    if (is_lame_internal_flags_null(gfp)) {
         return 0;
     }
     gfc = gfp->internal_flags;
-    if (gfc == 0) {
-        return 0;
-    }
     if (test_tag_spec_flags(gfc, V1_ONLY_FLAG)) {
         return 0;
     }
@@ -1711,7 +1773,12 @@
 int
 id3tag_write_v2(lame_t gfp)
 {
-    lame_internal_flags *gfc = gfp->internal_flags;
+    lame_internal_flags *gfc = 0;
+
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
+    gfc = gfp->internal_flags;
 #if 0
     debug_tag_spec_flags(gfc, "write v2");
 #endif
@@ -1812,10 +1879,15 @@
 int
 id3tag_write_v1(lame_t gfp)
 {
-    lame_internal_flags *const gfc = gfp->internal_flags;
+    lame_internal_flags* gfc = 0;
     size_t  i, n, m;
     unsigned char tag[128];
 
+    if (is_lame_internal_flags_null(gfp)) {
+        return 0;
+    }
+    gfc = gfp->internal_flags;
+
     m = sizeof(tag);
     n = lame_get_id3v1_tag(gfp, tag, m);
     if (n > m) {