Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-10 Thread bm1les
Exactly my point. Even if the circumstances were contrived, I think it would 
good to fix it just for the sake of correctness.

The issue is actually a pattern I found not only in /etc/netstart but also in 
/etc/rc. (( )) cannot deal with an empty result yet it sometimes includes calls 
to sysctl which apparently can return an empty string in some cases.

So options are:

1. ensure that sysctl always returns a number where it is expected
2. work around the issue by using /bin/[ in place of or in addition to, the 
arithmetic expression (depending on the expression being tested), so that 
whatever returns empty string can be tested instead of blowing up.

P.S. To answer another poster's question, I didn't get any error from sysctl. I 
tried it manually at the prompt and I got back nothing.

Hope that helps.
‐‐‐ Original Message ‐‐‐
On Friday, October 8th, 2021 at 1:41 PM, Philip Guenther  
wrote:

> On Fri, Oct 8, 2021 at 8:57 AM Theo de Raadt  wrote:
>
>> Philip Guenther  wrote:
>>
>>> On Thu, Oct 7, 2021 at 5:57 PM bm1les  wrote:
>>>
>>> > --- netstart 2 Sep 2021 19:38:20 - 1.216
>>> > +++ netstart 8 Oct 2021 02:43:30 -
>>> > @@ -365,7 +365,7 @@ ifmstart "tun tap gif etherip gre egre p
>>> > if [[ $ip6kernel == YES ]]; then
>>> > # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
>>> > count=0
>>> > - while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) !=
>>> > 0)); do
>>> > + while ((count++ < 10 && "$(sysctl -n net.inet6.ip6.dad_pending)"
>>> > != 0)); do
>>> > sleep 1
>>> > done
>>> > fi
>>> >
>>>
>>> I can't figure out what problem you think this could solve. Can you
>>> explain the circumstances under which those quotes could make a difference?
>>
>> Not the OP's issue, but I think a kernels compiled without option INET6
>> will return an errno, and I cannot tell if sysctl prints out an error message
>> or converts to "", the empty string, which would conceivably mis-parse.
>
> AFAICT, an empty quoted string there results in the exact same error. As I 
> wrote off-list to the original submitter:
>
>> Can you be clearer about how the quoting makes the result any better when 
>> run under bsd.rd? Doesn't it fail in the same way? Testing with 'echo' 
>> instead would seem to indicate so:
>> : bleys; (( 1 < 10 && $(echo) != 0 )); echo $?
>> /bin/ksh: 1 < 10 && != 0 : unexpected `!='
>> 2
>> : bleys; (( 1 < 10 && $(echo -n) != 0 )); echo $?
>> /bin/ksh: 1 < 10 && != 0 : unexpected `!='
>> 2
>> : bleys; (( 1 < 10 && "$(echo)" != 0 )); echo $?
>> /bin/ksh: 1 < 10 && != 0 : unexpected `!='
>> 2
>> : bleys; (( 1 < 10 && "$(echo -n)" != 0 )); echo $?
>> /bin/ksh: 1 < 10 && != 0 : unexpected `!='
>> 2
>> : bleys;
>
> Philip

Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-10 Thread Philip Guenther
On Sun, Oct 10, 2021 at 1:48 PM bm1les  wrote:

> Exactly my point. Even if the circumstances were contrived, I think it
> would good to fix it just for the sake of correctness.
>

Sure, knowing what circumstances could cause a problem assists in
achieving correctness.



