Skip to content

Commit 12c8243

Browse files
committed
Refactored SNMP engine after vulnerabilities
1 parent c61897a commit 12c8243

19 files changed

+1236
-1003
lines changed

Diff for: examples/snmp-server/project-conf.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
2+
* Copyright (C) 2019-2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -30,4 +30,9 @@
3030
*/
3131
/*---------------------------------------------------------------------------*/
3232

33+
/*
34+
* In order to test SNMP compliance,
35+
* at least three OID's in the same package are necessary
36+
*/
37+
#define SNMP_CONF_MAX_NR_VALUES 3
3338
#define LOG_CONF_LEVEL_SNMP LOG_LEVEL_NONE

Diff for: examples/snmp-server/resources/snmp-SNMP-MIB-2-System.c

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
2+
* Copyright (C) 2019-2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -33,91 +33,91 @@
3333

3434
/*---------------------------------------------------------------------------*/
3535
static void
36-
sysDescr_handler(snmp_varbind_t *varbind, uint32_t *oid);
36+
sysDescr_handler(snmp_varbind_t *varbind, snmp_oid_t *oid);
3737

3838
MIB_RESOURCE(sysDescr, sysDescr_handler, 1, 3, 6, 1, 2, 1, 1, 1, 0);
3939

4040
static void
41-
sysDescr_handler(snmp_varbind_t *varbind, uint32_t *oid)
41+
sysDescr_handler(snmp_varbind_t *varbind, snmp_oid_t *oid)
4242
{
4343
snmp_api_set_string(varbind, oid, CONTIKI_VERSION_STRING);
4444
}
4545
/*---------------------------------------------------------------------------*/
4646

4747
/*---------------------------------------------------------------------------*/
4848
static void
49-
sysObjectID_handler(snmp_varbind_t *varbind, uint32_t *oid);
49+
sysObjectID_handler(snmp_varbind_t *varbind, snmp_oid_t *oid);
5050

5151
MIB_RESOURCE(sysObjectID, sysObjectID_handler, 1, 3, 6, 1, 2, 1, 1, 2, 0);
5252

5353
static void
54-
sysObjectID_handler(snmp_varbind_t *varbind, uint32_t *oid)
54+
sysObjectID_handler(snmp_varbind_t *varbind, snmp_oid_t *oid)
5555
{
5656
OID(sysObjectID_oid, 1, 3, 6, 1, 4, 1, 54352);
57-
snmp_api_set_oid(varbind, oid, sysObjectID_oid);
57+
snmp_api_set_oid(varbind, oid, &sysObjectID_oid);
5858
}
5959
/*---------------------------------------------------------------------------*/
6060

6161
/*---------------------------------------------------------------------------*/
6262
static void
63-
sysUpTime_handler(snmp_varbind_t *varbind, uint32_t *oid);
63+
sysUpTime_handler(snmp_varbind_t *varbind, snmp_oid_t *oid);
6464

6565
MIB_RESOURCE(sysUpTime, sysUpTime_handler, 1, 3, 6, 1, 2, 1, 1, 3, 0);
6666

6767
static void
68-
sysUpTime_handler(snmp_varbind_t *varbind, uint32_t *oid)
68+
sysUpTime_handler(snmp_varbind_t *varbind, snmp_oid_t *oid)
6969
{
7070
snmp_api_set_time_ticks(varbind, oid, clock_seconds() * 100);
7171
}
7272
/*---------------------------------------------------------------------------*/
7373

7474
/*---------------------------------------------------------------------------*/
7575
static void
76-
sysContact_handler(snmp_varbind_t *varbind, uint32_t *oid);
76+
sysContact_handler(snmp_varbind_t *varbind, snmp_oid_t *oid);
7777

7878
MIB_RESOURCE(sysContact, sysContact_handler, 1, 3, 6, 1, 2, 1, 1, 4, 0);
7979

8080
static void
81-
sysContact_handler(snmp_varbind_t *varbind, uint32_t *oid)
81+
sysContact_handler(snmp_varbind_t *varbind, snmp_oid_t *oid)
8282
{
8383
snmp_api_set_string(varbind, oid, "Contiki-NG, https://github.com/contiki-ng/contiki-ng");
8484
}
8585
/*---------------------------------------------------------------------------*/
8686

8787
/*---------------------------------------------------------------------------*/
8888
static void
89-
sysName_handler(snmp_varbind_t *varbind, uint32_t *oid);
89+
sysName_handler(snmp_varbind_t *varbind, snmp_oid_t *oid);
9090

9191
MIB_RESOURCE(sysName, sysName_handler, 1, 3, 6, 1, 2, 1, 1, 5, 0);
9292

