[PATCH 2/3] avpacket: Removing a memset that could cause double free
Removing a memset from copy_side_data which could potentially cause double free. Signed-off-by: Vignesh Venkatasubramanian <vigneshv@google.com> --- libavcodec/avpacket.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 7196c31..ac761f1 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -225,8 +225,6 @@ int av_copy_packet_side_data(AVPacket *pkt, AVPacket *src) int i; DUP_DATA(pkt->side_data, src->side_data, src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC); - memset(pkt->side_data, 0, - src->side_data_elems * sizeof(*src->side_data)); for (i = 0; i < src->side_data_elems; i++) { DUP_DATA(pkt->side_data[i].data, src->side_data[i].data, src->side_data[i].size, 1, ALLOC_MALLOC); -- 1.8.4
Adds the end trimming value (duration to be trimmed from the end of the file due to padding) to the packet's side data. This is then made use by the muxer to put the value in the container. Signed-off-by: Vignesh Venkatasubramanian <vigneshv@google.com> --- libavcodec/libopusenc.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c index 59caac7..6b22b75 100644 --- a/libavcodec/libopusenc.c +++ b/libavcodec/libopusenc.c @@ -313,6 +313,7 @@ static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, av_get_bytes_per_sample(avctx->sample_fmt); uint8_t *audio; int ret; + int discard_padding; if (frame) { ff_af_queue_add(&opus->afq, frame); @@ -354,6 +355,25 @@ static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, ff_af_queue_remove(&opus->afq, opus->opts.packet_size, &avpkt->pts, &avpkt->duration); + discard_padding = opus->opts.packet_size - avpkt->duration; + // Check if subtraction resulted in an overflow + if ((discard_padding < opus->opts.packet_size) != (avpkt->duration > 0)) { + av_free_packet(avpkt); + av_free(avpkt); + return AVERROR(EINVAL); + } + if (discard_padding > 0) { + uint8_t* side_data = av_packet_new_side_data(avpkt, + AV_PKT_DATA_SKIP_SAMPLES, + 10); + if(side_data == NULL) { + av_free_packet(avpkt); + av_free(avpkt); + return AVERROR(ENOMEM); + } + AV_WL32(side_data + 4, discard_padding); + } + *got_packet_ptr = 1; return 0; -- 1.8.4
On Fri, Aug 30, 2013 at 11:26 AM, Vignesh Venkatasubramanian <vigneshv@google.com> wrote:
Adds the end trimming value (duration to be trimmed from the end of the file due to padding) to the packet's side data. This is then made use by the muxer to put the value in the container.
Signed-off-by: Vignesh Venkatasubramanian <vigneshv@google.com> --- libavcodec/libopusenc.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c index 59caac7..6b22b75 100644 --- a/libavcodec/libopusenc.c +++ b/libavcodec/libopusenc.c @@ -313,6 +313,7 @@ static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, av_get_bytes_per_sample(avctx->sample_fmt); uint8_t *audio; int ret; + int discard_padding;
if (frame) { ff_af_queue_add(&opus->afq, frame); @@ -354,6 +355,25 @@ static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, ff_af_queue_remove(&opus->afq, opus->opts.packet_size, &avpkt->pts, &avpkt->duration);
+ discard_padding = opus->opts.packet_size - avpkt->duration; + // Check if subtraction resulted in an overflow + if ((discard_padding < opus->opts.packet_size) != (avpkt->duration > 0)) { + av_free_packet(avpkt); + av_free(avpkt); + return AVERROR(EINVAL); + } + if (discard_padding > 0) { + uint8_t* side_data = av_packet_new_side_data(avpkt, + AV_PKT_DATA_SKIP_SAMPLES, + 10); + if(side_data == NULL) { + av_free_packet(avpkt); + av_free(avpkt); + return AVERROR(ENOMEM); + } + AV_WL32(side_data + 4, discard_padding); + } + *got_packet_ptr = 1;
return 0; -- 1.8.4
Michael, could you please look into this too? This is the only patch left out for Opus Matroska functionality. Thanks!
On 10/09/13 6:41 PM, Vignesh Venkatasubramanian wrote:
Michael, could you please look into this too? This is the only patch left out for Opus Matroska functionality. Thanks!
Now that the implementation is complete, could you send a patch to make the muxer write files without the "/EXPERIMENTAL" suffix? It's a matter of moving the "A_OPUS" entry in matroska.c above the experimental to give it priority. Regards.
On 11/09/13 5:02 AM, James Almer wrote:
Now that the implementation is complete, could you send a patch to make the muxer write files without the "/EXPERIMENTAL" suffix? It's a matter of moving the "A_OPUS" entry in matroska.c above the experimental to give it priority.
Regards.
And an entry in Changelog about Opus muxing support in Matroska would be a good idea as well.
On Wed, Sep 11, 2013 at 1:09 AM, James Almer <jamrial@gmail.com> wrote:
On 11/09/13 5:02 AM, James Almer wrote:
Now that the implementation is complete, could you send a patch to make the muxer write files without the "/EXPERIMENTAL" suffix? It's a matter of moving the "A_OPUS" entry in matroska.c above the experimental to give it priority.
Regards.
And an entry in Changelog about Opus muxing support in Matroska would be a good idea as well.
Yes, I meant to do that. Will send patches shortly. I also have a local patch which implements End Trimming for Opus in Ogg container too. I will try to refactor and send that in too.
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Fri, Aug 30, 2013 at 11:26:53AM -0700, Vignesh Venkatasubramanian wrote:
Adds the end trimming value (duration to be trimmed from the end of the file due to padding) to the packet's side data. This is then made use by the muxer to put the value in the container.
applied thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I am the wisest man alive, for I know one thing, and that is that I know nothing. -- Socrates
On Fri, Aug 30, 2013 at 11:26 AM, Vignesh Venkatasubramanian <vigneshv@google.com> wrote:
Removing a memset from copy_side_data which could potentially cause double free.
Signed-off-by: Vignesh Venkatasubramanian <vigneshv@google.com> --- libavcodec/avpacket.c | 2 -- 1 file changed, 2 deletions(-)
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 7196c31..ac761f1 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -225,8 +225,6 @@ int av_copy_packet_side_data(AVPacket *pkt, AVPacket *src) int i; DUP_DATA(pkt->side_data, src->side_data, src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC); - memset(pkt->side_data, 0, - src->side_data_elems * sizeof(*src->side_data)); for (i = 0; i < src->side_data_elems; i++) { DUP_DATA(pkt->side_data[i].data, src->side_data[i].data, src->side_data[i].size, 1, ALLOC_MALLOC); -- 1.8.4
I think git send-email named the subjects as 2/3 and 3/3 because of the patch file names being that way. But there are only two patches in this set. Also, please note that all 4 patches (related to muxer/encoder) should be applied together. So please wait until all of them are reviewed and approved. Thanks.
On Fri, Aug 30, 2013 at 11:26:52AM -0700, Vignesh Venkatasubramanian wrote:
Removing a memset from copy_side_data which could potentially cause double free.
this is a misunderstanding, this patch causes the problems, it does not fix them you can just force a error path and remove the memset to see this in make fate (needs latest git head as there where other bugs when the error paths where used that i just fixed) [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB If you think the mosad wants you dead since a long time then you are either wrong or dead since a long time.
On Fri, Aug 30, 2013 at 12:49 PM, Michael Niedermayer <michaelni@gmx.at> wrote:
On Fri, Aug 30, 2013 at 11:26:52AM -0700, Vignesh Venkatasubramanian wrote:
Removing a memset from copy_side_data which could potentially cause double free.
this is a misunderstanding, this patch causes the problems, it does not fix them you can just force a error path and remove the memset to see this in make fate (needs latest git head as there where other bugs when the error paths where used that i just fixed)
ok, any idea as to how i can fix this to get it working to do what i want with the side data copying?
[...]
-- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
If you think the mosad wants you dead since a long time then you are either wrong or dead since a long time.
On Fri, Aug 30, 2013 at 03:28:45PM -0700, Vignesh Venkatasubramanian wrote:
On Fri, Aug 30, 2013 at 12:49 PM, Michael Niedermayer <michaelni@gmx.at> wrote:
On Fri, Aug 30, 2013 at 11:26:52AM -0700, Vignesh Venkatasubramanian wrote:
Removing a memset from copy_side_data which could potentially cause double free.
this is a misunderstanding, this patch causes the problems, it does not fix them you can just force a error path and remove the memset to see this in make fate (needs latest git head as there where other bugs when the error paths where used that i just fixed)
ok, any idea as to how i can fix this to get it working to do what i want with the side data copying?
if i undestand the issue correctly puting the memset under if(src != dst) might work [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Everything should be made as simple as possible, but not simpler. -- Albert Einstein
On Sat, Aug 31, 2013 at 03:54:31AM +0200, Michael Niedermayer wrote:
On Fri, Aug 30, 2013 at 03:28:45PM -0700, Vignesh Venkatasubramanian wrote:
On Fri, Aug 30, 2013 at 12:49 PM, Michael Niedermayer <michaelni@gmx.at> wrote:
On Fri, Aug 30, 2013 at 11:26:52AM -0700, Vignesh Venkatasubramanian wrote:
Removing a memset from copy_side_data which could potentially cause double free.
this is a misunderstanding, this patch causes the problems, it does not fix them you can just force a error path and remove the memset to see this in make fate (needs latest git head as there where other bugs when the error paths where used that i just fixed)
ok, any idea as to how i can fix this to get it working to do what i want with the side data copying?
if i undestand the issue correctly puting the memset under if(src != dst) might work
but make sure that the error cases work without leaks and double frees [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Asymptotically faster algorithms should always be preferred if you have asymptotical amounts of data
Fixing av_packet_copy_side_data to work correctly when source and destination are the same. This makes sure that there is no memory leak and double frees. Signed-off by: Vignesh Venkatasubramanian <vigneshv@google.com> --- libavcodec/avpacket.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 7196c31..dca3353 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -225,8 +225,10 @@ int av_copy_packet_side_data(AVPacket *pkt, AVPacket *src) int i; DUP_DATA(pkt->side_data, src->side_data, src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC); - memset(pkt->side_data, 0, - src->side_data_elems * sizeof(*src->side_data)); + if (src != pkt) { + memset(pkt->side_data, 0, + src->side_data_elems * sizeof(*src->side_data)); + } for (i = 0; i < src->side_data_elems; i++) { DUP_DATA(pkt->side_data[i].data, src->side_data[i].data, src->side_data[i].size, 1, ALLOC_MALLOC); -- 1.8.4
On Mon, Sep 02, 2013 at 10:42:22AM -0700, Vignesh Venkatasubramanian wrote:
Fixing av_packet_copy_side_data to work correctly when source and destination are the same. This makes sure that there is no memory leak and double frees.
Signed-off by: Vignesh Venkatasubramanian <vigneshv@google.com> --- libavcodec/avpacket.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
applied thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Complexity theory is the science of finding the exact solution to an approximation. Benchmarking OTOH is finding an approximation of the exact
On Fri, Aug 30, 2013 at 6:57 PM, Michael Niedermayer <michaelni@gmx.at> wrote:
On Sat, Aug 31, 2013 at 03:54:31AM +0200, Michael Niedermayer wrote:
On Fri, Aug 30, 2013 at 03:28:45PM -0700, Vignesh Venkatasubramanian wrote:
On Fri, Aug 30, 2013 at 12:49 PM, Michael Niedermayer <michaelni@gmx.at> wrote:
On Fri, Aug 30, 2013 at 11:26:52AM -0700, Vignesh Venkatasubramanian wrote:
Removing a memset from copy_side_data which could potentially cause double free.
this is a misunderstanding, this patch causes the problems, it does not fix them you can just force a error path and remove the memset to see this in make fate (needs latest git head as there where other bugs when the error paths where used that i just fixed)
ok, any idea as to how i can fix this to get it working to do what i want with the side data copying?
if i undestand the issue correctly puting the memset under if(src != dst) might work
but make sure that the error cases work without leaks and double frees
yes, it worked and i verified that there are no issues with the error cases as well. i tried replying the new patch to this thread, but git send-email decided to create a new thread (probably because i changed the commit message). we shall abandon this thread. could you please review the new patch. thanks.
[...]
-- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Asymptotically faster algorithms should always be preferred if you have asymptotical amounts of data
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
participants (3)
-
James Almer -
Michael Niedermayer -
Vignesh Venkatasubramanian