Commit Graph

22 Commits

Author SHA1 Message Date
Nalin Dahyabhai 0f2bccfa56 Complete "pulling up" of images in updateNames()
When updateNames() copies an image's record from a read-only store into
the read-write store, copy the accompanying data as well.

Add fields for setting data items at creation-time to LayerOptions,
ImageOptions, and ContainerOptions to make this easier for us and our
consumers.

Replace the store-specific Create() (and the one CreateWithFlags() and
Put()) with private create() and put() methods, since they're not
intended for consumption outside of this package, and add Flags to the
options structures we pass into those methods.  In create() methods,
make copies of those passed-in options structures before modifying any
of their contents.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2023-03-31 10:36:30 -04:00
Giuseppe Scrivano b876a48e08
userns: skip the nobody user
improve the heuristic to detect the user namespace size needed to run
an image.  Hardcode the nobody user value to 65534, which is the value
used by the kernel, and ignore this value when parsing /etc/passwd and
/etc/group.

Closes: https://github.com/containers/storage/issues/1472

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2023-01-16 20:13:04 +01:00
Miloslav Trmač 845c2f4a30 Eliminate racy uses of Layer.MountCount
Instead of reading that value, releasing the mount lock,
and then unmounting, provide a "conditional" unmount mode.
And use that in the existing "loop unmounting" code.

That's at least safer against concurrent processes unmounting
the same layer. But the callers that try to "really unmount"
the layer in a loop are still possibly racing against other processes
trying to mount the layer in the meantime.

I'm not quite sure that we need the "conditional" parameter as an
explicit choice; it seems fairly likely that Umount() should just fail
with ErrLayerNotMounted for all !force callers. I chose to use the flag
to be conservative WRT possible unknown constraints.

Similarly, it's not very clear to me that the unmount loops need to exist;
maybe they should just be unmount(force=true, conditional=true).

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2023-01-03 23:47:16 +01:00
Aditya R 812462ec9f
userns: stop printing size as escaped string in error
Using `%q` as format specifier for size prints non-meaningful error
messages instead use `%v` which will print in default format.

Example Bad error message
```console
Error: identifier is not a container: preparing container for next step:
creating build container: creating container: the container needs a user
namespace with size '𘛋' that is bigger than the maximum value allowed
with userns=auto '𐀀''
```

Signed-off-by: Aditya R <arajan@redhat.com>
2022-12-12 17:24:42 +05:30
Miloslav Trmač ad8046ad2a Fix layer locking in CreateContainer
- imageTopLayerForMapping, in the cParentLayer loop, was reading all
  additional layer stores, although only some of them might have been
  locked at that point.
- getAutoUserNS -> getMaxSizeFromImage was assuming that all layer stores
  are locked, but in fact the additional layer stores were all unlocked
  at that point
- getMaxSizeFromImage was calling store.getLayerStore() and the like,
  which could potentially trigger a reload via graphLock and return
  a _different_ store than the one that was locked

So, lock _all_ layer stores in CreateContainer (at least on the path
where images are involved), and pass the locked layer stores down the
call stack.

Also, document a bit more explicitly what's going on.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-14 02:36:39 +02:00
Miloslav Trmač b997309d26 Make store's accessors of {Container,Layer,Image,ROLayer,ROImage}Store private
It is not directly reachable by any public API, and there are no known
external users.

So, make it obviously private to make it easier to change the returned
interface in the future.

Should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-11 19:25:19 +02:00
Miloslav Trmač 55907efbe1 Add store.allImageStores and store.allLayerStores
... to reduce duplication of read implementations of Store.

This can almost certainly be simplified further; this is enough
for the purposes of this PR, where we want to decrease the
number of callers that need to be updated.

