cherry-pick: 4670-invalid-arg-cap-check

Updates #4670.

Squashed commit of the following:

commit 9c32739eb92ef57c78a4dc3ec3c0f280aebf7182
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Aug 3 20:04:54 2022 +0300

    aghnet: imp port check for older linuxes
This commit is contained in:
Ainar Garipov 2022-08-03 20:36:20 +03:00 committed by Ainar Garipov
parent 44222c604c
commit c8d3afe869
2 changed files with 19 additions and 2 deletions

View file

@ -24,7 +24,13 @@ and this project adheres to
- Go 1.18 support. v0.109.0 will require at least Go 1.19 to build.
### Fixed
- `invalid argument` errors during update checks on older Linux kernels
([#4670]).
[#2993]: https://github.com/AdguardTeam/AdGuardHome/issues/2993
[#4670]: https://github.com/AdguardTeam/AdGuardHome/issues/4670

View file

@ -13,6 +13,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/stringutil"
"github.com/google/renameio/maybe"
"golang.org/x/sys/unix"
@ -22,17 +23,27 @@ import (
const dhcpcdConf = "etc/dhcpcd.conf"
func canBindPrivilegedPorts() (can bool, err error) {
cnbs, err := unix.PrctlRetInt(
res, err := unix.PrctlRetInt(
unix.PR_CAP_AMBIENT,
unix.PR_CAP_AMBIENT_IS_SET,
unix.CAP_NET_BIND_SERVICE,
0,
0,
)
if err != nil {
if errors.Is(err, unix.EINVAL) {
// Older versions of Linux kernel do not support this. Print a
// warning and check admin rights.
log.Info("warning: cannot check capability cap_net_bind_service: %s", err)
} else {
return false, err
}
}
// Don't check the error because it's always nil on Linux.
adm, _ := aghos.HaveAdminRights()
return cnbs == 1 || adm, err
return res == 1 || adm, nil
}
// dhcpcdStaticConfig checks if interface is configured by /etc/dhcpcd.conf to