Date: Wed, 24 Mar 2021 09:51:45 +0100 From: Sergio Lopez Message-ID: <20210324085145.z7fsz3cifsliznte@mhamilton> References: <47a35ffe-ef39-d098-2c29-34cf7d4eb7ef@redhat.com> <20210323163911.5lxnlv5tn66smscn@mhamilton> <8fdd157d-68ce-b743-7841-7fd80f7ccf2e@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="gialj67l5buuc47q" Content-Disposition: inline In-Reply-To: <8fdd157d-68ce-b743-7841-7fd80f7ccf2e@redhat.com> Subject: Re: [Virtio-fs] Current file handle status and open questions List-Id: Development discussions about virtio-fs List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Max Reitz Cc: virtio-fs-list --gialj67l5buuc47q Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Mar 24, 2021 at 08:40:26AM +0100, Max Reitz wrote: > On 23.03.21 17:39, Sergio Lopez wrote: > > On Thu, Mar 18, 2021 at 06:09:58PM +0100, Max Reitz wrote: > > > Hi, > > >=20 > > > As threatened in our last meeting, I=E2=80=99ve written this mail to = give an > > > overview on where we stand with regards to virtiofsd(-rs) using file > > > handles. > > >=20 > > > Technically, this should be a reply to the =E2=80=9CSecurint file han= dles=E2=80=9D > > > thread, but this mail is so long I think it=E2=80=99s better to split= it off. > > >=20 > > > There are multiple problems that somehow relate to file handles, the > > > ones I=E2=80=99m aware of are: > > >=20 > > > (A) We have a problem with too many FDs open. To solve it, we could > > > attach a file handle to each node, then close the FD (as far as > > > possible) and reopen it when needed from the file handle. > > >=20 > > > (B) We want to allow the guest to use persistent file handles. > > >=20 > > > (C) For live migration, the problem isn=E2=80=99t even clear yet, but= it seems > > > like we=E2=80=99ll want to translate nodes into their file handl= es and > > > transmit those and open them again on the destination (at least = on > > > shared filesystems). > > >=20 > > > Now every case has its own specific tricky bits: > > >=20 > > > Case (A) is something that we=E2=80=99d really like to have by defaul= t, and it > > > would need to work all the time during runtime. So the problem here = is > > > that we=E2=80=99d need CAP_DAC_READ_SEARCH, and we=E2=80=99d need it = all the time, but > > > we don=E2=80=99t want that. One interesting bit is that we don=E2=80= =99t need these > > > file handles to be persistent between virtiofsd invocations. > > >=20 > > > For (B) we have the problem of needing to protect against a potential= ly > > > malicious guest, i.e. that it must not be able to reference files > > > outside the shared directory. (Perhaps except for cases where the fi= le > > > was once reachable, i.e. where a file handle was generated by the gue= st, > > > then the file was moved outside of the shared directory, but remains > > > accessible through the file handle.) Furthermore, file handles should > > > really be persistent between virtiofsd invocations. On the positive > > > side, it would be easier for us to require CAP_DAC_READ_SEARCH for th= is > > > case, because it really is optional. We could require users to give = us > > > that capability if they want file handles in the guest (and we find no > > > way to avoid requiring that capability). > > >=20 > > > (C) needs persistency between source and destination, but on the > > > positive side, we only need to be able to open file handles during the > > > in-migrate phase on the destination. So requiring CAP_DAC_READ_SEARCH > > > only during that phase might not be that big of a deal. > > >=20 > > >=20 > > > (Ideally, we=E2=80=99d want all cases to work without CAP_DAC_READ_SE= ARCH, but > > > as you can see, that requirement is weakened for cases (B) and > > > especially (C).) > > >=20 > > >=20 > > > As far as I=E2=80=99ve understood, (A) is the case that we want to fo= cus on > > > first, and the main problem there is that we need to open file handles > > > without CAP_DAC_READ_SEARCH. > > >=20 > > > To do that, I at one point proposed a service process that has > > > CAP_DAC_READ_SEARCH and would open file handles for virtiofsd. But t= hat > > > probably won=E2=80=99t really be an improvement, because this process= too would > > > probably need to run in the container and so if we can=E2=80=99t give= virtiofsd > > > that capability, we can=E2=80=99t give it to that service process eit= her. > > >=20 > > > What Miklos proposed was to modify the kernel to allow processes to o= pen > > > file handles even if they don=E2=80=99t have CAP_DAC_READ_SEARCH as l= ong as > > > those files are in the process=E2=80=99s scope. One way to implement= this > > > restriction (in a very restrictive manner) is to only allow opening f= ile > > > handles that the process has generated before, e.g. by appending a MAC > > > to every file handle (generated with a process-specific key) and > > > checking that when opening a handle. (You would request this MAC wit= h a > > > new AT_* flag passed to name_to_handle_at(). open_by_handle_at() > > > recognizes it due to a special file handle type value.) > > >=20 > > > (Process-specific key =3D stored in current->files, i.e. the files_st= ruct. > > > I=E2=80=99m not 100% sure what this is, but I guess this is the struc= ture that > > > keeps a process=E2=80=99s open file descriptors, and so should genera= lly be > > > unique to a process, or at least unique to a group of processes that > > > share the same FDs.) > >=20 > > Does this mean that if we have, let's say, 3 virtiofsd instances each > > one "remembering" (because there's a reference in the dentry cache of > > the guest they are servicing) 10.000 files, the Host's kernel would > > need to keep 30.000 entries holding the host_file+MAC information? Or > > is there some kind of VFS trickery at play here that allows to do the > > check without holding all that information? >=20 > No, you only store one MAC key per process. The MAC is appended to the f= ile > handle that is handed back to virtiofsd, so the kernel doesn=E2=80=99t ne= ed to store > it. Ah, that sounds great, thanks for the clarification. I'm mainly concerned about the need to keep an FD open per each "known" (as in present in the guest's dentry cache) file, and this seems like a good solution for it. Sergio. --gialj67l5buuc47q Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEvtX891EthoCRQuii9GknjS8MAjUFAmBa/ZcACgkQ9GknjS8M AjWC2hAAjpSV01GYBPmYsLwAzYFd1q+QrmTZMqKsh2FC8w6+50eOcgEndhRRW5dQ lxOpw4eazK60pBfTS4rzrNMvVMbwb8rU18U1AnpUU4kruq0cy77s0Qktr/cgtyjX 5ufsXN6o2kmjVuUGuSeXBSklXROPfuzdXqTUwLYk3G9ON4sngEa3lh5IodL/XmJR 0THAOXRzUREkNWNYsqgKnzRf37/VkRxWH1LZ6tuXZy/uogt6qVzUknYHmDBDqflH fK5Es0DVZ86DEuP3HYX33NQ4L38Hwa/yuHPhyDXNPhQJcPJ9L3ojjL6sGDsM7dFc w+TbIPA5hUrf2qw1njIlaKW9PUdyt+nKm/f+8KK8FLyQbMYlI05Vf5+aufrW4oJT /lbD93hzJ4Bq8knHETOXb3ImcHNZs7nikQBEVhRtp7om9fM3Bzgd7j+4BAB2wVLn A+xwVYta39a9xdIm5hWtlmXdmlYM6vbPExTC2NcTefv9qxm9d+RNpNAlvQRMtwlb 2tqD8EmVdSq0+4bANhPfL9VxTE/F9FeoAfEL6YmvljkpvP0J1uT5jFxAluYw1aXj uvuSdSPTZ7VLURjs59FxsQ/V/PxnHSLEPihSadf0L6K0iOuODuHUF0zzGCSYydVc nzTS0/9FY8OZCVSsVgTL9RGI41YnytXZd1RVZ17wW5PCra3G2YE= =c6WR -----END PGP SIGNATURE----- --gialj67l5buuc47q--