networkConfig_linux.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2020, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  22. "github.com/syndtr/gocapability/capability"
  23. )
  24. func configureNetworkConfigSubprocessCapabilities() error {
  25. // If this process has CAP_NET_ADMIN, make it available to be inherited
  26. // be child processes via ambient mechanism described here:
  27. // https://github.com/torvalds/linux/commit/58319057b7847667f0c9585b9de0e8932b0fdb08
  28. //
  29. // The ambient mechanism is available in Linux kernel 4.3 and later.
  30. // When using capabilities, this process should have CAP_NET_ADMIN in order
  31. // to create tun devices. And the subprocess operations such as using "ifconfig"
  32. // and "iptables" for network config require the same CAP_NET_ADMIN capability.
  33. cap, err := capability.NewPid(0)
  34. if err != nil {
  35. return errors.Trace(err)
  36. }
  37. if cap.Get(capability.EFFECTIVE, capability.CAP_NET_ADMIN) {
  38. cap.Set(capability.INHERITABLE|capability.AMBIENT, capability.CAP_NET_ADMIN)
  39. err = cap.Apply(capability.AMBIENT)
  40. if err != nil {
  41. return errors.Trace(err)
  42. }
  43. }
  44. return nil
  45. }