From 2e92a84fa8dd9047434f8496eb30d9f4fec63eae Mon Sep 17 00:00:00 2001
From: Aaron Lehmann <aaron.lehmann@docker.com>
Date: Wed, 30 Mar 2016 19:34:51 -0700
Subject: [PATCH] Fix layer store Get locking

Get was calling getReference without layerL held. This meant writes to
the references map could race. Such races are dangerous because they can
corrupt the map and crash the process.

Fixes #21616
Fixes #21674

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
(cherry picked from commit 0538981c31a714a4183e846a4b512deb7879cc29)
---
 layer/layer_store.go | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/layer/layer_store.go b/layer/layer_store.go
index a05b4a38b9..73a1b34c93 100644
--- a/layer/layer_store.go
+++ b/layer/layer_store.go
@@ -334,7 +334,10 @@ func (ls *layerStore) get(l ChainID) *roLayer {
 }
 
 func (ls *layerStore) Get(l ChainID) (Layer, error) {
-	layer := ls.get(l)
+	ls.layerL.Lock()
+	defer ls.layerL.Unlock()
+
+	layer := ls.getWithoutLock(l)
 	if layer == nil {
 		return nil, ErrLayerDoesNotExist
 	}