History

avfilter/formats: Fix double frees and memleaks on error

The formats API deals with lists of channel layouts, sample rates, pixel formats and sample formats. These lists are refcounted in a way in which the list structure itself contains pointers to all of its owners. Furthermore, it is possible for a list to be not owned by anyone yet; this status is temporary until the list has been attached to an owner. Adding an owner to a list involves reallocating the list's list of owners and can therefore fail. In order to reduce the amount of checks and cleanup code for the users of this API, the API is supposed to be lenient when faced with input lists that are NULL and it is supposed to clean up if adding an owner to a list fails, so that a simple use case like list = ff_make_format_list(foo_fmts); if ((ret = ff_formats_ref(list, &ctx->inputs[0]->out_formats)) < 0) return ret; needn't check whether list could be successfully allocated (ff_formats_ref() return AVERROR(ENOMEM) if it couldn't) and it also needn't free list if ff_formats_ref() couldn't add an owner for it. But the cleaning up after itself was broken. The root cause was that the refcount was decremented during unreferencing whether or not the element to be unreferenced was actually an owner of the list or not. This means that if the above sample code is continued by if ((ret = ff_formats_ref(list, &ctx->inputs[1]->out_formats)) < 0) return ret; and that if an error happens at the second ff_formats_ref() call, the automatic cleaning of list will decrement the refcount from 1 (the sole owner of list at this moment is ctx->input[0]->out_formats) to 0 and so the list will be freed; yet ctx->input[0]->out_formats still points to the list and this will lead to a double free/use-after-free when ctx->input[0] is freed later. Presumably in order to work around such an issue, commit 93afb338a405eac0f9e7b092bc26603378bfcca6 restricted unreferencing to lists with owners. This does not solve the root cause (the above example is not fixed by this) at all, but it solves some crashs. This commit fixes the API: The list's refcount is only decremented if an owner is removed from the list of owners and not if the unref-function is called with a pointer that is not among the owners of the list. Furtermore, the requirement for the list to have owners is dropped. This implies that if the first call to ff_formats_ref() in the above example fails, the refcount which is initially zero during unreferencing is not modified, so that the list will be freed automatically in said call to ff_formats_ref() as every list whose refcount reaches zero is. If on the other hand, the second call to ff_formats_ref() is the first to fail, the refcount would stay at one during the automatic unreferencing in ff_formats_ref(). The list would later be freed when its last (and in this case sole) owner (namely ctx->inputs[0]->out_formats) gets unreferenced. The issues described here for ff_formats_ref() also affected the other functions of this API. E.g. ff_add_format() failed to clean up after itself if adding an entry to an already existing list failed (the case of a freshly allocated list was handled specially and this commit also removes said code). E.g. ff_all_formats() inherited the flaw.

Andreas Rheinhardt committed 6 years ago

Commit 2a471af1 parent 44bcd6f1 file · +10 −22Browse files

Unified · Split

libavfilter/formats.c+10−22
@@ -314,7 +314,6 @@ AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts) · 313 unmodified lines
314314 #define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \
315315 do { \
316316 type *fmts; \
317− void *oldf = *f; \
318317 \
319318 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \
320319 return AVERROR(ENOMEM); \
@@ -324,8 +323,6 @@ do { \ · 3 unmodified lines
324323 sizeof(*(*f)->list)); \
325324 if (!fmts) { \
326325 unref_fn(f); \
327− if (!oldf) \
328− av_freep(f); \
329326 return AVERROR(ENOMEM); \
330327 } \
331328 \
@@ -486,16 +483,17 @@ do { \ · 154 unmodified lines
486483 do { \
487484 int idx = -1; \
488485 \
489− if (!ref || !*ref || !(*ref)->refs) \
486+ if (!ref || !*ref) \
490487 return; \
491488 \
492489 FIND_REF_INDEX(ref, idx); \
493490 \
494− if (idx >= 0) \
491+ if (idx >= 0) { \
495492 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
496493 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
497− \
498− if(!--(*ref)->refcount) { \
494+ --(*ref)->refcount; \
495+ } \
496+ if (!(*ref)->refcount) { \
499497 av_free((*ref)->list); \
500498 av_free((*ref)->refs); \
501499 av_free(*ref); \
@@ -537,7 +535,7 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref) · 35 unmodified lines
537535 FORMATS_CHANGEREF(oldref, newref);
538536 }
539537
540−#define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref_fn, unref_fn, list) \
538+#define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref_fn, unref_fn) \
541539 int count = 0, i; \
542540 \
543541 if (!fmts) \
@@ -547,10 +545,6 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref) · 3 unmodified lines
547545 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
548546 int ret = ref_fn(fmts, &ctx->inputs[i]->out_fmts); \
549547 if (ret < 0) { \
550− unref_fn(&fmts); \
551− if (fmts) \
552− av_freep(&fmts->list); \
553− av_freep(&fmts); \
554548 return ret; \
555549 } \
556550 count++; \
@@ -560,10 +554,6 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref) · 3 unmodified lines
560554 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
561555 int ret = ref_fn(fmts, &ctx->outputs[i]->in_fmts); \
562556 if (ret < 0) { \
563− unref_fn(&fmts); \
564− if (fmts) \
565− av_freep(&fmts->list); \
566− av_freep(&fmts); \
567557 return ret; \
568558 } \
569559 count++; \
@@ -571,9 +561,7 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref) · 1 unmodified line
571561 } \
572562 \
573563 if (!count) { \
574− av_freep(&fmts->list); \
575− av_freep(&fmts->refs); \
576− av_freep(&fmts); \
564+ unref_fn(&fmts); \
577565 } \
578566 \
579567 return 0;
@@ -582,14 +570,14 @@ int ff_set_common_channel_layouts(AVFilterContext *ctx, · 2 unmodified lines
582570 AVFilterChannelLayouts *layouts)
583571 {
584572 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
585− ff_channel_layouts_ref, ff_channel_layouts_unref, channel_layouts);
573+ ff_channel_layouts_ref, ff_channel_layouts_unref);
586574 }
587575
588576 int ff_set_common_samplerates(AVFilterContext *ctx,
589577 AVFilterFormats *samplerates)
590578 {
591579 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
592− ff_formats_ref, ff_formats_unref, formats);
580+ ff_formats_ref, ff_formats_unref);
593581 }
594582
595583 /**
@@ -600,7 +588,7 @@ int ff_set_common_samplerates(AVFilterContext *ctx, · 4 unmodified lines
600588 int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
601589 {
602590 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
603− ff_formats_ref, ff_formats_unref, formats);
591+ ff_formats_ref, ff_formats_unref);
604592 }
605593
606594 int ff_default_query_formats(AVFilterContext *ctx)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t