aboutsummaryrefslogtreecommitdiff
path: root/hal/fake_nvram_storage.cpp
blob: e525462f42fb349acdbd3f66ce4df4ec16c9ca24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <nvram/core/storage.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/unique_fd.h>

#include <nvram/core/logger.h>

// An NVRAM storage layer implementation backed by the file system.
//
// NOTE: This does not meet the tamper evidence requirements for
// access-controlled NVRAM implementations, since the file system can't provide
// sufficient protection against tampering by attackers.

namespace {

// Name of the storage object holding the header.
const char kHeaderFileName[] = "header";

// Pattern for space data storage object names.
const char kSpaceDataFileNamePattern[] = "space_%08x";

// Temporary file name used in write-rename atomic write operations.
const char kTempFileName[] = "temp";

// Maximum size of objects we're willing to read and write.
const off_t kMaxFileSize = 2048;

// Buffer size for formatting names.
using NameBuffer = char[16];

// Global data directory descriptor.
int g_data_dir_fd = -1;

// Formats the storage object name for the given space index.
bool FormatSpaceFileName(NameBuffer name, uint32_t index) {
  int ret =
      snprintf(name, sizeof(NameBuffer), kSpaceDataFileNamePattern, index);
  return ret >= 0 && ret < static_cast<int>(sizeof(NameBuffer));
};

nvram::storage::Status DeleteFile(const char* name) {
  if (TEMP_FAILURE_RETRY(unlinkat(g_data_dir_fd, name, 0))) {
    if (errno == ENOENT) {
      return nvram::storage::Status::kNotFound;
    }
    PLOG(ERROR) << "Failed to remove " << name;
    return nvram::storage::Status::kStorageError;
  }

  return nvram::storage::Status::kSuccess;
}

// Loads the storage object identified by |name|.
nvram::storage::Status LoadFile(const char* name, nvram::Blob* blob) {
  android::base::unique_fd data_file_fd(
      TEMP_FAILURE_RETRY(openat(g_data_dir_fd, name, O_RDONLY)));
  if (data_file_fd.get() < 0) {
    if (errno == ENOENT) {
      return nvram::storage::Status::kNotFound;
    }
    PLOG(ERROR) << "Failed to open " << name;
    return nvram::storage::Status::kStorageError;
  }

  struct stat data_file_stat;
  if (TEMP_FAILURE_RETRY(fstat(data_file_fd.get(), &data_file_stat))) {
    PLOG(ERROR) << "Failed to stat " << name;
    return nvram::storage::Status::kStorageError;
  }

  if (data_file_stat.st_size > kMaxFileSize) {
    LOG(ERROR) << "Bad size for " << name << ":" << data_file_stat.st_size;
    return nvram::storage::Status::kStorageError;
  }

  if (!blob->Resize(data_file_stat.st_size)) {
    LOG(ERROR) << "Failed to allocate read buffer for " << name;
    return nvram::storage::Status::kStorageError;
  }

  if (!android::base::ReadFully(data_file_fd.get(), blob->data(),
                                blob->size())) {
    PLOG(ERROR) << "Failed to read " << name;
    return nvram::storage::Status::kStorageError;
  }

  return nvram::storage::Status::kSuccess;
}

// Writes blob to the storage object indicated by |name|.
nvram::storage::Status StoreFile(const char* name, const nvram::Blob& blob) {
  android::base::unique_fd data_file_fd(TEMP_FAILURE_RETRY(
      openat(g_data_dir_fd, kTempFileName, O_WRONLY | O_CREAT | O_TRUNC,
             S_IRUSR | S_IWUSR)));
  if (data_file_fd.get() < 0) {
    if (errno == ENOENT) {
      return nvram::storage::Status::kNotFound;
    }
    PLOG(ERROR) << "Failed to open " << kTempFileName;
    return nvram::storage::Status::kStorageError;
  }

  if (!android::base::WriteFully(data_file_fd.get(), blob.data(),
                                 blob.size())) {
    PLOG(ERROR) << "Failed to write " << kTempFileName;
    DeleteFile(kTempFileName);
    return nvram::storage::Status::kStorageError;
  }

  // Force the file contents to be written to disk.
  if (TEMP_FAILURE_RETRY(fdatasync(data_file_fd.get()))) {
    PLOG(ERROR) << "Failed to sync " << kTempFileName;
    DeleteFile(kTempFileName);
    return nvram::storage::Status::kStorageError;
  }

  data_file_fd.reset();

  // Move the file into place.
  if (TEMP_FAILURE_RETRY(
          renameat(g_data_dir_fd, kTempFileName, g_data_dir_fd, name))) {
    PLOG(ERROR) << "Failed to move " << kTempFileName << " to " << name;
    DeleteFile(kTempFileName);
    return nvram::storage::Status::kStorageError;
  }

  // Force the directory meta data to be written to disk.
  if (TEMP_FAILURE_RETRY(fsync(g_data_dir_fd))) {
    PLOG(ERROR) << "Failed to sync data directory";
    return nvram::storage::Status::kStorageError;
  }

  return nvram::storage::Status::kSuccess;
}

}  // namespace

// Initializes the storage layer with the provided data directory descriptor.
void InitStorage(int data_dir_fd) {
  g_data_dir_fd = data_dir_fd;
}

namespace nvram {
namespace storage {

Status LoadHeader(Blob* blob) {
  return LoadFile(kHeaderFileName, blob);
}

Status StoreHeader(const Blob& blob) {
  return StoreFile(kHeaderFileName, blob);
}

Status LoadSpace(uint32_t index, Blob* blob) {
  NameBuffer name;
  if (!FormatSpaceFileName(name, index)) {
    return Status::kStorageError;
  }
  return LoadFile(name, blob);
}

Status StoreSpace(uint32_t index, const Blob& blob) {
  NameBuffer name;
  if (!FormatSpaceFileName(name, index)) {
    return Status::kStorageError;
  }
  return StoreFile(name, blob);
}

Status DeleteSpace(uint32_t index) {
  NameBuffer name;
  if (!FormatSpaceFileName(name, index)) {
    return Status::kStorageError;
  }

  return DeleteFile(name);
}

}  // namespace storage
}  // namespace nvram