9393
static void
94-
sysName_handler(snmp_varbind_t *varbind, uint32_t *oid)
94+
sysName_handler(snmp_varbind_t *varbind, snmp_oid_t *oid)
9595
{
9696
snmp_api_set_string(varbind, oid, "Contiki-NG - "CONTIKI_TARGET_STRING);
9797
}
9898
/*---------------------------------------------------------------------------*/
9999

100100
/*---------------------------------------------------------------------------*/
101101
static void
102-
sysLocation_handler(snmp_varbind_t *varbind, uint32_t *oid);
102+
sysLocation_handler(snmp_varbind_t *varbind, snmp_oid_t *oid);
103103

104104
MIB_RESOURCE(sysLocation, sysLocation_handler, 1, 3, 6, 1, 2, 1, 1, 6, 0);
105105

106106
static void
107-
sysLocation_handler(snmp_varbind_t *varbind, uint32_t *oid)
107+
sysLocation_handler(snmp_varbind_t *varbind, snmp_oid_t *oid)
108108
{
109109
snmp_api_set_string(varbind, oid, "");
110110
}
111111
/*---------------------------------------------------------------------------*/
112112

113113
/*---------------------------------------------------------------------------*/
114114
static void
115-
sysServices_handler(snmp_varbind_t *varbind, uint32_t *oid);
115+
sysServices_handler(snmp_varbind_t *varbind, snmp_oid_t *oid);
116116

117117
MIB_RESOURCE(sysServices, sysServices_handler, 1, 3, 6, 1, 2, 1, 1, 7, 0);
118118

119119
static void
120-
sysServices_handler(snmp_varbind_t *varbind, uint32_t *oid)
120+
sysServices_handler(snmp_varbind_t *varbind, snmp_oid_t *oid)
121121
{
122122
snmp_api_set_time_ticks(varbind, oid, clock_seconds() * 100);
123123
}

Diff for: examples/snmp-server/snmp-server.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
2+
* Copyright (C) 2019-2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without

Diff for: os/net/app-layer/snmp/snmp-api.c

+11-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
2+
* Copyright (C) 2019-2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -32,7 +32,7 @@
3232

3333
/**
3434
* \file
35-
* An implementation of the Simple Network Management Protocol (RFC 3411-3418)
35+
* SNMP Implementation of the public API
3636
* \author
3737
* Yago Fontoura do Rosario <yago.rosario@hotmail.com.br
3838
*/
@@ -43,47 +43,31 @@
4343

4444
#include "snmp-message.h"
4545
#include "snmp-ber.h"
46-
#include "snmp-oid.h"
4746

48-
static void
49-
snmp_api_replace_oid(snmp_varbind_t *varbind, uint32_t *oid)
50-
{
51-
uint8_t i;
52-
53-
i = 0;
54-
while(oid[i] != ((uint32_t)-1)) {
55-
varbind->oid[i] = oid[i];
56-
i++;
57-
}
58-
varbind->oid[i] = ((uint32_t)-1);
59-
}
6047
/*---------------------------------------------------------------------------*/
6148
void
62-
snmp_api_set_string(snmp_varbind_t *varbind, uint32_t *oid, char *string)
49+
snmp_api_set_string(snmp_varbind_t *varbind, snmp_oid_t *oid, char *string)
6350
{
64-
65-
snmp_api_replace_oid(varbind, oid);
51+
memcpy(&varbind->oid, oid, sizeof(snmp_oid_t));
6652
varbind->value_type = BER_DATA_TYPE_OCTET_STRING;
6753
varbind->value.string.string = string;
6854
varbind->value.string.length = strlen(string);
6955
}
7056
/*---------------------------------------------------------------------------*/
7157
void
72-
snmp_api_set_time_ticks(snmp_varbind_t *varbind, uint32_t *oid, uint32_t integer)
58+
snmp_api_set_time_ticks(snmp_varbind_t *varbind, snmp_oid_t *oid, uint32_t integer)
7359
{
74-
75-
snmp_api_replace_oid(varbind, oid);
76-
varbind->value_type = SNMP_DATA_TYPE_TIME_TICKS;
60+
memcpy(&varbind->oid, oid, sizeof(snmp_oid_t));
61+
varbind->value_type = BER_DATA_TYPE_TIMETICKS;
7762
varbind->value.integer = integer;
7863
}
7964
/*---------------------------------------------------------------------------*/
8065
void
81-
snmp_api_set_oid(snmp_varbind_t *varbind, uint32_t *oid, uint32_t *ret_oid)
66+
snmp_api_set_oid(snmp_varbind_t *varbind, snmp_oid_t *oid, snmp_oid_t *ret_oid)
8267
{
83-
84-
snmp_api_replace_oid(varbind, oid);
85-
varbind->value_type = BER_DATA_TYPE_OID;
86-
varbind->value.oid = ret_oid;
68+
memcpy(&varbind->oid, oid, sizeof(snmp_oid_t));
69+
varbind->value_type = BER_DATA_TYPE_OBJECT_IDENTIFIER;
70+
memcpy(&varbind->value.oid, ret_oid, sizeof(snmp_oid_t));
8771
}
8872
/*---------------------------------------------------------------------------*/
8973
void

