Assuming the following:
- side A configures the n_gsm in basic option mode
- side B sends the header of a basic option mode frame with data length 1
- side A switches to advanced option mode
- side B sends 2 data bytes which exceeds gsm->len
  Reason: gsm->len is not used in advanced option mode.
- side A switches to basic option mode
- side B keeps sending until gsm0_receive() writes past gsm->buf
  Reason: Neither gsm->state nor gsm->len have been reset after
  reconfiguration.
Fix this by changing gsm->count to gsm->len comparison from equal to less
than. Also add upper limit checks against the constant MAX_MRU in
gsm0_receive() and gsm1_receive() to harden against memory corruption of
gsm->len and gsm->mru.
All other checks remain as we still need to limit the data according to the
user configuration and actual payload size.
Reported-by: j51569436@gmail.com
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218708
Tested-by: j51569436@gmail.com
Fixes: e1eaea46bb40 ("tty: n_gsm line discipline")
Cc: stable@vger.kernel.org
Signed-off-by: Daniel Starke <daniel.starke@siemens.com>
Link: https://lore.kernel.org/r/20240424054842.7741-1-daniel.starke@siemens.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
                break;
        case GSM_DATA:          /* Data */
                gsm->buf[gsm->count++] = c;
-               if (gsm->count == gsm->len) {
+               if (gsm->count >= MAX_MRU) {
+                       gsm->bad_size++;
+                       gsm->state = GSM_SEARCH;
+               } else if (gsm->count >= gsm->len) {
                        /* Calculate final FCS for UI frames over all data */
                        if ((gsm->control & ~PF) != UIH) {
                                gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
                gsm->state = GSM_DATA;
                break;
        case GSM_DATA:          /* Data */
-               if (gsm->count > gsm->mru) {    /* Allow one for the FCS */
+               if (gsm->count > gsm->mru || gsm->count > MAX_MRU) {    /* Allow one for the FCS */
                        gsm->state = GSM_OVERRUN;
                        gsm->bad_size++;
                } else