summaryrefslogtreecommitdiffhomepage
path: root/src/http/modules/ngx_http_mp4_module.c (follow)
AgeCommit message (Collapse)AuthorFilesLines
2024-11-21Mp4: prevent chunk index underflow.Roman Arutyunyan1-0/+6
When cropping stsc atom, it's assumed that chunk index is never 0. Based on this assumption, start_chunk and end_chunk are calculated by subtracting 1 from it. If chunk index is zero, start_chunk or end_chunk may underflow, which will later trigger "start/end time is out mp4 stco chunks" error. The change adds an explicit check for zero chunk index to avoid underflow and report a proper error. Zero chunk index is explicitly banned in ISO/IEC 14496-12, 8.7.4 Sample To Chunk Box. It's also implicitly banned in QuickTime File Format specification. Description of chunk offset table references "Chunk 1" as the first table element.
2024-11-21Mp4: unordered stsc chunks error for the final chunk.Roman Arutyunyan1-0/+7
Currently an error is triggered if any of the chunk runs in stsc are unordered. This however does not include the final chunk run, which ends with trak->chunks + 1. The previous chunk index can be larger leading to a 32-bit overflow. This could allow to skip the validity check "if (start_sample > n)". This could later lead to a large trak->start_chunk/trak->end_chunk, which would be caught later in ngx_http_mp4_update_stco_atom() or ngx_http_mp4_update_co64_atom(). While there are no implications of the validity check being avoided, the change still adds a check to ensure the final chunk run is ordered, to produce a meaningful error and avoid a potential integer overflow.
2024-11-21Mp4: fixed handling an empty run of chunks in stsc atom.Roman Arutyunyan1-1/+4
A specially crafted mp4 file with an empty run of chunks in the stsc atom and a large value for samples per chunk for that run, combined with a specially crafted request, allowed to store that large value in prev_samples and later in trak->end_chunk_samples while in ngx_http_mp4_crop_stsc_data(). Later in ngx_http_mp4_update_stsz_atom() this could result in buffer overread while calculating trak->end_chunk_samples_size. Now the value of samples per chunk specified for an empty run is ignored.
2024-08-12Mp4: rejecting unordered chunks in stsc atom.Roman Arutyunyan1-0/+7
Unordered chunks could result in trak->end_chunk smaller than trak->start_chunk in ngx_http_mp4_crop_stsc_data(). Later in ngx_http_mp4_update_stco_atom() this caused buffer overread while trying to calculate trak->end_offset.
2024-08-12Mp4: fixed buffer underread while updating stsz atom.Roman Arutyunyan1-3/+4
While cropping an stsc atom in ngx_http_mp4_crop_stsc_data(), a 32-bit integer overflow could happen, which could result in incorrect seeking and a very large value stored in "samples". This resulted in a large invalid value of trak->end_chunk_samples. This value is further used to calculate the value of trak->end_chunk_samples_size in ngx_http_mp4_update_stsz_atom(). While doing this, a large invalid value of trak->end_chunk_samples could result in reading memory before stsz atom start. This could potentially result in a segfault.
2023-01-28Fixed "zero size buf" alerts with subrequests.Maxim Dounin1-0/+1
Since 4611:2b6cb7528409 responses from the gzip static, flv, and mp4 modules can be used with subrequests, though empty files were not properly handled. Empty gzipped, flv, and mp4 files thus resulted in "zero size buf in output" alerts. While valid corresponding files are not expected to be empty, such files shouldn't result in alerts. Fix is to set b->sync on such empty subrequest responses, similarly to what ngx_http_send_special() does. Additionally, the static module, the ngx_http_send_response() function, and file cache are modified to do the same instead of not sending the response body at all in such cases, since not sending the response body at all is believed to be at least questionable, and might break various filters which do not expect such behaviour.
2022-10-19Mp4: disabled duplicate atoms.Roman Arutyunyan1-0/+147
Most atoms should not appear more than once in a container. Previously, this was not enforced by the module, which could result in worker process crash, memory corruption and disclosure.
2022-06-07Mp4: fixed potential overflow in ngx_http_mp4_crop_stts_data().Maxim Dounin1-1/+1
Both "count" and "duration" variables are 32-bit, so their product might potentially overflow. It is used to reduce 64-bit start_time variable, and with very large start_time this can result in incorrect seeking. Found by Coverity (CID 1499904).
2021-10-28Mp4: mp4_start_key_frame directive.Roman Arutyunyan1-27/+194
The directive enables including all frames from start time to the most recent key frame in the result. Those frames are removed from presentation timeline using mp4 edit lists. Edit lists are currently supported by popular players and browsers such as Chrome, Safari, QuickTime and ffmpeg. Among those not supporting them properly is Firefox[1]. Based on a patch by Tracey Jaquith, Internet Archive. [1] https://bugzilla.mozilla.org/show_bug.cgi?id=1735300
2021-10-28Mp4: added ngx_http_mp4_update_mdhd_atom() function.Roman Arutyunyan1-8/+32
The function updates the duration field of mdhd atom. Previously it was updated in ngx_http_mp4_read_mdhd_atom(). The change makes it possible to alter track duration as a result of processing track frames.
2020-12-11Fixed double close of non-regular files in flv and mp4.Maxim Dounin1-6/+0
With introduction of open_file_cache in 1454:f497ed7682a7, opening a file with ngx_open_cached_file() automatically adds a cleanup handler to close the file. As such, calling ngx_close_file() directly for non-regular files is no longer needed and will result in duplicate close() call. In 1454:f497ed7682a7 ngx_close_file() call for non-regular files was removed in the static module, but wasn't in the flv module. And the resulting incorrect code was later copied to the mp4 module. Fix is to remove the ngx_close_file() call from both modules. Reported by Chris Newton.
2020-02-26Mp4: fixed possible chunk offset overflow.Roman Arutyunyan1-11/+64
In "co64" atom chunk start offset is a 64-bit unsigned integer. When trimming the "mdat" atom, chunk offsets are casted to off_t values which are typically 64-bit signed integers. A specially crafted mp4 file with huge chunk offsets may lead to off_t overflow and result in negative trim boundaries. The consequences of the overflow are: - Incorrect Content-Length header value in the response. - Negative left boundary of the response file buffer holding the trimmed "mdat". This leads to pread()/sendfile() errors followed by closing the client connection. On rare systems where off_t is a 32-bit integer, this scenario is also feasible with the "stco" atom. The fix is to add checks which make sure data chunks referenced by each track are within the mp4 file boundaries. Additionally a few more checks are added to ensure mp4 file consistency and log errors.
2018-11-21Mp4: fixed possible pointer overflow on 32-bit platforms.Maxim Dounin1-1/+8
On 32-bit platforms mp4->buffer_pos might overflow when a large enough (close to 4 gigabytes) atom is being skipped, resulting in incorrect memory addesses being read further in the code. In most cases this results in harmless errors being logged, though may also result in a segmentation fault if hitting unmapped pages. To address this, ngx_mp4_atom_next() now only increments mp4->buffer_pos up to mp4->buffer_end. This ensures that overflow cannot happen.
2018-11-06Mp4: fixed reading 64-bit atoms.Roman Arutyunyan1-0/+7
Previously there was no validation for the size of a 64-bit atom in an mp4 file. This could lead to a CPU hog when the size is 0, or various other problems due to integer underflow when calculating atom data size, including segmentation fault or worker process memory disclosure.
2017-04-12Use ngx_calloc_buf() where appropriate.Ruslan Ermilov1-1/+1
2016-11-22Mp4: fixed setting wrong mdat atom size in very rare cases.hucongcong1-1/+3
Atom size is the sum of atom header size and atom data size. The specification says that the first 4 bytes are set to one when the atom size is greater than the maximum unsigned 32-bit value. Which means atom header size should be considered when the comparison takes place between atom data size and 0xffffffff.
2016-10-27Mp4: introduced custom version of ngx_atofp().Maxim Dounin1-14/+60
This allows to correctly parse "start" and "end" arguments without null-termination (ticket #475), and also fixes rounding errors observed with strtod() when using i387 instructions.
2016-10-03Do not set last_buf flag in subrequests.Roman Arutyunyan1-1/+1
The last_buf flag should only be set in the last buffer of the main request. Otherwise, several last_buf flags can appear in output. This can, for example, break the chunked filter, which will include several final chunks in output.
2016-04-07Fixed spelling.Josh Soref1-4/+4
2016-03-31Fixed logging with variable field width.Sergey Kandaurov1-2/+2
2016-03-30Style.Ruslan Ermilov1-8/+8
2014-08-19Mp4: use trak->smhd_size in ngx_http_mp4_read_smhd_atom().Roman Arutyunyan1-1/+1
Reported by Gang Li.
2014-04-01Mp4: allow end values bigger than track duration.Roman Arutyunyan1-4/+14
If start time is within the track but end time is out of it, error "end time is out mp4 stts samples" is generated. However it's better to ignore the error and output the track until its end.
2014-03-31Mp4: improved logging after adding "end" support.Roman Arutyunyan1-27/+71
Despite introducing start and end crop operations existing log messages still mostly refer only to start. Logging is improved to match both cases. New debug logging is added to track entry count in atoms after cropping. Two format type mismatches are fixed as well.
2014-03-31Mp4: fixed seeking to a track end.Roman Arutyunyan1-2/+2
When "start" value is equal to a track duration the request fails with "time is out mp4 stts" like it did before track duration check was added. Now such tracks are considered short and skipped.
2014-03-24Mp4: skipped empty stss atom table in output.Sergey Kandaurov1-8/+13
The atom may have no data entries after cropping. This fixes "zero size buf in output" alerts.
2014-03-21Range filter: single_range flag in request.Maxim Dounin1-1/+1
If set, it means that response body is going to be in more than one buffer, hence only range requests with a single range should be honored. The flag is now used by mp4 and cacheable upstream responses, thus allowing range requests of mp4 files with start/end, as well as range processing on a first request to a not-yet-cached files with proxy_cache. Notably this makes it possible to play mp4 files (with proxy_cache, or with mp4 module) on iOS devices, as byte-range support is required by Apple.
2014-03-20Mp4: added "end" argument support.Roman Arutyunyan1-89/+377
2014-03-20Mp4: moved atom cropping code out of update functions.Roman Arutyunyan1-85/+176
It can now be reused for implementing mp4 end.
2014-02-14Mp4: remove useless leading stsc entry in result mp4.Roman Arutyunyan1-1/+1
The fix removes useless stsc entry in result mp4. If start_sample == n then current stsc entry should be skipped and the result stsc should start with the next entry. The reason for that is start_sample starts from 0, not 1.
2014-01-29Mp4: fix seeks to standalone last chunk.Roman Arutyunyan1-1/+1
If seek position is within the last track chunk and that chunk is standalone (stsc entry describes only this chunk) such seek generates stsc seek error. The problem is that chunk numbers start with 1, not with 0.
2014-01-29Mp4: skip tracks shorter than seek position (ticket #414).Roman Arutyunyan1-6/+31
Mp4 module does not check movie and track durations when reading file. Instead it generates errors when track metadata is shorter than seek position. Now such tracks are skipped and movie duration check is performed at file read stage.
2014-01-29Mp4: fix seeks after the last key frame.Roman Arutyunyan1-5/+2
Mp4 module does not allow seeks after the last key frame. Since stss atom only contains key frames it's usually shorter than other track atoms. That leads to stss seek error when seek position is close to the end of file. The fix outputs empty stss frame instead of generating error.
2013-12-27Style: removed surplus semicolons.Valentin Bartenev1-1/+1
2013-09-04Win32: Borland C compatibility fixes.Maxim Dounin1-3/+7
Several false positive warnings silenced, notably W8012 "Comparing signed and unsigned" (due to u_short values promoted to int), and W8072 "Suspicious pointer arithmetic" (due to large type values added to pointers). With this patch, it's now again possible to compile nginx using bcc32, with options we normally compile on win32 minus ipv6 and ssl.
2013-09-04Win32: Open Watcom C compatibility fixes.Maxim Dounin1-1/+1
Precompiled headers are disabled as they lead to internal compiler errors with long configure lines. Couple of false positive warnings silenced. Various win32 typedefs are adjusted to work with Open Watcom C 1.9 headers. With this patch, it's now again possible to compile nginx using owc386, with options we normally compile on win32 minus ipv6 and ssl.
2013-05-11Mp4: indentation and style, no functional changes.Maxim Dounin1-31/+39
2013-03-04Mp4: fixed handling of too small mdat atoms (ticket #266).Maxim Dounin1-0/+7
Patch by Gernot Vormayr (with minor changes).
2012-08-17Mp4: removed restriction to avc1/mp4a formats (ticket #194).Maxim Dounin1-8/+0
2012-07-09Entity tags: set for static respones.Maxim Dounin1-0/+4
2012-06-26Mp4: fixed build on win32 after r4689.Maxim Dounin1-1/+1
2012-06-18Mp4: fixed streaming if moov atom is at buffer edge.Maxim Dounin1-0/+10
2012-06-18Mp4: fixed non-keyframe seeks in some cases (ticket #175).Maxim Dounin1-1/+8
Number of entries in stsc atom was wrong if we've added an entry to split a chunk. Additionally, there is no need to add an entry if we are going to split last chunk in an entry, it's enough to update the entry we already have. Previously new entry was added and old one was left as is, resulting in incorrect entry with zero chunks which might confuse some software.
2012-04-26Allows particular modules to handle subrequests properly.Andrey Belov1-1/+1
2012-04-12Mp4: sanity checks cleanup.Maxim Dounin1-66/+208
2012-04-10Fixed debug logging.Igor Sysoev1-1/+1
2012-04-10Fixed previous commit.Igor Sysoev1-1/+1
2012-04-10Fixed mp4 module seek.Igor Sysoev1-1/+1
2012-02-27Disable symlinks: initialization of the "disable_symlinks" field inValentin Bartenev1-3/+4
ngx_open_file_info_t moved to a separate function. This is preparation for the "from=" parameter implementation of the "disable_symlinks" directive.
2012-02-13Support for disable_symlinks in various modules.Andrey Belov1-0/+7