Should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-11 19:25:19 +02:00
Miloslav Trmač 74931e97b0 Add missing error handling on error recovery paths
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-01 02:53:04 +02:00
Nalin Dahyabhai 449ffb0f8d Use defined constants for flag names
Use constants for the names of flags that we set in Flags maps that we
store in layer/image/container records, to make it easier to avoid
possible breakages due to typos in the future.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-09-14 10:54:54 -04:00
Sascha Grunert 3455d12729
Switch to golang native error wrapping
We now use the golang error wrapping format specifier `%w` instead of the
deprecated github.com/pkg/errors package.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
2022-07-07 13:22:46 +02:00
Miloslav Trmač a834ef8eac Don't use a container ID for an ID of a temporary layer
There isn't any easy-to-see reason to use the same ID for
the two objects, and not needing to known the container ID
at that point will help generating it correctly without
breaking the ContainerStore abstraction.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-06-15 17:44:56 +02:00
Daniel J Walsh 534b0b3281
Standardize on capatalized logrus messages, and remove stutters
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-09-23 14:43:35 -04:00
Kan Li c9bcfd8a5f Rework autons ID mapping generation.
This implements the algorithm proposed in
https://github.com/containers/storage/issues/852#issuecomment-798954173,
which is:
1. find available IDs from subuid/subgid file; by subtracting the used
   IDs (from other containers) as well as additional IDs, we get the IDs
   available to allocate;
2. target ID range is [0, requestedSize), subtract the additional IDs;
3. allocate IDs from range in step 1; the number to allocate is the
   number of IDs in step 2;
4. generate a mapping from IDs in step 3 to the ones in step 2.

Closes: https://github.com/containers/storage/issues/852

Signed-off-by: Kan Li <likan@google.com>
2021-04-03 13:22:23 -07:00
Daniel J Walsh 120cc997d2
Move storageOpts structures into types subdir to shrink bindings.
Currently when we build podman bindings we are pulling in the entire
storage libraries, even though we only need a few structures and
functions.

Testing with the following program

```
package main

import (
	"fmt"
	"github.com/containers/storage/types"
)

func main() {
	fmt.Println(types.GetRootlessRuntimeDir(0))
}
```

Removing types above gives me compile size of the the program

du -s t.old t.new
9640	t.before
3232	t.after

Currently these functions are being vendored into
containers/common/pkg/config, which leads to large size in podman-remote
and podman bindings.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-02-26 05:34:09 -05:00
Kan Li d27c88d3ef Fix subtractAll bugs.
The logic of range subtraction [a,b)-[c,d) can be viewed as intersection
of [a,b) with (-inf, c) and [d, +inf), respectively. This makes the
logic simpler, that we no longer need to check 5 different cases.

It also fixes bugs that returns incorrect range.

Closes #763
Signed-off-by: Kan Li <likan@google.com>
2020-11-01 10:52:08 -08:00
Giuseppe Scrivano 4caae16a28
userns: make sure host id is not always 0
when it finds multiple available ranges, make sure the host id is
correctly initialized.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2020-08-06 16:34:57 +02:00
Giuseppe Scrivano 64570f6324
userns: fix host id calculation when ranges overlap
issue found while testing: https://github.com/cri-o/cri-o/pull/3944

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2020-08-03 14:57:43 +02:00
Giuseppe Scrivano aff464e9c6
userns: simplify function
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2020-08-03 14:27:45 +02:00
Giuseppe Scrivano ae6846d67a
userns: fix available range with explicit idmapping
when an explicit idmapping is specified, the host id must be taken
from the available range of IDs.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2020-07-12 15:44:53 +02:00
Daniel J Walsh e99de20dfd
Coverity found an issue with copy and pasted code
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2020-05-14 11:00:29 -04:00
Giuseppe Scrivano 422ad8f8da
vendor: move common/pkg/unshare to storage/pkg/unshare
to avoid a circular dependency.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2020-03-31 10:16:09 +02:00
Giuseppe Scrivano 2cff5ddf93
userns: add support for auto
automatically pick an empty range and create an user namespace for the
container.

For root containers, it is necessary to specify an entry in
the /etc/subuid and /etc/subgid files to use for picking the range of
available IDs.  This is necessary to avoid collisions with IDs used
for rootless containers.  This setting is ignored for rootless
containers, since it is not possible to use arbitrary IDs, and the
initial set is always picked by the IDs assigned to the rootless
user.

When using auto userns, a container will use a range of IDs that is
not used by any other container user namespace, also those that are
not using auto userns, this is checked at creation time.
A successive container that doesn't use auto userns feature can still
collide with IDs used by an auto userns container.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2020-03-26 11:12:34 +01:00