Re: smtpd: LINE_MAX might not be enough for a response

2021-11-24 Thread Jan Stary
On Nov 25 01:38:35, h...@stare.cz wrote:
> On Nov 24 14:47:48, j...@maudlin.dev wrote:
> > 
> > Jan Stary  writes:
> > > smtpd just failed to parse a SMTP response (below),
> > > saying 'line too long'.
> > >
> > > Looking at the source, this seems to be parse_smtp_response() in util.c,
> > > which errors out right away with
> > >
> > >   if (len >= LINE_MAX)
> > >   return "line too long";
> > >
> > > Apparently,
> > >
> > >   /usr/include/sys/syslimits.h:#define  LINE_MAX 2048
> > >
> > > is not enough, as shown by this line from
> > > (you guessed it) outlook.office365.com:
> > 
> > > There is also
> > >
> > >   smtp_session.c:#define  SMTP_LINE_MAX  65535
> > 
> > > Index: util.c
> > > ===
> > > RCS file: /cvs/src/usr.sbin/smtpd/util.c,v
> > > retrieving revision 1.154
> > > diff -u -p -r1.154 util.c
> > > --- util.c14 Jun 2021 17:58:16 -  1.154
> > > +++ util.c24 Nov 2021 17:59:14 -
> > > @@ -685,9 +685,6 @@ session_socket_error(int fd)
> > >  const char *
> > >  parse_smtp_response(char *line, size_t len, char **msg, int *cont)
> > >  {
> > > - if (len >= LINE_MAX)
> > > - return "line too long";
> > > -
> > >   if (len > 3) {
> > >   if (msg)
> > >   *msg = line + 4;
> > 
> > [...]

> > At this point it seems to me that changing LINE_MAX to SMTP_LINE_MAX
> > would require a bit more digging, and I didn't look into any RFCs for
> > whether this is a standard value that outlook is ignoring.

https://datatracker.ietf.org/doc/html/rfc5321#section-4.5.3.1
https://datatracker.ietf.org/doc/html/rfc5321#section-4.5.3.1.5

I have no idea what the server is trying to say
with the long reply.

Jan



Re: smtpd: LINE_MAX might not be enough for a response

2021-11-24 Thread Jan Stary
On Nov 24 14:47:48, j...@maudlin.dev wrote:
> 
> Jan Stary  writes:
> > smtpd just failed to parse a SMTP response (below),
> > saying 'line too long'.
> >
> > Looking at the source, this seems to be parse_smtp_response() in util.c,
> > which errors out right away with
> >
> > if (len >= LINE_MAX)
> > return "line too long";
> >
> > Apparently,
> >
> > /usr/include/sys/syslimits.h:#define  LINE_MAX 2048
> >
> > is not enough, as shown by this line from
> > (you guessed it) outlook.office365.com:
> 
> > There is also
> >
> > smtp_session.c:#define  SMTP_LINE_MAX  65535
> 
> > Index: util.c
> > ===
> > RCS file: /cvs/src/usr.sbin/smtpd/util.c,v
> > retrieving revision 1.154
> > diff -u -p -r1.154 util.c
> > --- util.c  14 Jun 2021 17:58:16 -  1.154
> > +++ util.c  24 Nov 2021 17:59:14 -
> > @@ -685,9 +685,6 @@ session_socket_error(int fd)
> >  const char *
> >  parse_smtp_response(char *line, size_t len, char **msg, int *cont)
> >  {
> > -   if (len >= LINE_MAX)
> > -   return "line too long";
> > -
> > if (len > 3) {
> > if (msg)
> > *msg = line + 4;
> 
> We really shouldn't simply remove this check.

Absolutely, that was just to see what the long line was.
Thanks for digging in.

Jan


> In this case parse_smtp_response gets called in two places: 
> 
> * 
> https://github.com/openbsd/src/blob/ceeebefe3564938ff6ba87e7f465f6c35aeb8b03/usr.sbin/smtpd/bounce.c#L726
> * 
> https://github.com/openbsd/src/blob/ceeebefe3564938ff6ba87e7f465f6c35aeb8b03/usr.sbin/smtpd/mta_session.c#L1252
> 
> I think the correct thing to do in this case is examine how 'char *line'
> is allocated, which should help to determine if SMTP_LINE_MAX would in
> fact be a suitable replacement for LINE_MAX.
> 
> Digging a little further, it appears that in usr.sbin/smtpd/bounce.c:711
> we see that LINE_MAX is used for comparison before later calling
> parse_smtp_response with char *line and size_t len:
> 
>   case IO_DATAIN:
>   nextline:
>   line = io_getline(s->io, );
>   if (line == NULL && io_datalen(s->io) >= LINE_MAX) {
>   bounce_status(s, "Input too long");
>   bounce_free(s);
>   return;
>   }
> 
> At this point it seems to me that changing LINE_MAX to SMTP_LINE_MAX
> would require a bit more digging, and I didn't look into any RFCs for
> whether this is a standard value that outlook is ignoring.
> 
> 



smtpd: LINE_MAX might not be enough for a response

2021-11-24 Thread Jan Stary
This is current/amd64 on a PC.

smtpd just failed to parse a SMTP response (below),
saying 'line too long'.

Looking at the source, this seems to be parse_smtp_response() in util.c,
which errors out right away with

if (len >= LINE_MAX)
return "line too long";

Apparently,

/usr/include/sys/syslimits.h:#define  LINE_MAX 2048

is not enough, as shown by this line from
(you guessed it) outlook.office365.com:

h...@stare.cz: 554 5.2.252 SendAsDenied; stary...@cvut.cz not allowed to send 
as h...@biblio.stare.cz; 
STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; 
Failed to process message due to a permanent exception with message Cannot 
submit message. 0.35250:CF3A, 1.36674:0100, 1.61250:, 
1.45378:0200, 1.44866:, 1.36674:7A00, 1.61250:, 
1.45378:0500, 1.44866:0014, 1.36674:0A00, 1.61250:, 
1.45378:1600, 1.44866:AE1A, 1.36674:0E00, 1.61250:, 
1.45378:C71A, 1.44866:E000, 16.55847:EE0C, 
17.43559:30020100, 
20.52176:140F69921A001F001432, 20.50032:140F69928A1770200201E265, 
0.35180:40001E32, 255.23226:2100, 255.27962:7A00, 255.27962:0A00, 
255.27962:0E00, 255.31418:03000136, 0.35250:0300A531, 1.36674:0A00, 
1.61250:, 1.45378:0200, 1.44866:1800, 1.36674:3200, 
1.61250:, 1.45378:1D00, 1.44866:0100, 16.55847:8400, 
17.43559:8803, 
20.52176:140F69921A0070200A001380, 20.50032:140F69928A1710106F00, 
0.35180:03000B67, 255.23226:0A00CD30, 255.27962:0A00, 255.27962:3200, 
255.17082:DC04, 0.27745:7900, 4.21921:DC04, 255.27962:FA00, 
255.1494:7E00, 0.38698:05000780, 1.41134:4600, 0.37692:8600, 
0.37948:8600, 5.33852:534D5450, 
7.36354:010001098600, 4.56248:DC04, 
7.40748:0100010B8600, 7.57132:8600, 
1.63016:3200, 4.39640:DC04, 
8.45434:9A6929EA0096114FBBA64268B1990A848600, 1.46798:0400, 
5.10786:31352E32302E343731332E3032363A414D39505230364D42383138303A65613539613834342D653163302D343234332D613934632D3263343563353536646533383A34353639323400DA10100300,
 7.51330:8C34EA6973AFD9080700, 0.39570:0300, 1.55954:0A00, 
0.49266:0200, 1.33010:0A00, 2.54258:, 0.40002:7A00, 
1.56562:, 1.64146:3200, 1.33010:3200, 2.54258:DC04, 
255.1750:0300

Yes, this is exactly 2048 bytes of an unfinished line.

(The actual error is my misconfiguration,
but that's beside the point here.)

There is also

smtp_session.c:#define  SMTP_LINE_MAX  65535

which I guess would be the right replacement, but as this is
is only defined in one C file and I don't know the code base,
I'll leave that to someone else.

Naively, I am just removing the len >= condition,
which is probably not correct; I don't see where the
parsing function needs that, but perhaps this limitation
is relied upon elsewhere.

Jan



Index: util.c
===
RCS file: /cvs/src/usr.sbin/smtpd/util.c,v
retrieving revision 1.154
diff -u -p -r1.154 util.c
--- util.c  14 Jun 2021 17:58:16 -  1.154
+++ util.c  24 Nov 2021 17:59:14 -
@@ -685,9 +685,6 @@ session_socket_error(int fd)
 const char *
 parse_smtp_response(char *line, size_t len, char **msg, int *cont)
 {
-   if (len >= LINE_MAX)
-   return "line too long";
-
if (len > 3) {
if (msg)
*msg = line + 4;