Skip to content

Commit 4d076cf

Browse files
committed
changes from review
1 parent 305631d commit 4d076cf

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

ext/snmp/snmp.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ static bool php_snmp_parse_oid(
832832
/* {{{ netsnmp_session_init
833833
allocates memory for session and session->peername, caller should free it manually using session_free() and efree()
834834
*/
835-
static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, zend_long timeout, zend_long retries)
835+
static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, zend_long timeout, zend_long retries, int timeout_argument_offset)
836836
{
837837
php_snmp_session *session;
838838
char *pptr, *host_ptr;
@@ -843,21 +843,41 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
843843

844844
*session_p = 0;
845845

846+
ZEND_ASSERT(hostname != NULL);
847+
ZEND_ASSERT(community != NULL);
848+
849+
if (zend_str_has_nul_byte(hostname)) {
850+
zend_argument_value_error(2, "must not contain any null bytes");
851+
return false;
852+
}
853+
846854
if (ZSTR_LEN(hostname) >= MAX_NAME_LEN) {
847-
zend_value_error("hostname length must be lower than %d", MAX_NAME_LEN);
855+
zend_argument_value_error(2, "length must be lower than %d", MAX_NAME_LEN);
848856
return false;
849857
}
850858

851-
if (timeout < -1 || timeout > LONG_MAX) {
852-
zend_value_error("timeout must be between -1 and %ld", LONG_MAX);
859+
if (zend_str_has_nul_byte(community)) {
860+
zend_argument_value_error(3, "must not contain any null bytes");
853861
return false;
854862
}
855863

856-
if (retries < -1 || retries > INT_MAX) {
857-
zend_value_error("retries must be between -1 and %d", INT_MAX);
864+
if (ZSTR_LEN(community) == 0) {
865+
zend_argument_value_error(3, "cannot be empty");
858866
return false;
859867
}
860868

869+
if (timeout_argument_offset != -1) {
870+
if (timeout < -1 || timeout > LONG_MAX) {
871+
zend_argument_value_error(timeout_argument_offset, "must be between -1 and %ld", LONG_MAX);
872+
return false;
873+
}
874+
875+
if (retries < -1 || retries > INT_MAX) {
876+
zend_argument_value_error(timeout_argument_offset, "must be between -1 and %d", INT_MAX);
877+
return false;
878+
}
879+
}
880+
861881
// TODO: Do not strip and re-add the port in peername?
862882
unsigned short remote_port = SNMP_PORT;
863883
int tmp_port;
@@ -1207,6 +1227,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12071227
struct objid_query objid_query;
12081228
php_snmp_session *session;
12091229
int session_less_mode = (getThis() == NULL);
1230+
int timeout_argument_offset = -1;
12101231
php_snmp_object *snmp_object;
12111232
php_snmp_object glob_snmp_object;
12121233

@@ -1233,6 +1254,8 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12331254
Z_PARAM_LONG(timeout)
12341255
Z_PARAM_LONG(retries)
12351256
ZEND_PARSE_PARAMETERS_END();
1257+
1258+
timeout_argument_offset = 10;
12361259
} else {
12371260
/* SNMP_CMD_GET
12381261
* SNMP_CMD_GETNEXT
@@ -1251,6 +1274,8 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12511274
Z_PARAM_LONG(timeout)
12521275
Z_PARAM_LONG(retries)
12531276
ZEND_PARSE_PARAMETERS_END();
1277+
1278+
timeout_argument_offset = 9;
12541279
}
12551280
} else {
12561281
if (st & SNMP_CMD_SET) {
@@ -1264,6 +1289,8 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12641289
Z_PARAM_LONG(timeout)
12651290
Z_PARAM_LONG(retries)
12661291
ZEND_PARSE_PARAMETERS_END();
1292+
1293+
timeout_argument_offset = 6;
12671294
} else {
12681295
/* SNMP_CMD_GET
12691296
* SNMP_CMD_GETNEXT
@@ -1277,6 +1304,8 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12771304
Z_PARAM_LONG(timeout)
12781305
Z_PARAM_LONG(retries)
12791306
ZEND_PARSE_PARAMETERS_END();
1307+
1308+
timeout_argument_offset = 4;
12801309
}
12811310
}
12821311
} else {
@@ -1320,7 +1349,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
13201349
}
13211350

13221351
if (session_less_mode) {
1323-
if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries)) {
1352+
if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries, timeout_argument_offset)) {
13241353
php_free_objid_query(&objid_query, oid_ht, value_ht, st);
13251354
netsnmp_session_free(&session);
13261355
RETURN_FALSE;
@@ -1624,7 +1653,7 @@ PHP_METHOD(SNMP, __construct)
16241653
netsnmp_session_free(&(snmp_object->session));
16251654
}
16261655

1627-
if (!netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries)) {
1656+
if (!netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries, 4)) {
16281657
return;
16291658
}
16301659
snmp_object->max_oids = 0;

ext/snmp/tests/snmp_session_error.phpt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,30 @@ try {
3838
} catch (\ValueError $e) {
3939
echo $e->getMessage(), PHP_EOL;
4040
}
41+
try {
42+
new SNMP(SNMP::VERSION_1, "\0$hostname:$port", $community, $timeout, $retries);
43+
} catch (\ValueError $e) {
44+
echo $e->getMessage(), PHP_EOL;
45+
}
46+
try {
47+
new SNMP(SNMP::VERSION_1, "$hostname:$port", "", $timeout, $retries);
48+
} catch (\ValueError $e) {
49+
echo $e->getMessage(), PHP_EOL;
50+
}
51+
try {
52+
new SNMP(SNMP::VERSION_1, "$hostname:$port", "\0$community", $timeout, $retries);
53+
} catch (\ValueError $e) {
54+
echo $e->getMessage(), PHP_EOL;
55+
}
4156
echo "OK";
4257
?>
4358
--EXPECTF--
4459
remote port must be between 0 and 65535
4560
remote port must be between 0 and 65535
46-
hostname length must be lower than 128
47-
timeout must be between -1 and %d
48-
retries must be between -1 and %d
61+
SNMP::__construct(): Argument #2 ($hostname) length must be lower than 128
62+
SNMP::__construct(): Argument #4 ($timeout) must be between -1 and %d
63+
SNMP::__construct(): Argument #4 ($timeout) must be between -1 and %d
64+
SNMP::__construct(): Argument #2 ($hostname) must not contain any null bytes
65+
SNMP::__construct(): Argument #3 ($community) cannot be empty
66+
SNMP::__construct(): Argument #3 ($community) must not contain any null bytes
4967
OK

0 commit comments

Comments
 (0)