> The issue is actually a pattern I found not only in /etc/netstart but also
> in /etc/rc. (( )) cannot deal with an empty result yet it sometimes
> includes calls to sysctl which apparently can return an empty string in
> some cases.
>
> So options are:
>
> 1. ensure that sysctl always returns a number where it is expected
> 2. work around the issue by using /bin/[ in place of or in addition to,
> the arithmetic expression (depending on the expression being tested), so
> that whatever returns empty string can be tested instead of blowing up.
>

3. use syntax with works when the expansion is empty, such as
  (( $(sysctl blah) + 0 != 0 ))

   which is unary plus when it's empty, or
   (( $(sysctl blah)0 != 0 ))

   which multiplies the value by 10 when not empty and is zero when it's
empty.


Philip Guenther


(Per POSIX rules, any arithmetic expression is effectively evaluated in
double-quotes.  If you follow the spec from there, you can work out that if
an arithmetic expression that directly** contains double-quotes parses
correctly, then it must also parse correctly with the double-quotes
removed; adding double-quotes can't fix an arithmetic expression.  The
reverse is not true: adding double-quotes can break the parsing.  ** i.e.,
not counting quotes inside a nested parameter expansion or command
substitution.)


Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-08 Thread Theo de Raadt
Philip Guenther  wrote:

> On Fri, Oct 8, 2021 at 8:57 AM Theo de Raadt  wrote:
> 
>  Philip Guenther  wrote:
> 
>  > On Thu, Oct 7, 2021 at 5:57 PM bm1les  wrote:
>  > 
>  > > --- netstart2 Sep 2021 19:38:20 -   1.216
>  > > +++ netstart8 Oct 2021 02:43:30 -
>  > > @@ -365,7 +365,7 @@ ifmstart "tun tap gif etherip gre egre p
>  > >  if [[ $ip6kernel == YES ]]; then
>  > > # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
>  > > count=0
>  > > -   while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) !=
>  > > 0)); do
>  > > +   while ((count++ < 10 && "$(sysctl -n net.inet6.ip6.dad_pending)"
>  > > != 0)); do
>  > > sleep 1
>  > > done
>  > >  fi
>  > >
>  > 
>  > I can't figure out what problem you think this could solve.  Can you
>  > explain the circumstances under which those quotes could make a difference?
> 
>  Not the OP's issue, but I think a kernels compiled without option INET6
>  will return an errno, and I cannot tell if sysctl prints out an error message
>  or converts to "", the empty string, which would conceivably mis-parse.
> 
> AFAICT, an empty quoted string there results in the exact same error.  As I 
> wrote
> off-list to the original submitter:
> 
>  Can you be clearer about how the quoting makes the result any better when run
>  under bsd.rd?  Doesn't it fail in the same way?  Testing with 'echo' instead 
> would
>  seem to indicate so:
>  : bleys; (( 1 < 10 && $(echo) != 0 )); echo $?  
>  /bin/ksh:  1 < 10 &&  != 0 : unexpected `!='
>  2
>  : bleys; (( 1 < 10 && $(echo -n) != 0 )); echo $?  
>  /bin/ksh:  1 < 10 &&  != 0 : unexpected `!='
>  2
>  : bleys; (( 1 < 10 && "$(echo)" != 0 )); echo $?
>  /bin/ksh:  1 < 10 &&  != 0 : unexpected `!='
>  2
>  : bleys; (( 1 < 10 && "$(echo -n)" != 0 )); echo $?
>  /bin/ksh:  1 < 10 &&  != 0 : unexpected `!='
>  2
>  : bleys;

Well, netstart can do better, and should not emit low-level parsing errors



Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-08 Thread Philip Guenther
On Fri, Oct 8, 2021 at 8:57 AM Theo de Raadt  wrote:

> Philip Guenther  wrote:
>
> > On Thu, Oct 7, 2021 at 5:57 PM bm1les  wrote:
> >
> > > --- netstart2 Sep 2021 19:38:20 -   1.216
> > > +++ netstart8 Oct 2021 02:43:30 -
> > > @@ -365,7 +365,7 @@ ifmstart "tun tap gif etherip gre egre p
> > >  if [[ $ip6kernel == YES ]]; then
> > > # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
> > > count=0
> > > -   while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending)
> !=
> > > 0)); do
> > > +   while ((count++ < 10 && "$(sysctl -n
> net.inet6.ip6.dad_pending)"
> > > != 0)); do
> > > sleep 1
> > > done
> > >  fi
> > >
> >
> > I can't figure out what problem you think this could solve.  Can you
> > explain the circumstances under which those quotes could make a
> difference?
>
> Not the OP's issue, but I think a kernels compiled without option INET6
> will return an errno, and I cannot tell if sysctl prints out an error
> message
> or converts to "", the empty string, which would conceivably mis-parse.
>

AFAICT, an empty quoted string there results in the exact same error.  As I
wrote off-list to the original submitter:

Can you be clearer about how the quoting makes the result any better when
> run under bsd.rd?  Doesn't it fail in the same way?  Testing with 'echo'
> instead would seem to indicate so:
> : bleys; (( 1 < 10 && $(echo) != 0 )); echo $?
> /bin/ksh:  1 < 10 &&  != 0 : unexpected `!='
> 2
> : bleys; (( 1 < 10 && $(echo -n) != 0 )); echo $?
> /bin/ksh:  1 < 10 &&  != 0 : unexpected `!='
> 2
> : bleys; (( 1 < 10 && "$(echo)" != 0 )); echo $?
> /bin/ksh:  1 < 10 &&  != 0 : unexpected `!='
> 2
> : bleys; (( 1 < 10 && "$(echo -n)" != 0 )); echo $?
> /bin/ksh:  1 < 10 &&  != 0 : unexpected `!='
> 2
> : bleys;



Philip


Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-08 Thread Theo de Raadt
Philip Guenther  wrote:

> On Thu, Oct 7, 2021 at 5:57 PM bm1les  wrote:
> 
> > --- netstart2 Sep 2021 19:38:20 -   1.216
> > +++ netstart8 Oct 2021 02:43:30 -
> > @@ -365,7 +365,7 @@ ifmstart "tun tap gif etherip gre egre p
> >  if [[ $ip6kernel == YES ]]; then
> > # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
> > count=0
> > -   while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) !=
> > 0)); do
> > +   while ((count++ < 10 && "$(sysctl -n net.inet6.ip6.dad_pending)"
> > != 0)); do
> > sleep 1
> > done
> >  fi
> >
> 
> I can't figure out what problem you think this could solve.  Can you
> explain the circumstances under which those quotes could make a difference?

