The mcompand option parser counts comma fields literally but uses
av_strtok(), which skips empty fields. A trailing empty field makes the
attack/decay loop call sscanf() with a NULL token and ignore its return
value, leading to a NULL dereference or silent misconfiguration. Check
the tokenizer result and the sscanf() return value at every parse site
and return AVERROR(EINVAL) for invalid input.

Signed-off-by: Mohammed Mustafa <[email protected]>
---
 libavfilter/Makefile                |  1 +
 libavfilter/af_mcompand.c           | 30 ++++++++++++++++++++++------
 libavfilter/tests/mcompand_sscanf.c | 31 +++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 6 deletions(-)
 create mode 100644 libavfilter/tests/mcompand_sscanf.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 8f3c64459c..6bcaebf8c9 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -692,6 +692,7 @@ SKIPHEADERS-$(CONFIG_SCALE_CUDA_FILTER)      +=
vf_scale_cuda.h
 TOOLS     = graph2dot
 TESTPROGS = drawutils filtfmts formats integral

+TESTPROGS-$(CONFIG_MCOMPAND_FILTER) += mcompand_sscanf
 TESTPROGS-$(CONFIG_AMIX_FILTER)   += amix
 TESTPROGS-$(CONFIG_DRAWVG_FILTER) += drawvg

diff --git a/libavfilter/af_mcompand.c b/libavfilter/af_mcompand.c
index 855d893550..61b023b429 100644
--- a/libavfilter/af_mcompand.c
+++ b/libavfilter/af_mcompand.c
@@ -361,9 +361,15 @@ static int config_output(AVFilterLink *outlink)
             char *tstr3 = av_strtok(p3, ",", &saveptr3);

             p3 = NULL;
-            sscanf(tstr3, "%lf", &s->bands[i].attack_rate[k]);
+            if (!tstr3 || sscanf(tstr3, "%lf",
&s->bands[i].attack_rate[k]) != 1) {
+                av_log(ctx, AV_LOG_ERROR, "invalid attack rate in
band %d\n", i);
+                return AVERROR(EINVAL);
+            }
             tstr3 = av_strtok(p3, ",", &saveptr3);
-            sscanf(tstr3, "%lf", &s->bands[i].decay_rate[k]);
+            if (!tstr3 || sscanf(tstr3, "%lf",
&s->bands[i].decay_rate[k]) != 1) {
+                av_log(ctx, AV_LOG_ERROR, "invalid decay rate in band
%d\n", i);
+                return AVERROR(EINVAL);
+            }

             if (s->bands[i].attack_rate[k] > 1.0 / outlink->sample_rate) {
                 s->bands[i].attack_rate[k] = 1.0 - exp(-1.0 /
(outlink->sample_rate * s->bands[i].attack_rate[k]));
@@ -388,7 +394,10 @@ static int config_output(AVFilterLink *outlink)
             av_log(ctx, AV_LOG_ERROR, "transfer function curve in dB
must be set\n");
             return AVERROR(EINVAL);
         }
-        sscanf(tstr2, "%lf", &s->bands[i].transfer_fn.curve_dB);
+        if (sscanf(tstr2, "%lf", &s->bands[i].transfer_fn.curve_dB) != 1) {
+            av_log(ctx, AV_LOG_ERROR, "invalid transfer function
curve in band %d\n", i);
+            return AVERROR(EINVAL);
+        }

         radius = s->bands[i].transfer_fn.curve_dB * M_LN10 / 20.0;

@@ -431,14 +440,20 @@ static int config_output(AVFilterLink *outlink)

         tstr2 = av_strtok(p2, " ", &saveptr2);
         if (tstr2) {
-            sscanf(tstr2, "%lf", &s->bands[i].delay);
+            if (sscanf(tstr2, "%lf", &s->bands[i].delay) != 1) {
+                av_log(ctx, AV_LOG_ERROR, "invalid delay in band %d\n", i);
+                return AVERROR(EINVAL);
+            }
             max_delay_size = FFMAX(max_delay_size, s->bands[i].delay
* outlink->sample_rate);

             tstr2 = av_strtok(p2, " ", &saveptr2);
             if (tstr2) {
                 double initial_volume;

-                sscanf(tstr2, "%lf", &initial_volume);
+                if (sscanf(tstr2, "%lf", &initial_volume) != 1) {
+                    av_log(ctx, AV_LOG_ERROR, "invalid initial volume
in band %d\n", i);
+                    return AVERROR(EINVAL);
+                }
                 initial_volume = pow(10.0, initial_volume / 20);

                 for (k = 0; k < outlink->ch_layout.nb_channels; k++) {
@@ -447,7 +462,10 @@ static int config_output(AVFilterLink *outlink)

                 tstr2 = av_strtok(p2, " ", &saveptr2);
                 if (tstr2) {
-                    sscanf(tstr2, "%lf", &s->bands[i].transfer_fn.gain_dB);
+                    if (sscanf(tstr2, "%lf",
&s->bands[i].transfer_fn.gain_dB) != 1) {
+                        av_log(ctx, AV_LOG_ERROR, "invalid gain in
band %d\n", i);
+                        return AVERROR(EINVAL);
+                    }
                 }
             }
         }
diff --git a/libavfilter/tests/mcompand_sscanf.c
b/libavfilter/tests/mcompand_sscanf.c
new file mode 100644
index 0000000000..8a3e581a5d
--- /dev/null
+++ b/libavfilter/tests/mcompand_sscanf.c
@@ -0,0 +1,31 @@
+/*
+ * Regression test for unchecked sscanf()/NULL tokenizer results in
+ * the mcompand filter option parser.
+ */
+
+#include "libavfilter/avfilter.h"
+
+int main(void)
+{
+    AVFilterGraph *graph = avfilter_graph_alloc();
+    AVFilterInOut *inputs = NULL, *outputs = NULL;
+    const char *args = "anullsrc,mcompand=0.1\\,0.2\\,0.3\\, 6 "
+                       "-47/-40\\,-34/-34\\,-17/-33 100,anull";
+    int ret;
+
+    if (!graph)
+        return 1;
+
+    ret = avfilter_graph_parse_ptr(graph, args, &inputs, &outputs, NULL);
+    if (ret >= 0)
+        ret = avfilter_graph_config(graph, NULL);
+
+    av_log(NULL, AV_LOG_INFO, "mcompand malformed option: config
returned %d\n", ret);
+
+    avfilter_inout_free(&inputs);
+    avfilter_inout_free(&outputs);
+    avfilter_graph_free(&graph);
+
+    /* A malformed option must be rejected, not crash the process. */
+    return ret < 0 ? 0 : 1;
+}
-- 
2.53.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to