8000 py/objstringio: Prevent offset wraparound for io.BytesIO objects. · sparkfun/circuitpython@168350c · GitHub
[go: up one dir, main page]

Skip to content

Commit 168350c

Browse files
tomlogicpfalcon
authored andcommitted
py/objstringio: Prevent offset wraparound for io.BytesIO objects.
Too big positive, or too big negative offset values could lead to overflow and address space wraparound and thus access to unrelated areas of memory (a security issue).
1 parent 387a8d2 commit 168350c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

py/objstringio.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,19 @@ STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
125125
ref = o->vstr->len;
126126
break;
127127
}
128-
o->pos = ref + s->offset;
129-
s->offset = o->pos;
128+
mp_uint_t new_pos = ref + s->offset;
129+
if (s->offset < 0) {
130+
if (new_pos > ref) {
131+
// Negative offset from SEEK_CUR or SEEK_END went past 0.
132+
// CPython sets position to 0, POSIX returns an EINVAL error
133+
new_pos = 0;
134+
}
135+
} else if (new_pos < ref) {
136+
// positive offset went beyond the limit of mp_uint_t
137+
*errcode = MP_EINVAL; // replace with MP_EOVERFLOW when defined
138+
return MP_STREAM_ERROR;
139+
}
140+
s->offset = o->pos = new_pos;
130141
return 0;
131142
}
132143
case MP_STREAM_FLUSH:

0 commit comments

Comments
 (0)
0