Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sproto write_ff buffer overflow #1205

Merged
merged 2 commits into from
Jun 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions lualib-src/sproto/sproto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1262,15 +1262,15 @@ pack_seg(const uint8_t *src, uint8_t * buffer, int sz, int n) {
}

static inline void
write_ff(const uint8_t * src, uint8_t * des, int n) {
int i;
int align8_n = (n+7)&(~7);

write_ff(const uint8_t * src, const uint8_t * src_end, uint8_t * des, int n) {
des[0] = 0xff;
des[1] = align8_n/8 - 1;
memcpy(des+2, src, n);
for(i=0; i< align8_n-n; i++){
des[n+2+i] = 0;
des[1] = n - 1;
if (src + n * 8 <= src_end) {
memcpy(des+2, src, n*8);
} else {
int sz = (int)(src_end - src);
memcpy(des+2, src, sz);
memset(des+2+sz, 0, n*8-sz);
}
}

Expand All @@ -1283,6 +1283,7 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) {
int ff_n = 0;
int size = 0;
const uint8_t * src = srcv;
const uint8_t * src_end = (uint8_t *)srcv + srcsz;
uint8_t * buffer = bufferv;
for (i=0;i<srcsz;i+=8) {
int n;
Expand All @@ -1306,14 +1307,14 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) {
++ff_n;
if (ff_n == 256) {
if (bufsz >= 0) {
write_ff(ff_srcstart, ff_desstart, 256*8);
write_ff(ff_srcstart, src_end, ff_desstart, 256);
}
ff_n = 0;
}
} else {
if (ff_n > 0) {
if (bufsz >= 0) {
write_ff(ff_srcstart, ff_desstart, ff_n*8);
write_ff(ff_srcstart, src_end, ff_desstart, ff_n);
}
ff_n = 0;
}
Expand All @@ -1322,11 +1323,8 @@ sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) {
buffer += n;
size += n;
}
if(bufsz >= 0){
if(ff_n == 1)
write_ff(ff_srcstart, ff_desstart, 8);
else if (ff_n > 1)
write_ff(ff_srcstart, ff_desstart, srcsz - (intptr_t)(ff_srcstart - (const uint8_t*)srcv));
if(bufsz >= 0 && ff_n > 0) {
write_ff(ff_srcstart, src_end, ff_desstart, ff_n);
}
return size;
}
Expand Down