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
314#define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \314#define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \
315do { \315do { \
316 type *fmts; \316 type *fmts; \
317 void *oldf = *f; \
318 \317 \
319 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \318 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \
320 return AVERROR(ENOMEM); \319 return AVERROR(ENOMEM); \
@@ -324,8 +323,6 @@ do { \ · 3 unmodified lines
324 sizeof(*(*f)->list)); \323 sizeof(*(*f)->list)); \
325 if (!fmts) { \324 if (!fmts) { \
326 unref_fn(f); \325 unref_fn(f); \
327 if (!oldf) \
328 av_freep(f); \
329 return AVERROR(ENOMEM); \326 return AVERROR(ENOMEM); \
330 } \327 } \
331 \328 \
@@ -486,16 +483,17 @@ do { \ · 154 unmodified lines
486do { \483do { \
487 int idx = -1; \484 int idx = -1; \
488 \485 \
489 if (!ref || !*ref || !(*ref)->refs) \486 if (!ref || !*ref) \
490 return; \487 return; \
491 \488 \
492 FIND_REF_INDEX(ref, idx); \489 FIND_REF_INDEX(ref, idx); \
493 \490 \
494 if (idx >= 0) \491 if (idx >= 0) { \
495 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \492 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
496 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \493 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
497 \494 --(*ref)->refcount; \
498 if(!--(*ref)->refcount) { \495 } \
496 if (!(*ref)->refcount) { \
499 av_free((*ref)->list); \497 av_free((*ref)->list); \
500 av_free((*ref)->refs); \498 av_free((*ref)->refs); \
501 av_free(*ref); \499 av_free(*ref); \
@@ -537,7 +535,7 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref) · 35 unmodified lines
537 FORMATS_CHANGEREF(oldref, newref);535 FORMATS_CHANGEREF(oldref, newref);
538}536}
539 537
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) \
541 int count = 0, i; \539 int count = 0, i; \
542 \540 \
543 if (!fmts) \541 if (!fmts) \
@@ -547,10 +545,6 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref) · 3 unmodified lines
547 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \545 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
548 int ret = ref_fn(fmts, &ctx->inputs[i]->out_fmts); \546 int ret = ref_fn(fmts, &ctx->inputs[i]->out_fmts); \
549 if (ret < 0) { \547 if (ret < 0) { \
550 unref_fn(&fmts); \
551 if (fmts) \
552 av_freep(&fmts->list); \
553 av_freep(&fmts); \
554 return ret; \548 return ret; \
555 } \549 } \
556 count++; \550 count++; \
@@ -560,10 +554,6 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref) · 3 unmodified lines
560 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \554 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
561 int ret = ref_fn(fmts, &ctx->outputs[i]->in_fmts); \555 int ret = ref_fn(fmts, &ctx->outputs[i]->in_fmts); \
562 if (ret < 0) { \556 if (ret < 0) { \
563 unref_fn(&fmts); \
564 if (fmts) \
565 av_freep(&fmts->list); \
566 av_freep(&fmts); \
567 return ret; \557 return ret; \
568 } \558 } \
569 count++; \559 count++; \
@@ -571,9 +561,7 @@ void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref) · 1 unmodified line
571 } \561 } \
572 \562 \
573 if (!count) { \563 if (!count) { \
574 av_freep(&fmts->list); \564 unref_fn(&fmts); \
575 av_freep(&fmts->refs); \
576 av_freep(&fmts); \
577 } \565 } \
578 \566 \
579 return 0;567 return 0;
@@ -582,14 +570,14 @@ int ff_set_common_channel_layouts(AVFilterContext *ctx, · 2 unmodified lines
582 AVFilterChannelLayouts *layouts)570 AVFilterChannelLayouts *layouts)
583{571{
584 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,572 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);
586}574}
587 575
588int ff_set_common_samplerates(AVFilterContext *ctx,576int ff_set_common_samplerates(AVFilterContext *ctx,
589 AVFilterFormats *samplerates)577 AVFilterFormats *samplerates)
590{578{
591 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,579 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
592 ff_formats_ref, ff_formats_unref, formats);580 ff_formats_ref, ff_formats_unref);
593}581}
594 582
595/**583/**
@@ -600,7 +588,7 @@ int ff_set_common_samplerates(AVFilterContext *ctx, · 4 unmodified lines
600int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)588int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
601{589{
602 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,590 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
603 ff_formats_ref, ff_formats_unref, formats);591 ff_formats_ref, ff_formats_unref);
604}592}
605 593
606int ff_default_query_formats(AVFilterContext *ctx)594int 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