Skip to content

Commit 097bccf

Browse files
committed
Fix GH-21336: address code review for snmp setSecurity.
1 parent 90862fe commit 097bccf

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

ext/snmp/snmp.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -959,12 +959,8 @@ static bool netsnmp_session_set_sec_level(struct snmp_session *s, zend_string *l
959959
/* }}} */
960960

961961
/* {{{ Set the authentication protocol in the snmpv3 session */
962-
static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_string *prot)
962+
static ZEND_ATTRIBUTE_NONNULL bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_string *prot)
963963
{
964-
if (!prot) {
965-
zend_value_error("Authentication protocol can't be NULL");
966-
return false;
967-
}
968964
#ifndef DISABLE_MD5
969965
if (zend_string_equals_literal_ci(prot, "MD5")) {
970966
s->securityAuthProto = usmHMACMD5AuthProtocol;
@@ -1015,12 +1011,8 @@ static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_strin
10151011
/* }}} */
10161012

10171013
/* {{{ Set the security protocol in the snmpv3 session */
1018-
static bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string *prot)
1014+
static ZEND_ATTRIBUTE_NONNULL bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string *prot)
10191015
{
1020-
if (!prot) {
1021-
zend_value_error("Security protocol can't be NULL");
1022-
return false;
1023-
}
10241016
#ifndef NETSNMP_DISABLE_DES
10251017
if (zend_string_equals_literal_ci(prot, "DES")) {
10261018
s->securityPrivProto = usmDESPrivProtocol;
@@ -1056,15 +1048,10 @@ static bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string
10561048
/* }}} */
10571049

10581050
/* {{{ Make key from pass phrase in the snmpv3 session */
1059-
static bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pass)
1051+
static ZEND_ATTRIBUTE_NONNULL bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pass)
10601052
{
10611053
int snmp_errno;
10621054

1063-
if (!pass) {
1064-
zend_value_error("Authentication key can't be NULL");
1065-
return false;
1066-
}
1067-
10681055
s->securityAuthKeyLen = USM_AUTH_KU_LEN;
10691056
if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
10701057
(uint8_t *) ZSTR_VAL(pass), ZSTR_LEN(pass),
@@ -1077,15 +1064,10 @@ static bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pa
10771064
/* }}} */
10781065

10791066
/* {{{ Make key from pass phrase in the snmpv3 session */
1080-
static bool netsnmp_session_gen_sec_key(struct snmp_session *s, zend_string *pass)
1067+
static ZEND_ATTRIBUTE_NONNULL bool netsnmp_session_gen_sec_key(struct snmp_session *s, zend_string *pass)
10811068
{
10821069
int snmp_errno;
10831070

1084-
if (!pass) {
1085-
zend_value_error("Security key can't be NULL");
1086-
return false;
1087-
}
1088-
10891071
s->securityPrivKeyLen = USM_PRIV_KU_LEN;
10901072
if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
10911073
(uint8_t *)ZSTR_VAL(pass), ZSTR_LEN(pass),
@@ -1121,7 +1103,7 @@ static bool netsnmp_session_set_contextEngineID(struct snmp_session *s, zend_str
11211103
/* }}} */
11221104

11231105
/* {{{ Set all snmpv3-related security options */
1124-
static bool netsnmp_session_set_security(struct snmp_session *session, zend_string *sec_level,
1106+
static ZEND_ATTRIBUTE_NONNULL_ARGS(2) bool netsnmp_session_set_security(struct snmp_session *session, zend_string *sec_level,
11251107
zend_string *auth_protocol, zend_string *auth_passphrase, zend_string *priv_protocol,
11261108
zend_string *priv_passphrase, zend_string *contextName, zend_string *contextEngineID)
11271109
{
@@ -1134,25 +1116,46 @@ static bool netsnmp_session_set_security(struct snmp_session *session, zend_stri
11341116

11351117
if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
11361118

1119+
if (!auth_protocol) {
1120+
zend_value_error("Authentication protocol is required for security level \"authNoPriv\" or \"authPriv\"");
1121+
return false;
1122+
}
1123+
11371124
/* Setting the authentication protocol. */
11381125
if (!netsnmp_session_set_auth_protocol(session, auth_protocol)) {
11391126
/* ValueError already generated, just bail out */
11401127
return false;
11411128
}
11421129

1130+
if (!auth_passphrase) {
1131+
zend_value_error("Authentication passphrase is required for security level \"authNoPriv\" or \"authPriv\"");
1132+
return false;
1133+
}
1134+
11431135
/* Setting the authentication passphrase. */
11441136
if (!netsnmp_session_gen_auth_key(session, auth_passphrase)) {
11451137
/* Warning message sent already, just bail out */
11461138
return false;
11471139
}
11481140

11491141
if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1142+
1143+
if (!priv_protocol) {
1144+
zend_value_error("Privacy protocol is required for security level \"authPriv\"");
1145+
return false;
1146+
}
1147+
11501148
/* Setting the security protocol. */
11511149
if (!netsnmp_session_set_sec_protocol(session, priv_protocol)) {
11521150
/* ValueError already generated, just bail out */
11531151
return false;
11541152
}
11551153

1154+
if (!priv_passphrase) {
1155+
zend_value_error("Privacy passphrase is required for security level \"authPriv\"");
1156+
return false;
1157+
}
1158+
11561159
/* Setting the security protocol passphrase. */
11571160
if (!netsnmp_session_gen_sec_key(session, priv_passphrase)) {
11581161
/* Warning message sent already, just bail out */

ext/snmp/tests/gh21336.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ try {
3535
}
3636
?>
3737
--EXPECT--
38-
Authentication protocol can't be NULL
39-
Authentication key can't be NULL
40-
Security protocol can't be NULL
41-
Security key can't be NULL
38+
Authentication protocol is required for security level "authNoPriv" or "authPriv"
39+
Authentication passphrase is required for security level "authNoPriv" or "authPriv"
40+
Privacy protocol is required for security level "authPriv"
41+
Privacy passphrase is required for security level "authPriv"

0 commit comments

Comments
 (0)