for-win icon indicating copy to clipboard operation
for-win copied to clipboard

WindowsContainersController.FixPermissions sets acls too deep

Open anderslaub opened this issue 4 years ago • 23 comments

  • [x] I have tried with the latest version of Docker Desktop
  • [x] I have tried disabling enabled experimental features

Actual behavior

Entrypoints located in C:\{folder} is not allowed to run as ContainerUser.

Any access control set on files or folders is overridden when docker is restarted and ContainerUser is removed as owner of files and folders created during build.

Expected behavior

Access control lists within the container filesystem should not be modified by the docker service on startup.

Information

  • Windows Version: 10 21H2
  • Docker Desktop Version: 4.7.1
  • WSL2 or Hyper-V backend? hyperv
  • Are you running inside a virtualized Windows e.g. on a cloud server or a VM: no

When upgrading from Docker Desktop 4.6.1 to 4.7.1 some containers failed to start due to missing file system permissions.

I noticed that setting acls explicitly to allow users during container build solved the issue.

But only until docker service was restarted - then any acls set was gone and only local admin and system was allowed.

Adding an audit rule on changepermission to the folder within the data dir showed that it was com.docker.service that changed the permissions.

image

Further investigation led me to WindowsContainersController.Start > FixPermissions > PermissionUtils.SetAdministratorLocalSystemOnly > ApplyACLOnChildren where I noticed that acls are updated 4 levels down while 3 levels would have been sufficient.

