gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/network_control.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     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  
    20  package builtin
    21  
    22  import (
    23  	"gitee.com/mysnapcore/mysnapd/osutil"
    24  )
    25  
    26  const networkControlSummary = `allows configuring networking and network namespaces`
    27  
    28  const networkControlBaseDeclarationSlots = `
    29    network-control:
    30      allow-installation:
    31        slot-snap-type:
    32          - core
    33      deny-auto-connection: true
    34  `
    35  
    36  const networkControlConnectedPlugAppArmor = `
    37  # Description: Can configure networking and network namespaces via the standard
    38  # 'ip netns' command (man ip-netns(8)). This interface is restricted because it
    39  # gives wide, privileged access to networking and should only be used with
    40  # trusted apps.
    41  
    42  #include <abstractions/nameservice>
    43  /run/systemd/resolve/stub-resolv.conf rk,
    44  
    45  # systemd-resolved (not yet included in nameservice abstraction)
    46  #
    47  # Allow access to the safe members of the systemd-resolved D-Bus API:
    48  #
    49  #   https://www.freedesktop.org/software/systemd/man/org.freedesktop.resolve1.html
    50  #
    51  # This API may be used directly over the D-Bus system bus or it may be used
    52  # indirectly via the nss-resolve plugin:
    53  #
    54  #   https://www.freedesktop.org/software/systemd/man/nss-resolve.html
    55  #
    56  #include <abstractions/dbus-strict>
    57  dbus send
    58       bus=system
    59       path="/org/freedesktop/resolve1"
    60       interface="org.freedesktop.resolve1.Manager"
    61       member="Resolve{Address,Hostname,Record,Service}"
    62       peer=(name="org.freedesktop.resolve1", label=unconfined),
    63  
    64  dbus (send)
    65       bus=system
    66       path="/org/freedesktop/resolve1"
    67       interface="org.freedesktop.resolve1.Manager"
    68       member="SetLink{DefaultRoute,DNSOverTLS,DNS,DNSEx,DNSSEC,DNSSECNegativeTrustAnchors,MulticastDNS,Domains,LLMNR}"
    69       peer=(label=unconfined),
    70  
    71  # required by resolvectl command
    72  dbus (send)
    73       bus=system
    74       path="/org/freedesktop/resolve1"
    75       interface=org.freedesktop.DBus.Properties
    76       member=Get{,All}
    77       peer=(label=unconfined),
    78  
    79  # required by resolvectl command
    80  dbus (receive)
    81       bus=system
    82       path="/org/freedesktop/resolve1"
    83       interface=org.freedesktop.DBus.Properties
    84       member=PropertiesChanged
    85       peer=(label=unconfined),
    86  
    87  # required by resolvectl command
    88  dbus (send)
    89       bus=system
    90       path="/org/freedesktop/resolve1/link/*"
    91       interface="org.freedesktop.DBus.Properties"
    92       member=Get{,All}
    93       peer=(label=unconfined),
    94  
    95  # required by resolvectl command
    96  dbus (receive)
    97       bus=system
    98       path="/org/freedesktop/resolve1/link/*"
    99       interface="org.freedesktop.DBus.Properties"
   100       member=PropertiesChanged
   101       peer=(label=unconfined),
   102  
   103  #include <abstractions/ssl_certs>
   104  
   105  capability net_admin,
   106  capability net_raw,
   107  capability setuid, # ping
   108  capability net_broadcast, # openvswitchd
   109  
   110  # Allow protocols except those that we blacklist in
   111  # /etc/modprobe.d/blacklist-rare-network.conf
   112  network appletalk,
   113  network bridge,
   114  network inet,
   115  network inet6,
   116  network ipx,
   117  network packet,
   118  network pppox,
   119  network sna,
   120  
   121  @{PROC}/@{pid}/net/ r,
   122  @{PROC}/@{pid}/net/** r,
   123  
   124  # used by sysctl, et al
   125  @{PROC}/sys/ r,
   126  @{PROC}/sys/net/ r,
   127  @{PROC}/sys/net/core/ r,
   128  @{PROC}/sys/net/core/** rw,
   129  @{PROC}/sys/net/ipv{4,6}/ r,
   130  @{PROC}/sys/net/ipv{4,6}/** rw,
   131  @{PROC}/sys/net/netfilter/ r,
   132  @{PROC}/sys/net/netfilter/** rw,
   133  @{PROC}/sys/net/nf_conntrack_max rw,
   134  
   135  # For advanced wireless configuration
   136  /sys/kernel/debug/ieee80211/ r,
   137  /sys/kernel/debug/ieee80211/** rw,
   138  
   139  # read netfilter module parameters
   140  /sys/module/nf_*/                r,
   141  /sys/module/nf_*/parameters/{,*} r,
   142  
   143  # networking tools
   144  /{,usr/}{,s}bin/arp ixr,
   145  /{,usr/}{,s}bin/arpd ixr,
   146  /{,usr/}{,s}bin/bridge ixr,
   147  /{,usr/}{,s}bin/dhclient Pxr,             # use ixr instead if want to limit to snap dirs
   148  /{,usr/}{,s}bin/dhclient-script ixr,
   149  /{,usr/}{,s}bin/ifconfig ixr,
   150  /{,usr/}{,s}bin/ifdown ixr,
   151  /{,usr/}{,s}bin/ifquery ixr,
   152  /{,usr/}{,s}bin/ifup ixr,
   153  /{,usr/}{,s}bin/ip ixr,
   154  /{,usr/}{,s}bin/ipmaddr ixr,
   155  /{,usr/}{,s}bin/iptunnel ixr,
   156  /{,usr/}{,s}bin/iw ixr,
   157  /{,usr/}{,s}bin/nameif ixr,
   158  /{,usr/}{,s}bin/netstat ixr,              # -p not supported
   159  /{,usr/}{,s}bin/nstat ixr,
   160  /{,usr/}{,s}bin/ping ixr,
   161  /{,usr/}{,s}bin/ping6 ixr,
   162  /{,usr/}{,s}bin/pppd ixr,
   163  /{,usr/}{,s}bin/pppdump ixr,
   164  /{,usr/}{,s}bin/pppoe-discovery ixr,
   165  #/{,usr/}{,s}bin/pppstats ixr,            # needs sys_module
   166  /{,usr/}{,s}bin/resolvectl ixr,
   167  /{,usr/}{,s}bin/route ixr,
   168  /{,usr/}{,s}bin/routef ixr,
   169  /{,usr/}{,s}bin/routel ixr,
   170  /{,usr/}{,s}bin/rtacct ixr,
   171  /{,usr/}{,s}bin/rtmon ixr,
   172  /{,usr/}{,s}bin/ss ixr,
   173  /{,usr/}{,s}bin/sysctl ixr,
   174  /{,usr/}{,s}bin/tc ixr,
   175  /{,usr/}{,s}bin/wpa_action ixr,
   176  /{,usr/}{,s}bin/wpa_cli ixr,
   177  /{,usr/}{,s}bin/wpa_passphrase ixr,
   178  /{,usr/}{,s}bin/wpa_supplicant ixr,
   179  
   180  /dev/rfkill rw,
   181  /sys/class/rfkill/ r,
   182  /sys/devices/{pci[0-9a-f]*,platform,virtual}/**/rfkill[0-9]*/{,**} r,
   183  /sys/devices/{pci[0-9a-f]*,platform,virtual}/**/rfkill[0-9]*/state w,
   184  
   185  # For reading the address of a particular ethernet interface
   186  /sys/devices/{pci[0-9a-f]*,platform,virtual}/**/net/*/address r,
   187  
   188  # arp
   189  network netlink dgram,
   190  
   191  # ip, et al
   192  /etc/iproute2/{,**} r,
   193  /etc/iproute2/rt_{protos,realms,scopes,tables} w,
   194  /etc/iproute2/rt_{protos,tables}.d/* w,
   195  
   196  # ping - child profile would be nice but seccomp causes problems with that
   197  /{,usr/}{,s}bin/ping ixr,
   198  /{,usr/}{,s}bin/ping6 ixr,
   199  network inet raw,
   200  network inet6 raw,
   201  
   202  # pppd
   203  capability setuid,
   204  @{PROC}/@{pid}/loginuid r,
   205  @{PROC}/@{pid}/mounts r,
   206  
   207  # static host tables
   208  /etc/hosts w,
   209  
   210  # resolvconf
   211  /{,usr/}sbin/resolvconf ixr,
   212  /run/resolvconf/{,**} rk,
   213  /run/resolvconf/** w,
   214  /etc/resolvconf/{,**} r,
   215  /{,usr/}lib/resolvconf/* ix,
   216  # Required by resolvconf
   217  /{,usr/}bin/run-parts ixr,
   218  /etc/resolvconf/update.d/* ix,
   219  
   220  # wpa_suplicant
   221  /{,var/}run/wpa_supplicant/ w,
   222  /{,var/}run/wpa_supplicant/** rw,
   223  /etc/wpa_supplicant/{,**} ixr,
   224  
   225  #ifup,ifdown, dhclient
   226  /{,var/}run/dhclient.*.pid rw,
   227  /var/lib/dhcp/ r,
   228  /var/lib/dhcp/** rw,
   229  
   230  /run/network/ifstate* rw,
   231  /run/network/.ifstate* rw,
   232  /run/network/ifup-* rw,
   233  /run/network/ifdown-* rw,
   234  
   235  # route
   236  /etc/networks r,
   237  /etc/ethers r,
   238  
   239  /etc/rpc r,
   240  
   241  # TUN/TAP - https://www.kernel.org/doc/Documentation/networking/tuntap.txt
   242  #
   243  # We only need to tag /dev/net/tun since the tap[0-9]* and tun[0-9]* devices
   244  # are virtual and don't show up in /dev
   245  /dev/net/tun rw,
   246  
   247  # Access to sysfs interfaces for tun/tap/mstp/bchat device settings.
   248  /sys/devices/virtual/net/{tap*,mstp*,bchat*}/** rw,
   249  
   250  # access to bridge sysfs interfaces for bridge settings
   251  /sys/devices/virtual/net/*/bridge/* rw,
   252  
   253  # Network namespaces via 'ip netns'. In order to create network namespaces
   254  # that persist outside of the process and be entered (eg, via
   255  # 'ip netns exec ...') the ip command uses mount namespaces such that
   256  # applications can open the /run/netns/NAME object and use it with setns(2).
   257  # For 'ip netns exec' it will also create a mount namespace and bind mount
   258  # network configuration files into /etc in that namespace. See man ip-netns(8)
   259  # for details.
   260  
   261  capability sys_admin, # for setns()
   262  network netlink raw,
   263  
   264  / r,
   265  /run/netns/ r,     # only 'r' since snap-confine will create this for us
   266  /run/netns/* rw,
   267  mount options=(rw, rshared) -> /run/netns/,
   268  mount options=(rw, bind) /run/netns/ -> /run/netns/,
   269  mount options=(rw, bind) / -> /run/netns/*,
   270  umount /,
   271  
   272  # 'ip netns identify <pid>' and 'ip netns pids foo'. Intenionally omit 'ptrace
   273  # (trace)' here since ip netns doesn't actually need to trace other processes.
   274  capability sys_ptrace,
   275  
   276  # 'ip netns exec foo /bin/sh'
   277  mount options=(rw, rslave) /,
   278  mount options=(rw, rslave), # LP: #1648245
   279  umount /sys/,
   280  
   281  # Eg, nsenter --net=/run/netns/... <command>
   282  /{,usr/}{,s}bin/nsenter ixr,
   283  `
   284  
   285  const networkControlConnectedPlugSecComp = `
   286  # Description: Can configure networking and network namespaces via the standard
   287  # 'ip netns' command (man ip-netns(8)). This interface is restricted because it
   288  # gives wide, privileged access to networking and should only be used with
   289  # trusted apps.
   290  
   291  # for ping and ping6
   292  capset
   293  
   294  # Network namespaces via 'ip netns'. In order to create network namespaces
   295  # that persist outside of the process and be entered (eg, via
   296  # 'ip netns exec ...') the ip command uses mount namespaces such that
   297  # applications can open the /run/netns/NAME object and use it with setns(2).
   298  # For 'ip netns exec' it will also create a mount namespace and bind mount
   299  # network configuration files into /etc in that namespace. See man ip-netns(8)
   300  # for details.
   301  bind
   302  
   303  mount
   304  umount
   305  umount2
   306  
   307  unshare
   308  setns - CLONE_NEWNET
   309  
   310  # For various network related netlink sockets
   311  socket AF_NETLINK - NETLINK_ROUTE
   312  socket AF_NETLINK - NETLINK_FIB_LOOKUP
   313  socket AF_NETLINK - NETLINK_INET_DIAG
   314  socket AF_NETLINK - NETLINK_XFRM
   315  socket AF_NETLINK - NETLINK_DNRTMSG
   316  socket AF_NETLINK - NETLINK_ISCSI
   317  socket AF_NETLINK - NETLINK_RDMA
   318  socket AF_NETLINK - NETLINK_GENERIC
   319  
   320  # for receiving kobject_uevent() net messages from the kernel
   321  socket AF_NETLINK - NETLINK_KOBJECT_UEVENT
   322  `
   323  
   324  /* https://www.kernel.org/doc/Documentation/networking/tuntap.txt
   325   *
   326   * We only need to tag /dev/net/tun since the tap[0-9]* and tun[0-9]* devices
   327   * are virtual and don't show up in /dev
   328   */
   329  var networkControlConnectedPlugUDev = []string{
   330  	`KERNEL=="rfkill"`,
   331  	`KERNEL=="tun"`,
   332  }
   333  
   334  var networkControlConnectedPlugMount = []osutil.MountEntry{{
   335  	Name:    "/var/lib/snapd/hostfs/var/lib/dhcp",
   336  	Dir:     "/var/lib/dhcp",
   337  	Options: []string{"bind", "rw", osutil.XSnapdIgnoreMissing()},
   338  }}
   339  
   340  // TODO: Add a layer that derives this sort of data from mount entry, like the
   341  // one above, into a set of apparmor rules for snap-update-ns, like the ones
   342  // below.
   343  //
   344  // When setting up a mount entry, we also need corresponding
   345  // snap-updates-ns rules. Eg, if have:
   346  // []osutil.MountEntry{{
   347  //	Name:    "/foo/bar",
   348  //	Dir:     "/bar",
   349  //	Options: []string{"rw", "bind"},
   350  // }}
   351  // Then you can expect to need:
   352  // /foo/ r,
   353  // /foo/bar/ r,
   354  // mount options=(rw bind) /foo/bar/ -> /bar/,
   355  // umount /bar/,
   356  // ...
   357  // You'll need 'r' rules for all the directories that need to be traversed,
   358  // starting from the root directory all the way down to the directory being
   359  // mounted. This is required by the safe bind mounting trick employed by
   360  // snap-update-ns.
   361  //
   362  // You'll need 'rw' rules to support cases when snap-update-ns is expected to
   363  // create the missing directory, before performing the bind mount. Note that
   364  // there are two sides, one side is the host visible through
   365  // /var/lib/snapd/hostfs and the other side is everything else. To support
   366  // writes to the host side you need to coordinate with the trespassing rules
   367  // implemented in snap-update-ns/system.go.
   368  var networkControlConnectedPlugUpdateNSAppArmor = `
   369  /var/ r,
   370  /var/lib/ r,
   371  /var/lib/snapd/ r,
   372  /var/lib/snapd/hostfs/ r,
   373  /var/lib/snapd/hostfs/var/ r,
   374  /var/lib/snapd/hostfs/var/lib/ r,
   375  /var/lib/snapd/hostfs/var/lib/dhcp/ r,
   376  /var/lib/dhcp/ r,
   377  mount options=(rw bind) /var/lib/snapd/hostfs/var/lib/dhcp/ -> /var/lib/dhcp/,
   378  umount /var/lib/dhcp/,
   379  `
   380  
   381  func init() {
   382  	registerIface(&commonInterface{
   383  		name:                  "network-control",
   384  		summary:               networkControlSummary,
   385  		implicitOnCore:        true,
   386  		implicitOnClassic:     true,
   387  		baseDeclarationSlots:  networkControlBaseDeclarationSlots,
   388  		connectedPlugAppArmor: networkControlConnectedPlugAppArmor,
   389  		connectedPlugSecComp:  networkControlConnectedPlugSecComp,
   390  		connectedPlugUDev:     networkControlConnectedPlugUDev,
   391  
   392  		connectedPlugMount:            networkControlConnectedPlugMount,
   393  		connectedPlugUpdateNSAppArmor: networkControlConnectedPlugUpdateNSAppArmor,
   394  
   395  		suppressPtraceTrace:         true,
   396  		suppressSysModuleCapability: true,
   397  
   398  		// affects the plug snap because of mount backend
   399  		affectsPlugOnRefresh: true,
   400  	})
   401  }