| 1 |
/* |
| 2 |
* cache.go - Handles cache clearing and management. |
| 3 |
* |
| 4 |
* Copyright 2017 Google Inc. |
| 5 |
* Author: Joe Richey ([email protected]) |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
* use this file except in compliance with the License. You may obtain a copy of |
| 9 |
* the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
* License for the specific language governing permissions and limitations under |
| 17 |
* the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package security |
| 21 |
|
| 22 |
import ( |
| 23 |
"log" |
| 24 |
"os" |
| 25 |
|
| 26 |
"golang.org/x/sys/unix" |
| 27 |
) |
| 28 |
|
| 29 |
// DropFilesystemCache instructs the kernel to free the reclaimable inodes and |
| 30 |
// dentries. This has the effect of making encrypted directories whose keys are |
| 31 |
// not present no longer accessible. Requires root privileges. |
| 32 |
func DropFilesystemCache() error { |
| 33 |
// Dirty reclaimable inodes must be synced so that they will be freed. |
| 34 |
log.Print("syncing changes to filesystem") |
| 35 |
unix.Sync() |
| 36 |
|
| 37 |
// See: https://www.kernel.org/doc/Documentation/sysctl/vm.txt |
| 38 |
log.Print("freeing reclaimable inodes and dentries") |
| 39 |
file, err := os.OpenFile("/proc/sys/vm/drop_caches", os.O_WRONLY|os.O_SYNC, 0) |
| 40 |
if err != nil { |
| 41 |
return err |
| 42 |
} |
| 43 |
defer file.Close() |
| 44 |
// "2" just frees the reclaimable inodes and dentries. The associated |
| 45 |
// pages to these inodes will be freed. We do not need to free the |
| 46 |
// entire pagecache (as this will severely impact performance). |
| 47 |
_, err = file.WriteString("2") |
| 48 |
return err |
| 49 |
} |