Skip to content

Commit 2c28ae4

Browse files
jmberg-inteldavem330
authored andcommitted
netlink: factor out policy range helpers
Add helpers to get the policy's signed/unsigned range validation data. Signed-off-by: Johannes Berg <johannes.berg@intel.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c7721c0 commit 2c28ae4

2 files changed

Lines changed: 79 additions & 21 deletions

File tree

‎include/net/netlink.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,4 +1928,9 @@ static inline bool nla_is_last(const struct nlattr *nla, int rem)
19281928
return nla->nla_len == rem;
19291929
}
19301930

1931+
void nla_get_range_unsigned(const struct nla_policy *pt,
1932+
struct netlink_range_validation *range);
1933+
void nla_get_range_signed(const struct nla_policy *pt,
1934+
struct netlink_range_validation_signed *range);
1935+
19311936
#endif

‎lib/nlattr.c‎

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,58 @@ static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
111111
return 0;
112112
}
113113

114-
static int nla_validate_int_range_unsigned(const struct nla_policy *pt,
115-
const struct nlattr *nla,
116-
struct netlink_ext_ack *extack)
114+
void nla_get_range_unsigned(const struct nla_policy *pt,
115+
struct netlink_range_validation *range)
117116
{
118-
struct netlink_range_validation _range = {
119-
.min = 0,
120-
.max = U64_MAX,
121-
}, *range = &_range;
122-
u64 value;
123-
124117
WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR &&
125118
(pt->min < 0 || pt->max < 0));
126119

120+
range->min = 0;
121+
122+
switch (pt->type) {
123+
case NLA_U8:
124+
range->max = U8_MAX;
125+
break;
126+
case NLA_U16:
127+
range->max = U16_MAX;
128+
break;
129+
case NLA_U32:
130+
range->max = U32_MAX;
131+
break;
132+
case NLA_U64:
133+
case NLA_MSECS:
134+
range->max = U64_MAX;
135+
break;
136+
default:
137+
WARN_ON_ONCE(1);
138+
return;
139+
}
140+
127141
switch (pt->validation_type) {
128142
case NLA_VALIDATE_RANGE:
129143
range->min = pt->min;
130144
range->max = pt->max;
131145
break;
132146
case NLA_VALIDATE_RANGE_PTR:
133-
range = pt->range;
147+
*range = *pt->range;
134148
break;
135149
case NLA_VALIDATE_MIN:
136150
range->min = pt->min;
137151
break;
138152
case NLA_VALIDATE_MAX:
139153
range->max = pt->max;
140154
break;
155+
default:
156+
break;
141157
}
158+
}
159+
160+
static int nla_validate_int_range_unsigned(const struct nla_policy *pt,
161+
const struct nlattr *nla,
162+
struct netlink_ext_ack *extack)
163+
{
164+
struct netlink_range_validation range;
165+
u64 value;
142166

143167
switch (pt->type) {
144168
case NLA_U8:
@@ -158,7 +182,9 @@ static int nla_validate_int_range_unsigned(const struct nla_policy *pt,
158182
return -EINVAL;
159183
}
160184

161-
if (value < range->min || value > range->max) {
185+
nla_get_range_unsigned(pt, &range);
186+
187+
if (value < range.min || value > range.max) {
162188
NL_SET_ERR_MSG_ATTR(extack, nla,
163189
"integer out of range");
164190
return -ERANGE;
@@ -167,31 +193,56 @@ static int nla_validate_int_range_unsigned(const struct nla_policy *pt,
167193
return 0;
168194
}
169195

170-
static int nla_validate_int_range_signed(const struct nla_policy *pt,
171-
const struct nlattr *nla,
172-
struct netlink_ext_ack *extack)
196+
void nla_get_range_signed(const struct nla_policy *pt,
197+
struct netlink_range_validation_signed *range)
173198
{
174-
struct netlink_range_validation_signed _range = {
175-
.min = S64_MIN,
176-
.max = S64_MAX,
177-
}, *range = &_range;
178-
s64 value;
199+
switch (pt->type) {
200+
case NLA_S8:
201+
range->min = S8_MIN;
202+
range->max = S8_MAX;
203+
break;
204+
case NLA_S16:
205+
range->min = S16_MIN;
206+
range->max = S16_MAX;
207+
break;
208+
case NLA_S32:
209+
range->min = S32_MIN;
210+
range->max = S32_MAX;
211+
break;
212+
case NLA_S64:
213+
range->min = S64_MIN;
214+
range->max = S64_MAX;
215+
break;
216+
default:
217+
WARN_ON_ONCE(1);
218+
return;
219+
}
179220

180221
switch (pt->validation_type) {
181222
case NLA_VALIDATE_RANGE:
182223
range->min = pt->min;
183224
range->max = pt->max;
184225
break;
185226
case NLA_VALIDATE_RANGE_PTR:
186-
range = pt->range_signed;
227+
*range = *pt->range_signed;
187228
break;
188229
case NLA_VALIDATE_MIN:
189230
range->min = pt->min;
190231
break;
191232
case NLA_VALIDATE_MAX:
192233
range->max = pt->max;
193234
break;
235+
default:
236+
break;
194237
}
238+
}
239+
240+
static int nla_validate_int_range_signed(const struct nla_policy *pt,
241+
const struct nlattr *nla,
242+
struct netlink_ext_ack *extack)
243+
{
244+
struct netlink_range_validation_signed range;
245+
s64 value;
195246

196247
switch (pt->type) {
197248
case NLA_S8:
@@ -210,7 +261,9 @@ static int nla_validate_int_range_signed(const struct nla_policy *pt,
210261
return -EINVAL;
211262
}
212263

213-
if (value < range->min || value > range->max) {
264+
nla_get_range_signed(pt, &range);
265+
266+
if (value < range.min || value > range.max) {
214267
NL_SET_ERR_MSG_ATTR(extack, nla,
215268
"integer out of range");
216269
return -ERANGE;

0 commit comments

Comments
 (0)