1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
--- lib/libradius/radlib.c.orig
+++ lib/libradius/radlib.c
@@ -187,7 +187,7 @@
MD5_CTX ctx;
unsigned char md5[MD5_DIGEST_LENGTH];
const struct rad_server *srvp;
- int len;
+ int alen, len;
#ifdef WITH_SSL
HMAC_CTX *hctx;
u_char resp[MSGSIZE], md[EVP_MAX_MD_SIZE];
@@ -206,8 +206,8 @@
/* Check the message length */
if (h->in_len < POS_ATTRS)
return 0;
- len = h->in[POS_LENGTH] << 8 | h->in[POS_LENGTH+1];
- if (len > h->in_len)
+ len = (h->in[POS_LENGTH] << 8) | h->in[POS_LENGTH + 1];
+ if (len < POS_ATTRS || len > h->in_len)
return 0;
/* Check the response authenticator */
@@ -233,9 +233,16 @@
/* Search and verify the Message-Authenticator */
hctx = HMAC_CTX_new();
while (pos < len - 2) {
-
if (h->in[pos] == RAD_MESSAGE_AUTHENTIC) {
- /* zero fill the Message-Authenticator */
+ if (h->in[pos + 1] != MD5_DIGEST_LENGTH + 2) {
+ HMAC_CTX_free(hctx);
+ return 0;
+ }
+ if (len - pos < MD5_DIGEST_LENGTH + 2) {
+ HMAC_CTX_free(hctx);
+ return 0;
+ }
+
memset(&resp[pos + 2], 0, MD5_DIGEST_LENGTH);
HMAC_Init_ex(hctx, srvp->secret,
@@ -255,7 +262,12 @@
}
break;
}
- pos += h->in[pos + 1];
+ alen = h->in[pos + 1];
+ if (alen < 2) {
+ HMAC_CTX_free(hctx);
+ return 0;
+ }
+ pos += alen;
}
HMAC_CTX_free(hctx);
}
@@ -272,7 +284,7 @@
MD5_CTX ctx;
unsigned char md5[MD5_DIGEST_LENGTH];
const struct rad_server *srvp;
- int len;
+ int alen, len;
#ifdef WITH_SSL
HMAC_CTX *hctx;
u_char resp[MSGSIZE], md[EVP_MAX_MD_SIZE];
@@ -285,8 +297,8 @@
/* Check the message length */
if (h->in_len < POS_ATTRS)
return (0);
- len = h->in[POS_LENGTH] << 8 | h->in[POS_LENGTH+1];
- if (len > h->in_len)
+ len = (h->in[POS_LENGTH] << 8) | h->in[POS_LENGTH + 1];
+ if (len < POS_ATTRS || len > h->in_len)
return (0);
if (h->in[POS_CODE] != RAD_ACCESS_REQUEST) {
@@ -307,7 +319,18 @@
pos = POS_ATTRS;
hctx = HMAC_CTX_new();
while (pos < len - 2) {
+ alen = h->in[pos + 1];
+ if (alen < 2)
+ return (0);
if (h->in[pos] == RAD_MESSAGE_AUTHENTIC) {
+ if (len - pos < MD5_DIGEST_LENGTH + 2) {
+ HMAC_CTX_free(hctx);
+ return (0);
+ }
+ if (alen < MD5_DIGEST_LENGTH + 2) {
+ HMAC_CTX_free(hctx);
+ return (0);
+ }
memcpy(resp, h->in, MSGSIZE);
/* zero fill the Request-Authenticator */
if (h->in[POS_CODE] != RAD_ACCESS_REQUEST)
@@ -327,7 +350,7 @@
}
break;
}
- pos += h->in[pos + 1];
+ pos += alen;
}
HMAC_CTX_free(hctx);
#endif
@@ -929,9 +952,9 @@
* returns -1.
*/
int
-rad_get_attr(struct rad_handle *h, const void **value, size_t *len)
+rad_get_attr(struct rad_handle *h, const void **value, size_t *lenp)
{
- int type;
+ int len, type;
if (h->in_pos >= h->in_len)
return 0;
@@ -940,13 +963,14 @@
return -1;
}
type = h->in[h->in_pos++];
- *len = h->in[h->in_pos++] - 2;
- if (h->in_pos + (int)*len > h->in_len) {
+ len = h->in[h->in_pos++];
+ if (len < 2 || h->in_pos + len > h->in_len) {
generr(h, "Malformed attribute in response");
return -1;
}
+ *lenp = len;
*value = &h->in[h->in_pos];
- h->in_pos += *len;
+ h->in_pos += len;
return type;
}
|