Skip to contentSkip to conversation

potential integer overflow in xenbus_file_write()

DISCUSSION Dan Carpenter 3 participants 5 messages Sep 13, 2012 – Oct 16, 2012 virtio core

lore
Share & tools
lore
#0 lore raw

Hi,

I was reading some code and had a question in xenbus_file_write()

drivers/xen/xenbus/xenbus_dev_frontend.c

   461          if ((len + u->len) > sizeof(u->u.buffer)) {
                     ^^^^^^^^^^^^

Can this addition overflow? Should the test be something like:

	if (len > sizeof(u->u.buffer) || len + u->len > sizeof(u->u.buffer)) {
   462                  /* On error, dump existing buffer */
   463                  u->len = 0;
   464                  rc = -EINVAL;
   465                  goto out;

466 }
467
468 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
469

regards,
dan carpenter

#1 lore raw
Quoted 1 line · Dan Carpenter

Hi,

Thanks Dan. I'm not sure anyone from Xen-land really monitors virtualization@. Adding xen-devel and Konrad.

Quoted 7 lines

I was reading some code and had a question in xenbus_file_write()

drivers/xen/xenbus/xenbus_dev_frontend.c

   461          if ((len + u->len) > sizeof(u->u.buffer)) {
                     ^^^^^^^^^^^^

Can this addition overflow?

len is a size_t and u->len is an unsigned int, so I expect so.

Quoted 3 lines

Should the test be something like:

	if (len > sizeof(u->u.buffer) || len + u->len > sizeof(u->u.buffer)) {

I think that would do it.

Ian.

Quoted 16 lines
   462                  /* On error, dump existing buffer */
   463                  u->len = 0;
   464                  rc = -EINVAL;
   465                  goto out;
   466          }
   467  
   468          ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
   469  

regards,
dan carpenter
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 1 line · Dan Carpenter

Hi,

Thanks Dan. I'm not sure anyone from Xen-land really monitors virtualization@. Adding xen-devel and Konrad.

Quoted 7 lines

I was reading some code and had a question in xenbus_file_write()

drivers/xen/xenbus/xenbus_dev_frontend.c

   461          if ((len + u->len) > sizeof(u->u.buffer)) {
                     ^^^^^^^^^^^^

Can this addition overflow?

Quoted 7 lines

len is a size_t and u->len is an unsigned int, so I expect so.

Quoted 3 lines

Should the test be something like:

	if (len > sizeof(u->u.buffer) || len + u->len > sizeof(u->u.buffer)) {

I think that would do it.

Actually, it can remain a single range check:

	if (len > sizeof(u->u.buffer) - u->len) {

because the rest of the code guarantees u->len to be less or equal to sizeof(u->u.buffer) at all times.

Jan

#3 lore raw
In reply to Ian Campbell
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 1 line · Dan Carpenter

Hi,

Thanks Dan. I'm not sure anyone from Xen-land really monitors virtualization@. Adding xen-devel and Konrad.

Quoted 7 lines

I was reading some code and had a question in xenbus_file_write()

drivers/xen/xenbus/xenbus_dev_frontend.c

   461          if ((len + u->len) > sizeof(u->u.buffer)) {
                     ^^^^^^^^^^^^

Can this addition overflow?

Quoted 7 lines

len is a size_t and u->len is an unsigned int, so I expect so.

Quoted 3 lines

Should the test be something like:

	if (len > sizeof(u->u.buffer) || len + u->len > sizeof(u->u.buffer)) {

I think that would do it.

Actually, it can remain a single range check:

	if (len > sizeof(u->u.buffer) - u->len) {

because the rest of the code guarantees u->len to be less or equal to sizeof(u->u.buffer) at all times.

Jan

#4 lore raw
In reply to Dan Carpenter
Quoted 1 line · Dan Carpenter

Hi,

Thanks Dan. I'm not sure anyone from Xen-land really monitors virtualization@. Adding xen-devel and Konrad.

Quoted 7 lines

I was reading some code and had a question in xenbus_file_write()

drivers/xen/xenbus/xenbus_dev_frontend.c

   461          if ((len + u->len) > sizeof(u->u.buffer)) {
                     ^^^^^^^^^^^^

Can this addition overflow?

len is a size_t and u->len is an unsigned int, so I expect so.

Quoted 3 lines

Should the test be something like:

	if (len > sizeof(u->u.buffer) || len + u->len > sizeof(u->u.buffer)) {

I think that would do it.

Ian.

Quoted 16 lines
   462                  /* On error, dump existing buffer */
   463                  u->len = 0;
   464                  rc = -EINVAL;
   465                  goto out;
   466          }
   467  
   468          ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
   469  

regards,
dan carpenter
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization