Skip to content

Commit

Permalink
[core] Check invalid sockopt values (#1956)
Browse files Browse the repository at this point in the history
* SRTO_CONNTIMEO: reject negative
* SRTO_PAYLOADSIZE: reject negative and fix FEC
* SRTO_SNDDROPDELAY >= -1
* SRTO_SNDTIMEO >= -1
* SRTO_*LATENCY >= 0
* SRTO_PEERIDLETIMEO >= 0
* SRTO_RCVTIMEO >= -1
  • Loading branch information
maxsharabayko authored Apr 19, 2021
1 parent 43b78eb commit 3bf5ceb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 31 deletions.
34 changes: 17 additions & 17 deletions docs/API/API-socket-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ The following table lists SRT API socket options in alphabetical order. Option d
| Option Name | Since | Restrict | Type | Units | Default | Range | Dir |Entity |
| :----------------------------------------------------- | :---: | :------: | :-------: | :-----: | :---------------: | :------: |:---:|:-----:|
| [`SRTO_BINDTODEVICE`](#SRTO_BINDTODEVICE) | 1.4.2 | pre-bind | `string` | | | | RW | GSD+ |
| [`SRTO_CONGESTION`](#SRTO_CONGESTION) | 1.3.0 | pre | `string` | | "live" | * | W | S |
| [`SRTO_CONGESTION`](#SRTO_CONGESTION) | 1.3.0 | pre | `string` | | "live" | \* | W | S |
| [`SRTO_CONNTIMEO`](#SRTO_CONNTIMEO) | 1.1.2 | pre | `int32_t` | ms | 3000 | 0.. | W | GSD+ |
| [`SRTO_DRIFTTRACER`](#SRTO_DRIFTTRACER) | 1.4.2 | post | `bool` | | true | | RW | GSD |
| [`SRTO_ENFORCEDENCRYPTION`](#SRTO_ENFORCEDENCRYPTION) | 1.3.2 | pre | `bool` | | true | | W | GSD |
Expand All @@ -212,49 +212,49 @@ The following table lists SRT API socket options in alphabetical order. Option d
| [`SRTO_IPTTL`](#SRTO_IPTTL) | 1.0.5 | pre-bind | `int32_t` | hops | (system) | 1..255 | RW | GSD |
| [`SRTO_IPV6ONLY`](#SRTO_IPV6ONLY) | 1.4.0 | pre-bind | `int32_t` | | (system) | -1..1 | RW | GSD |
| [`SRTO_ISN`](#SRTO_ISN) | 1.3.0 | | `int32_t` | | | | R | S |
| [`SRTO_KMPREANNOUNCE`](#SRTO_KMPREANNOUNCE) | 1.3.2 | pre | `int32_t` | pkts | 0: 2<sup>12</sup> | 0.. * | RW | GSD |
| [`SRTO_KMPREANNOUNCE`](#SRTO_KMPREANNOUNCE) | 1.3.2 | pre | `int32_t` | pkts | 0: 2<sup>12</sup> | 0.. \* | RW | GSD |
| [`SRTO_KMREFRESHRATE`](#SRTO_KMREFRESHRATE) | 1.3.2 | pre | `int32_t` | pkts | 0: 2<sup>24</sup> | 0.. | RW | GSD |
| [`SRTO_KMSTATE`](#SRTO_KMSTATE) | 1.0.2 | | `int32_t` | enum | | | R | S |
| [`SRTO_LATENCY`](#SRTO_LATENCY) | 1.0.2 | pre | `int32_t` | ms | 120 * | 0.. | RW | GSD |
| [`SRTO_LATENCY`](#SRTO_LATENCY) | 1.0.2 | pre | `int32_t` | ms | 120 \* | 0.. | RW | GSD |
| [`SRTO_LINGER`](#SRTO_LINGER) | | post | `linger` | s | on, 180 | 0.. | RW | GSD |
| [`SRTO_LOSSMAXTTL`](#SRTO_LOSSMAXTTL) | 1.2.0 | post | `int32_t` | packets | 0 | 0.. | RW | GSD+ |
| [`SRTO_MAXBW`](#SRTO_MAXBW) | | post | `int64_t` | B/s | -1 | -1.. | RW | GSD |
| [`SRTO_MESSAGEAPI`](#SRTO_MESSAGEAPI) | 1.3.0 | pre | `bool` | | true | | W | GSD |
| [`SRTO_MININPUTBW`](#SRTO_MININPUTBW) | 1.4.3 | post | `int64_t` | B/s | 0 | 0.. | RW | GSD |
| [`SRTO_MINVERSION`](#SRTO_MINVERSION) | 1.3.0 | pre | `int32_t` | version | 0x010000 | * | RW | GSD |
| [`SRTO_MINVERSION`](#SRTO_MINVERSION) | 1.3.0 | pre | `int32_t` | version | 0x010000 | \* | RW | GSD |
| [`SRTO_MSS`](#SRTO_MSS) | | pre-bind | `int32_t` | bytes | 1500 | 76.. | RW | GSD |
| [`SRTO_NAKREPORT`](#SRTO_NAKREPORT) | 1.1.0 | pre | `bool` | | * | | RW | GSD+ |
| [`SRTO_NAKREPORT`](#SRTO_NAKREPORT) | 1.1.0 | pre | `bool` | | \* | | RW | GSD+ |
| [`SRTO_OHEADBW`](#SRTO_OHEADBW) | 1.0.5 | post | `int32_t` | % | 25 | 5..100 | RW | GSD |
| [`SRTO_PACKETFILTER`](#SRTO_PACKETFILTER) | 1.4.0 | pre | `string` | | "" | [512] | RW | GSD |
| [`SRTO_PASSPHRASE`](#SRTO_PASSPHRASE) | 0.0.0 | pre | `string` | | "" | [10..79] | W | GSD |
| [`SRTO_PAYLOADSIZE`](#SRTO_PAYLOADSIZE) | 1.3.0 | pre | `int32_t` | bytes | \* | \* | W | GSD |
| [`SRTO_PBKEYLEN`](#SRTO_PBKEYLEN) | 0.0.0 | pre | `int32_t` | bytes | 0 | * | RW | GSD |
| [`SRTO_PAYLOADSIZE`](#SRTO_PAYLOADSIZE) | 1.3.0 | pre | `int32_t` | bytes | \* | 0.. \* | W | GSD |
| [`SRTO_PBKEYLEN`](#SRTO_PBKEYLEN) | 0.0.0 | pre | `int32_t` | bytes | 0 | \* | RW | GSD |
| [`SRTO_PEERIDLETIMEO`](#SRTO_PEERIDLETIMEO) | 1.3.3 | pre | `int32_t` | ms | 5000 | 0.. | RW | GSD+ |
| [`SRTO_PEERLATENCY`](#SRTO_PEERLATENCY) | 1.3.0 | pre | `int32_t` | ms | 0 | 0.. | RW | GSD |
| [`SRTO_PEERVERSION`](#SRTO_PEERVERSION) | 1.1.0 | | `int32_t` | * | | | R | GS |
| [`SRTO_RCVBUF`](#SRTO_RCVBUF) | | pre-bind | `int32_t` | bytes | 8192 payloads | * | RW | GSD+ |
| [`SRTO_RCVBUF`](#SRTO_RCVBUF) | | pre-bind | `int32_t` | bytes | 8192 payloads | \* | RW | GSD+ |
| [`SRTO_RCVDATA`](#SRTO_RCVDATA) | | | `int32_t` | pkts | | | R | S |
| [`SRTO_RCVKMSTATE`](#SRTO_RCVKMSTATE) | 1.2.0 | | `int32_t` | enum | | | R | S |
| [`SRTO_RCVLATENCY`](#SRTO_RCVLATENCY) | 1.3.0 | pre | `int32_t` | msec | * | 0.. | RW | GSD |
| [`SRTO_RCVLATENCY`](#SRTO_RCVLATENCY) | 1.3.0 | pre | `int32_t` | msec | \* | 0.. | RW | GSD |
| [`SRTO_RCVSYN`](#SRTO_RCVSYN) | | post | `bool` | | true | | RW | GSI |
| [`SRTO_RCVTIMEO`](#SRTO_RCVTIMEO) | | post | `int32_t` | ms | -1 | -1, 0.. | RW | GSI |
| [`SRTO_RENDEZVOUS`](#SRTO_RENDEZVOUS) | | pre | `bool` | | false | | RW | S |
| [`SRTO_RETRANSMITALGO`](#SRTO_RETRANSMITALGO) | 1.4.2 | pre | `int32_t` | | 0 | [0, 1] | RW | GSD |
| [`SRTO_REUSEADDR`](#SRTO_REUSEADDR) | | pre-bind | `bool` | | true | | RW | GSD |
| [`SRTO_SENDER`](#SRTO_SENDER) | 1.0.4 | pre | `bool` | | false | | W | S |
| [`SRTO_SNDBUF`](#SRTO_SNDBUF) | | pre-bind | `int32_t` | bytes | 8192 payloads | * | RW | GSD+ |
| [`SRTO_SNDBUF`](#SRTO_SNDBUF) | | pre-bind | `int32_t` | bytes | 8192 payloads | \* | RW | GSD+ |
| [`SRTO_SNDDATA`](#SRTO_SNDDATA) | | | `int32_t` | pkts | | | R | S |
| [`SRTO_SNDDROPDELAY`](#SRTO_SNDDROPDELAY) | 1.3.2 | post | `int32_t` | ms | * | -1.. | W | GSD+ |
| [`SRTO_SNDDROPDELAY`](#SRTO_SNDDROPDELAY) | 1.3.2 | post | `int32_t` | ms | \* | -1.. | W | GSD+ |
| [`SRTO_SNDKMSTATE`](#SRTO_SNDKMSTATE) | 1.2.0 | | `int32_t` | enum | | | R | S |
| [`SRTO_SNDSYN`](#SRTO_SNDSYN) | | post | `bool` | | true | | RW | GSI |
| [`SRTO_SNDTIMEO`](#SRTO_SNDTIMEO) | | post | `int32_t` | ms | -1 | -1.. | RW | GSI |
| [`SRTO_STATE`](#SRTO_STATE) | | | `int32_t` | enum | | | R | S |
| [`SRTO_STREAMID`](#SRTO_STREAMID) | 1.3.0 | pre | `string` | | "" | [512] | RW | GSD |
| [`SRTO_TLPKTDROP`](#SRTO_TLPKTDROP) | 1.0.6 | pre | `bool` | | * | | RW | GSD |
| [`SRTO_TRANSTYPE`](#SRTO_TRANSTYPE) | 1.3.0 | pre | `int32_t` | enum |`SRTT_LIVE` | * | W | S |
| [`SRTO_TSBPDMODE`](#SRTO_TSBPDMODE) | 0.0.0 | pre | `bool` | | * | | W | S |
| [`SRTO_UDP_RCVBUF`](#SRTO_UDP_RCVBUF) | | pre-bind | `int32_t` | bytes | 8192 payloads | * | RW | GSD+ |
| [`SRTO_UDP_SNDBUF`](#SRTO_UDP_SNDBUF) | | pre-bind | `int32_t` | bytes | 65536 | * | RW | GSD+ |
| [`SRTO_TLPKTDROP`](#SRTO_TLPKTDROP) | 1.0.6 | pre | `bool` | | \* | | RW | GSD |
| [`SRTO_TRANSTYPE`](#SRTO_TRANSTYPE) | 1.3.0 | pre | `int32_t` | enum |`SRTT_LIVE` | \* | W | S |
| [`SRTO_TSBPDMODE`](#SRTO_TSBPDMODE) | 0.0.0 | pre | `bool` | | \* | | W | S |
| [`SRTO_UDP_RCVBUF`](#SRTO_UDP_RCVBUF) | | pre-bind | `int32_t` | bytes | 8192 payloads | \* | RW | GSD+ |
| [`SRTO_UDP_SNDBUF`](#SRTO_UDP_SNDBUF) | | pre-bind | `int32_t` | bytes | 65536 | \* | RW | GSD+ |
| [`SRTO_VERSION`](#SRTO_VERSION) | 1.1.0 | | `int32_t` | | | | R | S |

### Option Descriptions
Expand Down Expand Up @@ -997,7 +997,7 @@ encrypted connection, they have to simply set the same passphrase.

| OptName | Since | Restrict | Type | Units | Default | Range | Dir | Entity |
| -------------------- | ----- | -------- | ---------- | ------- | -------- | ------ | --- | ------ |
| `SRTO_PAYLOADSIZE` | 1.3.0 | pre | `int32_t` | bytes | \* | \* | W | GSD |
| `SRTO_PAYLOADSIZE` | 1.3.0 | pre | `int32_t` | bytes | \* | 0.. \* | W | GSD |

Sets the maximum declared size of a single call to sending function in Live
mode. When set to 0, there's no limit for a single sending call.
Expand Down
63 changes: 49 additions & 14 deletions srtcore/socketconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,11 @@ struct CSrtConfigSetter<SRTO_SNDTIMEO>
{
static void set(CSrtConfig& co, const void* optval, int optlen)
{
co.iSndTimeOut = cast_optval<int>(optval, optlen);
const int val = cast_optval<int>(optval, optlen);
if (val < -1)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);

co.iSndTimeOut = val;
}
};

Expand All @@ -511,7 +515,11 @@ struct CSrtConfigSetter<SRTO_RCVTIMEO>
{
static void set(CSrtConfig& co, const void* optval, int optlen)
{
co.iRcvTimeOut = cast_optval<int>(optval, optlen);
const int val = cast_optval<int>(optval, optlen);
if (val < -1)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);

co.iRcvTimeOut = val;
}
};

Expand Down Expand Up @@ -660,24 +668,36 @@ struct CSrtConfigSetter<SRTO_LATENCY>
{
static void set(CSrtConfig& co, const void* optval, int optlen)
{
co.iRcvLatency = cast_optval<int>(optval, optlen);
co.iPeerLatency = cast_optval<int>(optval);
const int val = cast_optval<int>(optval, optlen);
if (val < 0)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);

co.iRcvLatency = val;
co.iPeerLatency = val;
}
};
template<>
struct CSrtConfigSetter<SRTO_RCVLATENCY>
{
static void set(CSrtConfig& co, const void* optval, int optlen)
{
co.iRcvLatency = cast_optval<int>(optval, optlen);
const int val = cast_optval<int>(optval, optlen);
if (val < 0)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);

co.iRcvLatency = val;
}
};
template<>
struct CSrtConfigSetter<SRTO_PEERLATENCY>
{
static void set(CSrtConfig& co, const void* optval, int optlen)
{
co.iPeerLatency = cast_optval<int>(optval, optlen);
const int val = cast_optval<int>(optval, optlen);
if (val < 0)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);

co.iPeerLatency = val;
}
};
template<>
Expand All @@ -693,9 +713,11 @@ struct CSrtConfigSetter<SRTO_SNDDROPDELAY>
{
static void set(CSrtConfig& co, const void* optval, int optlen)
{
// Surprise: you may be connected to alter this option.
// The application may manipulate this option on sender while transmitting.
co.iSndDropDelay = cast_optval<int>(optval, optlen);
const int val = cast_optval<int>(optval, optlen);
if (val < -1)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);

co.iSndDropDelay = val;
}
};
template<>
Expand Down Expand Up @@ -803,8 +825,12 @@ struct CSrtConfigSetter<SRTO_CONNTIMEO>
{
static void set(CSrtConfig& co, const void* optval, int optlen)
{
const int val = cast_optval<int>(optval, optlen);
if (val < 0)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);

using namespace srt::sync;
co.tdConnTimeOut = milliseconds_from(cast_optval<int>(optval, optlen));
co.tdConnTimeOut = milliseconds_from(val);
}
};

Expand Down Expand Up @@ -894,8 +920,13 @@ struct CSrtConfigSetter<SRTO_PAYLOADSIZE>
static void set(CSrtConfig& co, const void* optval, int optlen)
{
using namespace srt_logging;
const int val = cast_optval<int>(optval, optlen);
if (val < 0)
{
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
}

if (*(int *)optval > SRT_LIVE_MAX_PLSIZE)
if (val > SRT_LIVE_MAX_PLSIZE)
{
LOGC(aclog.Error, log << "SRTO_PAYLOADSIZE: value exceeds SRT_LIVE_MAX_PLSIZE, maximum payload per MTU.");
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
Expand All @@ -915,7 +946,7 @@ struct CSrtConfigSetter<SRTO_PAYLOADSIZE>
}

size_t efc_max_payload_size = SRT_LIVE_MAX_PLSIZE - fc.extra_size;
if (co.zExpPayloadSize > efc_max_payload_size)
if (val > efc_max_payload_size)
{
LOGC(aclog.Error,
log << "SRTO_PAYLOADSIZE: value exceeds SRT_LIVE_MAX_PLSIZE decreased by " << fc.extra_size
Expand All @@ -924,7 +955,7 @@ struct CSrtConfigSetter<SRTO_PAYLOADSIZE>
}
}

co.zExpPayloadSize = cast_optval<int>(optval, optlen);
co.zExpPayloadSize = val;
}
};

Expand Down Expand Up @@ -1072,7 +1103,11 @@ struct CSrtConfigSetter<SRTO_PEERIDLETIMEO>
{
static void set(CSrtConfig& co, const void* optval, int optlen)
{
co.iPeerIdleTimeout = cast_optval<int>(optval, optlen);
const int val = cast_optval<int>(optval, optlen);
if (val < 0)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);

co.iPeerIdleTimeout = val;
}
};

Expand Down

0 comments on commit 3bf5ceb

Please sign in to comment.