Not the OP's issue, but I think a kernels compiled without option INET6
will return an errno, and I cannot tell if sysctl prints out an error message
or converts to "", the empty string, which would conceivably mis-parse.





Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-08 Thread bm1les
Since you obviously care enough to reply and wonder about what I'm doing, I can 
happily let you know.

I was trying to configure the network while running bsd.rd. I figured I could 
reuse netstart when I spotted this bug.

The bug is that the second part of the expression breaks when sysctl returns 
nothing. The solution could be to split just that expression out and use test.

Now that I have explained the bug and the solution, it's on you to fix it. I 
have already moved on.

Cheers.


‐‐‐ Original Message ‐‐‐

On Friday, October 8th, 2021 at 12:48 AM, Klemens Nanni  
wrote:

> On Fri, Oct 08, 2021 at 05:15:36AM +, bm1les wrote:
>
> > The first problem is the lack of correctness; that should be enough.
> >
> > The second problem is that such command actually breaks when run using 
> > bsd.rd.
>
> netstart(8) has nothing to do in or with bsd.rd, whatever you do:
>
> you own all the pieces.
>
> Either you manually run /etc/netstart during the installer (who knows
>
> why) and/or you run a kernel without IPv6 support.
>
> At this point, we don't care -- don't waste time with such mails lacking
>
> any trace of reasoning, justification or explanation.



Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-08 Thread bm1les
The first problem is the lack of correctness; that should be enough.
The second problem is that such command actually breaks when run using bsd.rd.

‐‐‐ Original Message ‐‐‐
On Friday, October 8th, 2021 at 12:11 AM, Philip Guenther  
wrote:

> On Thu, Oct 7, 2021 at 5:57 PM bm1les  wrote:
>
>> --- netstart 2 Sep 2021 19:38:20 - 1.216
>> +++ netstart 8 Oct 2021 02:43:30 -
>> @@ -365,7 +365,7 @@ ifmstart "tun tap gif etherip gre egre p
>> if [[ $ip6kernel == YES ]]; then
>> # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
>> count=0
>> - while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) != 0)); do
>> + while ((count++ < 10 && "$(sysctl -n net.inet6.ip6.dad_pending)" != 0)); do
>> sleep 1
>> done
>> fi
>
> I can't figure out what problem you think this could solve. Can you explain 
> the circumstances under which those quotes could make a difference?
>
> Philip Guenther

[PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-08 Thread bm1les
Index: netstart
===
RCS file: /cvs/src/etc/netstart,v
retrieving revision 1.216
diff -u -p -u -r1.216 netstart
--- netstart2 Sep 2021 19:38:20 -   1.216
+++ netstart8 Oct 2021 02:43:30 -
@@ -365,7 +365,7 @@ ifmstart "tun tap gif etherip gre egre p
 if [[ $ip6kernel == YES ]]; then
# Ensure IPv6 Duplicate Address Detection (DAD) is completed.
count=0
-   while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) != 0)); 
do
+   while ((count++ < 10 && "$(sysctl -n net.inet6.ip6.dad_pending)" != 
0)); do
sleep 1
done
 fi



Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-07 Thread Klemens Nanni
On Fri, Oct 08, 2021 at 05:15:36AM +, bm1les wrote:
> The first problem is the lack of correctness; that should be enough.
> The second problem is that such command actually breaks when run using bsd.rd.

netstart(8) has nothing to do in or with bsd.rd, whatever you do:
you own all the pieces.

Either you manually run /etc/netstart during the installer (who knows
why) and/or you run a kernel without IPv6 support.

At this point, we don't care -- don't waste time with such mails lacking
any trace of reasoning, justification or explanation.



Re: [PATCH] /etc/netstart: unquoted command substitution inside arithmetic expression

2021-10-07 Thread Philip Guenther
On Thu, Oct 7, 2021 at 5:57 PM bm1les  wrote:

> --- netstart2 Sep 2021 19:38:20 -   1.216
> +++ netstart8 Oct 2021 02:43:30 -
> @@ -365,7 +365,7 @@ ifmstart "tun tap gif etherip gre egre p
>  if [[ $ip6kernel == YES ]]; then
> # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
> count=0
> -   while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) !=
> 0)); do
> +   while ((count++ < 10 && "$(sysctl -n net.inet6.ip6.dad_pending)"
> != 0)); do
> sleep 1
> done
>  fi
>

I can't figure out what problem you think this could solve.  Can you
explain the circumstances under which those quotes could make a difference?


Philip Guenther