Skip to content

Commit 1250c30

Browse files
committed
IB/uverbs: Handle IDR and FD types without truncation
Our ABI for write() uses a s32 for FDs and a u32 for IDRs, but internally we ended up implicitly casting these ABI values into an 'int'. For ioctl() we use a s64 for FDs and a u64 for IDRs, again casting to an int. The various casts to int are all missing range checks which can cause userspace values that should be considered invalid to be accepted. Fix this by making the generic lookup routine accept a s64, which does not truncate the write API's u32/s32 or the ioctl API's s64. Then push the detailed range checking down to the actual type implementations to be shared by both interfaces. Finally, change the copy of the uobj->id to sign extend into a s64, so eg, if we ever wish to return a negative value for a FD it is carried properly. This ensures that userspace values are never weirdly interpreted due to the various trunctations and everything that is really out of range gets an EINVAL. Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
1 parent 3df593b commit 1250c30

7 files changed

Lines changed: 59 additions & 38 deletions

File tree

‎drivers/infiniband/core/rdma_core.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static int uverbs_try_lock_object(struct ib_uobject *uobj, bool exclusive)
133133
* returns success_res on success (negative errno on failure). For use by
134134
* callers that do not need the uobj.
135135
*/
136-
int __uobj_perform_destroy(const struct uverbs_obj_type *type, int id,
136+
int __uobj_perform_destroy(const struct uverbs_obj_type *type, u32 id,
137137
struct ib_uverbs_file *ufile, int success_res)
138138
{
139139
struct ib_uobject *uobj;
@@ -212,13 +212,17 @@ static void uverbs_idr_remove_uobj(struct ib_uobject *uobj)
212212
/* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
213213
static struct ib_uobject *
214214
lookup_get_idr_uobject(const struct uverbs_obj_type *type,
215-
struct ib_uverbs_file *ufile, int id, bool exclusive)
215+
struct ib_uverbs_file *ufile, s64 id, bool exclusive)
216216
{
217217
struct ib_uobject *uobj;
218+
unsigned long idrno = id;
219+
220+
if (id < 0 || id > ULONG_MAX)
221+
return ERR_PTR(-EINVAL);
218222

219223
rcu_read_lock();
220224
/* object won't be released as we're protected in rcu */
221-
uobj = idr_find(&ufile->idr, id);
225+
uobj = idr_find(&ufile->idr, idrno);
222226
if (!uobj) {
223227
uobj = ERR_PTR(-ENOENT);
224228
goto free;
@@ -240,17 +244,21 @@ lookup_get_idr_uobject(const struct uverbs_obj_type *type,
240244

241245
static struct ib_uobject *lookup_get_fd_uobject(const struct uverbs_obj_type *type,
242246
struct ib_uverbs_file *ufile,
243-
int id, bool exclusive)
247+
s64 id, bool exclusive)
244248
{
245249
struct file *f;
246250
struct ib_uobject *uobject;
251+
int fdno = id;
247252
const struct uverbs_obj_fd_type *fd_type =
248253
container_of(type, struct uverbs_obj_fd_type, type);
249254

255+
if (fdno != id)
256+
return ERR_PTR(-EINVAL);
257+
250258
if (exclusive)
251259
return ERR_PTR(-EOPNOTSUPP);
252260

253-
f = fget(id);
261+
f = fget(fdno);
254262
if (!f)
255263
return ERR_PTR(-EBADF);
256264

@@ -270,7 +278,7 @@ static struct ib_uobject *lookup_get_fd_uobject(const struct uverbs_obj_type *ty
270278
}
271279

272280
struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
273-
struct ib_uverbs_file *ufile, int id,
281+
struct ib_uverbs_file *ufile, s64 id,
274282
bool exclusive)
275283
{
276284
struct ib_uobject *uobj;
@@ -725,7 +733,7 @@ EXPORT_SYMBOL(uverbs_fd_class);
725733
struct ib_uobject *
726734
uverbs_get_uobject_from_file(const struct uverbs_obj_type *type_attrs,
727735
struct ib_uverbs_file *ufile,
728-
enum uverbs_obj_access access, int id)
736+
enum uverbs_obj_access access, s64 id)
729737
{
730738
switch (access) {
731739
case UVERBS_ACCESS_READ:

‎drivers/infiniband/core/rdma_core.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void uverbs_close_fd(struct file *f);
8686
struct ib_uobject *
8787
uverbs_get_uobject_from_file(const struct uverbs_obj_type *type_attrs,
8888
struct ib_uverbs_file *ufile,
89-
enum uverbs_obj_access access, int id);
89+
enum uverbs_obj_access access, s64 id);
9090

9191
/*
9292
* Note that certain finalize stages could return a status:

‎drivers/infiniband/core/uverbs_cmd.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848
#include "core_priv.h"
4949

5050
static struct ib_uverbs_completion_event_file *
51-
ib_uverbs_lookup_comp_file(int fd, struct ib_uverbs_file *ufile)
51+
_ib_uverbs_lookup_comp_file(s32 fd, struct ib_uverbs_file *ufile)
5252
{
53-
struct ib_uobject *uobj = uobj_get_read(UVERBS_OBJECT_COMP_CHANNEL,
54-
fd, ufile);
53+
struct ib_uobject *uobj = ufd_get_read(UVERBS_OBJECT_COMP_CHANNEL,
54+
fd, ufile);
5555

5656
if (IS_ERR(uobj))
5757
return (void *)uobj;
@@ -62,6 +62,8 @@ ib_uverbs_lookup_comp_file(int fd, struct ib_uverbs_file *ufile)
6262
return container_of(uobj, struct ib_uverbs_completion_event_file,
6363
uobj);
6464
}
65+
#define ib_uverbs_lookup_comp_file(_fd, _ufile) \
66+
_ib_uverbs_lookup_comp_file((_fd)*typecheck(s32, _fd), _ufile)
6567

6668
ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
6769
struct ib_device *ib_dev,

‎drivers/infiniband/core/uverbs_ioctl.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,35 @@ static int uverbs_process_attr(struct ib_uverbs_file *ufile,
136136
break;
137137

138138
case UVERBS_ATTR_TYPE_IDR:
139-
if (uattr->data >> 32)
140-
return -EINVAL;
141-
/* fall through */
142139
case UVERBS_ATTR_TYPE_FD:
143140
if (uattr->attr_data.reserved)
144141
return -EINVAL;
145142

146-
if (uattr->len != 0 || !ufile->ucontext ||
147-
uattr->data > INT_MAX)
143+
if (uattr->len != 0 || !ufile->ucontext)
148144
return -EINVAL;
149145

150146
o_attr = &e->obj_attr;
151147
object = uverbs_get_object(ufile, spec->u.obj.obj_type);
152148
if (!object)
153149
return -EINVAL;
154150

151+
/*
152+
* The type of uattr->data is u64 for UVERBS_ATTR_TYPE_IDR and
153+
* s64 for UVERBS_ATTR_TYPE_FD. We can cast the u64 to s64
154+
* here without caring about truncation as we know that the
155+
* IDR implementation today rejects negative IDs
156+
*/
155157
o_attr->uobject = uverbs_get_uobject_from_file(
156158
object->type_attrs,
157159
ufile,
158160
spec->u.obj.access,
159-
(int)uattr->data);
161+
uattr->data_s64);
160162

161163
if (IS_ERR(o_attr->uobject))
162164
return PTR_ERR(o_attr->uobject);
163165

164166
if (spec->u.obj.access == UVERBS_ACCESS_NEW) {
165-
u64 id = o_attr->uobject->id;
167+
s64 id = o_attr->uobject->id;
166168

167169
/* Copy the allocated id to the user-space */
168170
if (put_user(id, &e->uattr->data)) {

‎include/rdma/uverbs_std_types.h‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,43 @@ static inline const struct uverbs_object_tree_def *uverbs_default_get_objects(vo
4646
}
4747
#endif
4848

49-
static inline struct ib_uobject *__uobj_get(const struct uverbs_obj_type *type,
50-
bool write,
51-
struct ib_uverbs_file *ufile,
52-
int id)
53-
{
54-
return rdma_lookup_get_uobject(type, ufile, id, write);
55-
}
49+
/* Returns _id, or causes a compile error if _id is not a u32.
50+
*
51+
* The uobj APIs should only be used with the write based uAPI to access
52+
* object IDs. The write API must use a u32 for the object handle, which is
53+
* checked by this macro.
54+
*/
55+
#define _uobj_check_id(_id) ((_id) * typecheck(u32, _id))
5656

5757
#define uobj_get_type(_object) UVERBS_OBJECT(_object).type_attrs
5858

5959
#define uobj_get_read(_type, _id, _ufile) \
60-
__uobj_get(uobj_get_type(_type), false, _ufile, _id)
60+
rdma_lookup_get_uobject(uobj_get_type(_type), _ufile, \
61+
_uobj_check_id(_id), false)
6162

62-
static inline void *_uobj_get_obj_read(const struct uverbs_obj_type *type,
63-
int id, struct ib_uverbs_file *ufile)
64-
{
65-
struct ib_uobject *uobj = __uobj_get(type, false, ufile, id);
63+
#define ufd_get_read(_type, _fdnum, _ufile) \
64+
rdma_lookup_get_uobject(uobj_get_type(_type), _ufile, \
65+
(_fdnum)*typecheck(s32, _fdnum), false)
6666

67+
static inline void *_uobj_get_obj_read(struct ib_uobject *uobj)
68+
{
6769
if (IS_ERR(uobj))
6870
return NULL;
6971
return uobj->object;
7072
}
7173
#define uobj_get_obj_read(_object, _type, _id, _ufile) \
72-
((struct ib_##_object *)_uobj_get_obj_read(uobj_get_type(_type), _id, \
73-
_ufile))
74+
((struct ib_##_object *)_uobj_get_obj_read( \
75+
uobj_get_read(_type, _id, _ufile)))
7476

7577
#define uobj_get_write(_type, _id, _ufile) \
76-
__uobj_get(uobj_get_type(_type), true, _ufile, _id)
78+
rdma_lookup_get_uobject(uobj_get_type(_type), _ufile, \
79+
_uobj_check_id(_id), true)
7780

78-
int __uobj_perform_destroy(const struct uverbs_obj_type *type, int id,
81+
int __uobj_perform_destroy(const struct uverbs_obj_type *type, u32 id,
7982
struct ib_uverbs_file *ufile, int success_res);
8083
#define uobj_perform_destroy(_type, _id, _ufile, _success_res) \
81-
__uobj_perform_destroy(uobj_get_type(_type), _id, _ufile, _success_res)
84+
__uobj_perform_destroy(uobj_get_type(_type), _uobj_check_id(_id), \
85+
_ufile, _success_res)
8286

8387
static inline void uobj_put_read(struct ib_uobject *uobj)
8488
{

‎include/rdma/uverbs_types.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct uverbs_obj_type_class {
7777
void (*alloc_abort)(struct ib_uobject *uobj);
7878

7979
struct ib_uobject *(*lookup_get)(const struct uverbs_obj_type *type,
80-
struct ib_uverbs_file *ufile, int id,
80+
struct ib_uverbs_file *ufile, s64 id,
8181
bool exclusive);
8282
void (*lookup_put)(struct ib_uobject *uobj, bool exclusive);
8383
/*
@@ -121,7 +121,7 @@ struct uverbs_obj_idr_type {
121121

122122
struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
123123
struct ib_uverbs_file *ufile,
124-
int id, bool exclusive);
124+
s64 id, bool exclusive);
125125
void rdma_lookup_put_uobject(struct ib_uobject *uobj, bool exclusive);
126126
struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
127127
struct ib_uverbs_file *ufile);

‎include/uapi/rdma/rdma_user_ioctl_cmds.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ struct ib_uverbs_attr {
6262
} enum_data;
6363
__u16 reserved;
6464
} attr_data;
65-
__aligned_u64 data; /* ptr to command, inline data or idr/fd */
65+
union {
66+
/* Used by PTR_IN/OUT, ENUM_IN and IDR */
67+
__aligned_u64 data;
68+
/* Used by FD_IN and FD_OUT */
69+
__s64 data_s64;
70+
};
6671
};
6772

6873
struct ib_uverbs_ioctl_hdr {

0 commit comments

Comments
 (0)