It is a breaking change with significant impact that apps no longer can reside directly in a folder on the c drive. It is extremely common to deploy f. ex. netcore apps etc. to C:\App or similar which is not currently possible. Any apps would need to be deployed 2 levels deeper - or run as ContainerAdministrator which I fear some will see as a viable mitigation (don't).

The issue is easy to reproduce and others have confirmed experiencing the same issues following an upgrade.

Steps to reproduce the behavior

  1. Build below dockerfile
# escape=`

FROM mcr.microsoft.com/windows/nanoserver:1809

WORKDIR /app
RUN echo echo ¯\_(?)_/¯ > test.bat

ENTRYPOINT "C:\app\test.bat"
  1. Run it image
  2. Restart Docker
  3. Run it again

image

  1. Last; make a change to the RUN command in the Dockerfile and build the container again (using cache).

image

ContainerUser no longer can access C:\App in the cache layer even though ContainerUser really should be the owner.

Please help solve this quickly or provide more information on these recent unannounced changes in behaviour.

Thanks.

Related issue reported here: https://github.com/docker/for-win/issues/12684

anderslaub avatar May 04 '22 23:05 anderslaub

I have experienced this issue over the last few days. Quite the pain. In one instance the permissions were completely absent on the folder at c:\node

michaellwest avatar May 04 '22 23:05 michaellwest

Seeing this as well. Reverting back to 4.6.1 resolves the issues.

RAhnemann avatar May 05 '22 00:05 RAhnemann

seems to only happen using mcr.microsoft.com/windows/nanoserver ... works fine using mcr.microsoft.com/windows/servercore both in hyperv and process isolation.

pbering avatar May 05 '22 08:05 pbering

@pbering This is by "design" and due to mcr.microsoft.com/windows/servercore running as ContainerAdministrator by default (which really should be stopped since it enable easy container escapes if it is breached since ContainerAdministrator is local admin on the host too).

You can try this example Dockerfile to confirm that the same permission issues occur when using servercore as base:

# escape=`

FROM mcr.microsoft.com/windows/servercore:ltsc2019

USER ContainerUser

WORKDIR /app
RUN echo echo ¯\_(?)_/¯ > test.bat

ENTRYPOINT "C:\app\test.bat"

Inspecting the access control lists in the data dir also clearly shows that permissions in the fs are changed on restart - no matter the base image.

image

anderslaub avatar May 05 '22 09:05 anderslaub

yeah, something is broken with the ACL's when running as ContainerUser. Running with --user ContainerAdministrator works as a temporary workaround.

pbering avatar May 05 '22 09:05 pbering

Same issue here. Tried playing around with ContainerUser and ContainerAdministrator but in the end I gave up and reverted to 4.6.1.

steviemcg avatar May 05 '22 16:05 steviemcg

Thanks for the report and the good reproduction example. We're taking a look.

StefanScherer avatar May 06 '22 12:05 StefanScherer

Hello @anderslaub, can you try that build: https://desktop-stage.docker.com/win/main/amd64/79163/Docker%20Desktop%20Installer.exe You will need to rebuild your image or reset to default. But it seems to work on your simple example on my machine.

ebriney avatar May 11 '22 13:05 ebriney

First attempt appears to have worked. I removed the problematic images, downloaded them again, and everything fired up without error.

Second attempt worked as well. I shut down all the containers, restarted docker, and fired back up. So far so good.

michaellwest avatar May 11 '22 13:05 michaellwest

Thank you so much @ebriney I can confirm that it works too.

I can't help it but I had to peak on the changes made and I can see that you now skip the windowsfilter folder in PermissionUtils.ApplyACLOnChildren(..)

Can you elaborate a bit on why the acls has to be ensured restricted to local admin & system in the rest of the docker data root folder?

It runs on every WindowsContainerController Start which from the looks of it was something that was introduced in v4.8.0.

From an outsiders perspective, just looking through the reflected code it could seem to have been rushed in, which makes me quite curious on why it's needed in the first place.

anderslaub avatar May 11 '22 17:05 anderslaub

Good news!

We have to change ACLs @anderslaub to fix some CVEs that allowed privilege escalation (they will be published soon).

Thanks for your help on the issue.

ebriney avatar May 12 '22 08:05 ebriney

Just tried to install 4.9.0 but I get a message saying that docker-desktop is up to date, even though I'm currently on 4.8.2 :-(

So no luck installing it and checking that this will fix the permission issues we currently have. I will try to completely remove 4.8.2 and then install 4.9.0, just thought you'd like to know

jarllomax avatar May 18 '22 08:05 jarllomax

@ebriney We are seeing this exact issue just recently, out of the blue, on Windows Server 2019 with DockerMsftProvider and the docker pacakge:

Install-Module -Name DockerMsftProvider -Repository PSGallery -Force Install-Package -Name docker -ProviderName DockerMsftProvider -Force

The docker version is:

Docker version 20.10.9, build 591094d

Previous docker version was 20.10.5.

Does this issue apply to this as well and is there a newer version with a fix?

Update: we found that downgrading to docker 20.10.5 resolved the issue.

djarvis avatar Jun 06 '22 13:06 djarvis

I'm running into a similar issue in 4.9.0. When Docker Desktop Starts it attempts to change ACLs and it is breaking. It will never start again, until I manage to delete my Docker directory via cocker-ci-zap.exe. It appears my image and others have a symlink in it, breaking the start of Docker:

System.Exception: Path contains symlink: D:\ProgramData\Docker\windowsfilter\02334b1348055d79ea9243eb6385402429db79735f45d2fa94c7028981ec6487\Files\docker-entrypoint-initdb.d at Docker.Core.PermissionUtils.CheckPath(String path, Boolean allowDockerAppData) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Core\PermissionUtils.cs:line 272 at Docker.Core.PermissionUtils.CheckACLBeforeRestrictAndCheckSymlink(ILogger logger, String path) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Core\PermissionUtils.cs:line 94 at Docker.Core.PermissionUtils.ApplyACLOnChildren(ILogger logger, String path, Int32 recurse) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Core\PermissionUtils.cs:line 47 at Docker.Core.PermissionUtils.ApplyACLOnChildren(ILogger logger, String path, Int32 recurse) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Core\PermissionUtils.cs:line 63 at Docker.Core.PermissionUtils.ApplyACLOnChildren(ILogger logger, String path, Int32 recurse) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Core\PermissionUtils.cs:line 63 at Docker.Core.PermissionUtils.ApplyACLOnChildren(ILogger logger, String path, Int32 recurse) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Core\PermissionUtils.cs:line 63 at Docker.Core.PermissionUtils.ApplyACLOnChildren(ILogger logger, String path, Int32 recurse) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Core\PermissionUtils.cs:line 63 at Docker.Backend.HttpAPI.WindowsContainersController.FixPermissions(String path) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Backend\HttpAPI\WindowsContainersController.cs:line 72 at Docker.Backend.HttpAPI.WindowsContainersController.Start(WindowsContainerStartRequest request) in C:\workspaces\PR-18120\src\github.com\docker\pinata\win\src\Docker.Backend\HttpAPI\WindowsContainersController.cs:line 37 at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.b__0(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()

So I can never run PostgreSql as a Windows Container.

I have found I need to go back to 4.5.1 to have a stable environment

guyelight avatar Jun 08 '22 12:06 guyelight

I see the "Path contains symlink" issue on the official 4.9.0 release, need to go back to pre 4.7.x also.

pbering avatar Jun 13 '22 14:06 pbering

@djarvis It's not related to the issue in this ticket which only apply to Docker Desktop Is someone can test this build https://desktop-stage.docker.com/win/main/amd64/81206/Docker%20Desktop%20Installer.exe cc @pbering @guyelight

ebriney avatar Jun 15 '22 13:06 ebriney

After installation, docker desktop not starting up.

zhaotq avatar Jun 15 '22 20:06 zhaotq

@ebriney I've testet the 4.10.0 version and it seems to be working, I can:

  1. build
  2. up
  3. restart and then up still works

I've noticed that there are some debug info being printet during build:

Building solr-init
[2022-06-16T08:25:20.177759700Z][docker-credential-desktop][I] set path configuration to OnHost
[2022-06-16T08:25:20.254496300Z][docker-credential-desktop][I] set path configuration to OnHost
[2022-06-16T08:25:20.331550300Z][docker-credential-desktop][I] set path configuration to OnHost
[2022-06-16T08:25:20.407578900Z][docker-credential-desktop][I] set path configuration to OnHost
[2022-06-16T08:25:20.484186500Z][docker-credential-desktop][I] set path configuration to OnHost
[2022-06-16T08:25:20.562237900Z][docker-credential-desktop][I] set path configuration to OnHost
[2022-06-16T08:25:20.639151500Z][docker-credential-desktop][I] set path configuration to OnHost
[2022-06-16T08:25:20.716856200Z][docker-credential-desktop][I] set path configuration to OnHost
Sending build context to Docker daemon  2.048kB

Guess it some debug stuff of yours?

pbering avatar Jun 16 '22 08:06 pbering

Sorry we had an issue on main and the build I have provided has a bug that's been fixed now, you can try this new build @zhaotq : https://desktop-stage.docker.com/win/main/amd64/81320/Docker%20Desktop%20Installer.exe Thanks @pbering I will check that

ebriney avatar Jun 16 '22 13:06 ebriney

@ebriney your latest build 81320 still works, and now also the UI works (settings, dashboard etc.), debug messages still present though.

pbering avatar Jun 16 '22 13:06 pbering