Docker 防止使用无根容器更改装载中的权限

Docker 防止使用无根容器更改装载中的权限,docker,podman,rootless,Docker,Podman,Rootless,在rootful容器中,这个问题的解决方案是,这不适用于rootless contain系统(rootless docker,或者在我的例子中是podman): 因此,对于无根容器系统,我应该删除--user,因为根用户会自动映射到调用用户: $ podman run -v "$PWD/x:/x:rw" ubuntu:focal bash -c 'echo hi >> /x/test' $ ls -al x total 12 drwxr-xr-x 2 asott

在rootful容器中,这个问题的解决方案是,这不适用于rootless contain系统(rootless docker,或者在我的例子中是podman):

因此,对于无根容器系统,我应该删除
--user
,因为根用户会自动映射到调用用户:

$ podman run -v "$PWD/x:/x:rw" ubuntu:focal bash -c 'echo hi >> /x/test'
$ ls -al x
total 12
drwxr-xr-x  2 asottile asottile 4096 Sep  3 10:02 .
drwxrwxrwt 18 root     root     4096 Sep  3 10:01 ..
-rw-r--r--  1 asottile asottile    3 Sep  3 10:02 test
但是,由于这现在是根用户,他们可以将所有权更改为不可在容器外部删除的用户:

$ podman run -v "$PWD/x:/x:rw" ubuntu:focal bash -c 'mkdir -p /x/1/2/3 && chown -R nobody /x/1'
$ ls -al x/
total 16
drwxr-xr-x  3 asottile asottile 4096 Sep  3 10:03 .
drwxrwxrwt 18 root     root     4096 Sep  3 10:01 ..
drwxr-xr-x  3   165533 asottile 4096 Sep  3 10:03 1
-rw-r--r--  1 asottile asottile    3 Sep  3 10:02 test
$ rm -rf x/
rm: cannot remove 'x/1/2/3': Permission denied
因此,我的问题是:如何允许写入装载,但防止更改无根容器的所有权?

我认为
--user$(id-u):$(id-g)--userns=keep id
将得到您想要的

$ id -un                                                                                                                                                                                                                                      
erik                                                                                                                                                                                                                                          
$ id -gn                                                                                                                                                                                                                                      
erik                                                                                                                                                                                                                                          
$ mkdir x                                                                                                                                                                                                                                     
$ podman run -v "$PWD/x:/x:Z" --user $(id -u):$(id -g) --userns=keep-id docker.io/library/ubuntu:focal bash -c 'mkdir -p /x/1/2/3 && chown -R nobody /x/1'                                                                                    
chown: changing ownership of '/x/1/2/3': Operation not permitted                                                                                                                                                                              
chown: changing ownership of '/x/1/2': Operation not permitted                                                                                                                                                                                
chown: changing ownership of '/x/1': Operation not permitted                                                                                                                                                                                  
$ ls x                                                                                                                                                                                                                                        
1                                                                                                                                                                                                                                             
$ ls -l x                                                                                                                                                                                                                                     
total 0                                                                                                                                                                                                                                       
drwxr-xr-x. 3 erik erik 15 Sep  6 19:34 1                                                                                                                                                                                                     
$ ls -l x/1                                                                                                                                                                                                                                   
total 0                                                                                                                                                                                                                                       
drwxr-xr-x. 3 erik erik 15 Sep  6 19:34 2                                                                                                                                                                                                     
$ ls -l x/1/2                                                                                                                                                                                                                                 
total 0                                                                                                                                                                                                                                       
drwxr-xr-x. 2 erik erik 6 Sep  6 19:34 3                                                                                                                                                                                                      
$ 
关于删除不属于普通UID和GID的文件和目录(而是从/etc/subuid和/etc/subgid中的额外范围),您可以 使用
podman取消共享rm文件路径


podman unshare rm-rf directorypath

完美!我还发现,
——cap-drop-CHOWN
也很有效,但效果要好得多!
$ id -un                                                                                                                                                                                                                                      
erik                                                                                                                                                                                                                                          
$ id -gn                                                                                                                                                                                                                                      
erik                                                                                                                                                                                                                                          
$ mkdir x                                                                                                                                                                                                                                     
$ podman run -v "$PWD/x:/x:Z" --user $(id -u):$(id -g) --userns=keep-id docker.io/library/ubuntu:focal bash -c 'mkdir -p /x/1/2/3 && chown -R nobody /x/1'                                                                                    
chown: changing ownership of '/x/1/2/3': Operation not permitted                                                                                                                                                                              
chown: changing ownership of '/x/1/2': Operation not permitted                                                                                                                                                                                
chown: changing ownership of '/x/1': Operation not permitted                                                                                                                                                                                  
$ ls x                                                                                                                                                                                                                                        
1                                                                                                                                                                                                                                             
$ ls -l x                                                                                                                                                                                                                                     
total 0                                                                                                                                                                                                                                       
drwxr-xr-x. 3 erik erik 15 Sep  6 19:34 1                                                                                                                                                                                                     
$ ls -l x/1                                                                                                                                                                                                                                   
total 0                                                                                                                                                                                                                                       
drwxr-xr-x. 3 erik erik 15 Sep  6 19:34 2                                                                                                                                                                                                     
$ ls -l x/1/2                                                                                                                                                                                                                                 
total 0                                                                                                                                                                                                                                       
drwxr-xr-x. 2 erik erik 6 Sep  6 19:34 3                                                                                                                                                                                                      
$