Skip to content

Commit

Permalink
fix decode out-of-bounds
Browse files Browse the repository at this point in the history
on hash keys without :
security: could leak secrets.
Fixes GH 208
  • Loading branch information
rurban committed Feb 21, 2023
1 parent 1d12f00 commit 41f3239
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions XS.xs
Expand Up @@ -2724,6 +2724,8 @@ decode_comment (dec_t *dec)
INLINE void
decode_ws (dec_t *dec)
{
if (dec->cur >= dec->end)
return;
for (;;)
{
char ch = *dec->cur;
Expand All @@ -2750,7 +2752,7 @@ decode_ws (dec_t *dec)

#define ERR(reason) SB dec->err = reason; goto fail; SE

#define EXPECT_CH(ch) SB \
#define EXPECT_CH(ch) SB \
if (*dec->cur != ch) \
ERR (# ch " expected"); \
++dec->cur; \
Expand Down Expand Up @@ -3923,7 +3925,8 @@ decode_hv (pTHX_ dec_t *dec, SV *typesv)
}
} // else overwrite it below
}
decode_ws (dec); EXPECT_CH (':');
decode_ws (dec);
EXPECT_CH (':');
decode_ws (dec);

if (typesv)
Expand Down Expand Up @@ -3995,7 +3998,10 @@ decode_hv (pTHX_ dec_t *dec, SV *typesv)
}

dec->cur = p + 1;
decode_ws (dec); if (*p != ':') EXPECT_CH (':');
if (dec->cur >= dec->end)
EXPECT_CH (':');
decode_ws (dec);
if (*p != ':') EXPECT_CH (':');
decode_ws (dec);

if (typesv)
Expand Down Expand Up @@ -4026,6 +4032,10 @@ decode_hv (pTHX_ dec_t *dec, SV *typesv)
break;
}
++p;
if (p > dec->end) {
dec->cur = p;
EXPECT_CH (':');
}
}
}

Expand Down Expand Up @@ -4485,11 +4495,16 @@ decode_json (pTHX_ SV *string, JSON *json, STRLEN *offset_return, SV *typesv)
if (!sv)
{
SV *uni = sv_newmortal ();

COP cop = *PL_curcop;
if (dec.cur >= dec.end) // overshoot
{
croak ("%s, at character offset %d",
dec.err,
(int)ptr_to_index (aTHX_ string, dec.cur - SvPVX(string)));
}
#if PERL_VERSION >= 8
/* horrible hack to silence warning inside pv_uni_display */
/* TODO: Can be omitted with newer perls */
COP cop = *PL_curcop;
cop.cop_warnings = pWARN_NONE;
ENTER;
SAVEVPTR (PL_curcop);
Expand All @@ -4500,7 +4515,7 @@ decode_json (pTHX_ SV *string, JSON *json, STRLEN *offset_return, SV *typesv)
croak ("%s, at character offset %d (before \"%s\")",
dec.err,
(int)ptr_to_index (aTHX_ string, dec.cur - SvPVX(string)),
dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
dec.cur < dec.end ? SvPV_nolen (uni) : "(end of string)");
}

if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref(aTHX_ sv)) {
Expand Down

0 comments on commit 41f3239

Please sign in to comment.