Diff for: os/net/app-layer/snmp/snmp-api.h

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
2+
* Copyright (C) 2019-2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -32,7 +32,7 @@
3232

3333
/**
3434
* \file
35-
* The public API for the Contiki-NG SNMP implementation
35+
* SNMP Implementation of the public API
3636
* \author
3737
* Yago Fontoura do Rosario <yago.rosario@hotmail.com.br
3838
*/
@@ -49,11 +49,10 @@
4949
#include "snmp-mib.h"
5050

5151
/**
52-
* \defgroup SNMPAPI This is the SNMP Public API
52+
* \addtogroup SNMPAPI SNMP Public API
5353
* @{
5454
*
5555
* This group contains all the functions that can be used outside the OS level.
56-
* The function outside this header can be changed without notice
5756
*/
5857

5958
/**
@@ -62,7 +61,7 @@
6261
* @param varbind The varbind that is being changed
6362
* @param oid The oid from the resource
6463
*/
65-
typedef void (*snmp_mib_resource_handler_t)(snmp_varbind_t *varbind, uint32_t *oid);
64+
typedef void (*snmp_mib_resource_handler_t)(snmp_varbind_t *varbind, snmp_oid_t *oid);
6665

6766
/**
6867
* @brief The MIB Resource struct
@@ -78,7 +77,10 @@ typedef struct snmp_mib_resource_s snmp_mib_resource_t;
7877
* @param ... The Oid (comma-separeted)
7978
*/
8079
#define OID(name, ...) \
81-
static uint32_t name[] = { __VA_ARGS__, -1 };
80+
static snmp_oid_t name = { \
81+
.data = __VA_ARGS__, \
82+
.length = (sizeof((uint32_t[]){ __VA_ARGS__ }) / sizeof(uint32_t)) \
83+
};
8284

8385
/**
8486
* @brief Declare a MIB resource
@@ -88,8 +90,14 @@ typedef struct snmp_mib_resource_s snmp_mib_resource_t;
8890
* @param ... The OID (comma-separated)
8991
*/
9092
#define MIB_RESOURCE(name, handler, ...) \
91-
uint32_t name##_oid[] = { __VA_ARGS__, -1 }; \
92-
snmp_mib_resource_t name = { NULL, name##_oid, handler };
93+
snmp_mib_resource_t name = { \
94+
NULL, \
95+
{ \
96+
.data = __VA_ARGS__, \
97+
.length = (sizeof((uint32_t[]){ __VA_ARGS__ }) / sizeof(uint32_t)) \
98+
}, \
99+
handler \
100+
};
93101

94102
/**
95103
* @brief Function to set a varbind with a string
@@ -101,7 +109,7 @@ typedef struct snmp_mib_resource_s snmp_mib_resource_t;
101109
* @param string The string
102110
*/
103111
void
104-
snmp_api_set_string(snmp_varbind_t *varbind, uint32_t *oid, char *string);
112+
snmp_api_set_string(snmp_varbind_t *varbind, snmp_oid_t *oid, char *string);
105113

106114
/**
107115
* @brief Function to set a varbind with a time tick
@@ -113,7 +121,7 @@ snmp_api_set_string(snmp_varbind_t *varbind, uint32_t *oid, char *string);
113121
* @param integer The time tick value
114122
*/
115123
void
116-
snmp_api_set_time_ticks(snmp_varbind_t *varbind, uint32_t *oid, uint32_t integer);
124+
snmp_api_set_time_ticks(snmp_varbind_t *varbind, snmp_oid_t *oid, uint32_t integer);
117125

118126
/**
119127
* @brief Function to set a varbind with a oid
@@ -125,7 +133,7 @@ snmp_api_set_time_ticks(snmp_varbind_t *varbind, uint32_t *oid, uint32_t integer
125133
* @param ret_oid The oid value
126134
*/
127135
void
128-
snmp_api_set_oid(snmp_varbind_t *varbind, uint32_t *oid, uint32_t *ret_oid);
136+
snmp_api_set_oid(snmp_varbind_t *varbind, snmp_oid_t *oid, snmp_oid_t *ret_oid);
129137

130138
/**
131139
* @brief Function to add a new resource

0 commit comments

Comments
 (0)