VirtualBox

source: vbox/trunk/src/VBox/Main/include/MediumImpl.h@ 74442

Last change on this file since 74442 was 74353, checked in by vboxsync, 6 years ago

Main/mediumio: Corrected crypto filtering on write access.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1/* $Id: MediumImpl.h 74353 2018-09-18 21:51:33Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2008-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19#ifndef ____H_MEDIUMIMPL
20#define ____H_MEDIUMIMPL
21
22#include <VBox/vd.h>
23#include "MediumWrap.h"
24#include "VirtualBoxBase.h"
25#include "AutoCaller.h"
26#include "SecretKeyStore.h"
27class Progress;
28class MediumFormat;
29class MediumLockList;
30struct MediumCryptoFilterSettings;
31
32namespace settings
33{
34 struct Medium;
35}
36
37////////////////////////////////////////////////////////////////////////////////
38
39/**
40 * Medium component class for all media types.
41 */
42class ATL_NO_VTABLE Medium :
43 public MediumWrap
44{
45public:
46 DECLARE_EMPTY_CTOR_DTOR(Medium)
47
48 HRESULT FinalConstruct();
49 void FinalRelease();
50
51 enum HDDOpenMode { OpenReadWrite, OpenReadOnly };
52 // have to use a special enum for the overloaded init() below;
53 // can't use AccessMode_T from XIDL because that's mapped to an int
54 // and would be ambiguous
55
56 // public initializer/uninitializer for internal purposes only
57
58 // initializer to create empty medium (VirtualBox::CreateMedium())
59 HRESULT init(VirtualBox *aVirtualBox,
60 const Utf8Str &aFormat,
61 const Utf8Str &aLocation,
62 const Guid &uuidMachineRegistry,
63 const DeviceType_T aDeviceType);
64
65 // initializer for opening existing media
66 // (VirtualBox::OpenMedium(); Machine::AttachDevice())
67 HRESULT init(VirtualBox *aVirtualBox,
68 const Utf8Str &aLocation,
69 HDDOpenMode enOpenMode,
70 bool fForceNewUuid,
71 DeviceType_T aDeviceType);
72
73 // initializer used when loading settings
74 HRESULT initOne(Medium *aParent,
75 DeviceType_T aDeviceType,
76 const Guid &uuidMachineRegistry,
77 const settings::Medium &data,
78 const Utf8Str &strMachineFolder);
79 HRESULT init(VirtualBox *aVirtualBox,
80 Medium *aParent,
81 DeviceType_T aDeviceType,
82 const Guid &uuidMachineRegistry,
83 const settings::Medium &data,
84 const Utf8Str &strMachineFolder,
85 AutoWriteLock &mediaTreeLock);
86
87 // initializer for host floppy/DVD
88 HRESULT init(VirtualBox *aVirtualBox,
89 DeviceType_T aDeviceType,
90 const Utf8Str &aLocation,
91 const Utf8Str &aDescription = Utf8Str::Empty);
92
93 void uninit();
94
95 void i_deparent();
96 void i_setParent(const ComObjPtr<Medium> &pParent);
97
98 // unsafe methods for internal purposes only (ensure there is
99 // a caller and a read lock before calling them!)
100 const ComObjPtr<Medium>& i_getParent() const;
101 const MediaList& i_getChildren() const;
102
103 const Guid& i_getId() const;
104 MediumState_T i_getState() const;
105 MediumVariant_T i_getVariant() const;
106 bool i_isHostDrive() const;
107 const Utf8Str& i_getLocationFull() const;
108 const Utf8Str& i_getFormat() const;
109 const ComObjPtr<MediumFormat> & i_getMediumFormat() const;
110 bool i_isMediumFormatFile() const;
111 uint64_t i_getSize() const;
112 uint64_t i_getLogicalSize() const;
113 DeviceType_T i_getDeviceType() const;
114 MediumType_T i_getType() const;
115 Utf8Str i_getName();
116
117 /* handles caller/locking itself */
118 bool i_addRegistry(const Guid &id);
119 /* handles caller/locking itself, caller is responsible for tree lock */
120 bool i_addRegistryRecursive(const Guid &id);
121 /* handles caller/locking itself */
122 bool i_removeRegistry(const Guid& id);
123 /* handles caller/locking itself, caller is responsible for tree lock */
124 bool i_removeRegistryRecursive(const Guid& id);
125 bool i_isInRegistry(const Guid& id);
126 bool i_getFirstRegistryMachineId(Guid &uuid) const;
127 void i_markRegistriesModified();
128
129 HRESULT i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue);
130
131 HRESULT i_addBackReference(const Guid &aMachineId,
132 const Guid &aSnapshotId = Guid::Empty);
133 HRESULT i_removeBackReference(const Guid &aMachineId,
134 const Guid &aSnapshotId = Guid::Empty);
135
136
137 const Guid* i_getFirstMachineBackrefId() const;
138 const Guid* i_getAnyMachineBackref() const;
139 const Guid* i_getFirstMachineBackrefSnapshotId() const;
140 size_t i_getMachineBackRefCount() const;
141
142#ifdef DEBUG
143 void i_dumpBackRefs();
144#endif
145
146 HRESULT i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath);
147
148 /* handles caller/locking itself */
149 ComObjPtr<Medium> i_getBase(uint32_t *aLevel = NULL);
150 /* handles caller/locking itself */
151 uint32_t i_getDepth();
152
153 bool i_isReadOnly();
154 void i_updateId(const Guid &id);
155
156 void i_saveSettingsOne(settings::Medium &data,
157 const Utf8Str &strHardDiskFolder);
158 HRESULT i_saveSettings(settings::Medium &data,
159 const Utf8Str &strHardDiskFolder);
160
161 HRESULT i_createMediumLockList(bool fFailIfInaccessible,
162 Medium *pToLock,
163 bool fMediumLockWriteAll,
164 Medium *pToBeParent,
165 MediumLockList &mediumLockList);
166
167 HRESULT i_createDiffStorage(ComObjPtr<Medium> &aTarget,
168 MediumVariant_T aVariant,
169 MediumLockList *pMediumLockList,
170 ComObjPtr<Progress> *aProgress,
171 bool aWait);
172 Utf8Str i_getPreferredDiffFormat();
173 MediumVariant_T i_getPreferredDiffVariant();
174
175 HRESULT i_close(AutoCaller &autoCaller);
176 HRESULT i_unlockRead(MediumState_T *aState);
177 HRESULT i_unlockWrite(MediumState_T *aState);
178 HRESULT i_deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait);
179 HRESULT i_markForDeletion();
180 HRESULT i_unmarkForDeletion();
181 HRESULT i_markLockedForDeletion();
182 HRESULT i_unmarkLockedForDeletion();
183
184 HRESULT i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
185 bool &fMergeForward);
186
187 HRESULT i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
188 const Guid *aMachineId,
189 const Guid *aSnapshotId,
190 bool fLockMedia,
191 bool &fMergeForward,
192 ComObjPtr<Medium> &pParentForTarget,
193 MediumLockList * &aChildrenToReparent,
194 MediumLockList * &aMediumLockList);
195 HRESULT i_mergeTo(const ComObjPtr<Medium> &pTarget,
196 bool fMergeForward,
197 const ComObjPtr<Medium> &pParentForTarget,
198 MediumLockList *aChildrenToReparent,
199 MediumLockList *aMediumLockList,
200 ComObjPtr<Progress> *aProgress,
201 bool aWait);
202 void i_cancelMergeTo(MediumLockList *aChildrenToReparent,
203 MediumLockList *aMediumLockList);
204
205 HRESULT i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent);
206
207 HRESULT i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
208 const ComObjPtr<Progress> &aProgress, bool fSparse);
209
210 HRESULT i_exportFile(const char *aFilename,
211 const ComObjPtr<MediumFormat> &aFormat,
212 MediumVariant_T aVariant,
213 SecretKeyStore *pKeyStore,
214 RTVFSIOSTREAM hVfsIosDst,
215 const ComObjPtr<Progress> &aProgress);
216 HRESULT i_importFile(const char *aFilename,
217 const ComObjPtr<MediumFormat> &aFormat,
218 MediumVariant_T aVariant,
219 RTVFSIOSTREAM hVfsIosSrc,
220 const ComObjPtr<Medium> &aParent,
221 const ComObjPtr<Progress> &aProgress);
222
223 HRESULT i_cloneToEx(const ComObjPtr<Medium> &aTarget, MediumVariant_T aVariant,
224 const ComObjPtr<Medium> &aParent, IProgress **aProgress,
225 uint32_t idxSrcImageSame, uint32_t idxDstImageSame);
226
227 const Utf8Str& i_getKeyId();
228
229 HRESULT i_openForIO(bool fWritable, SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
230 struct MediumCryptoFilterSettings *pCryptoSettings);
231
232private:
233
234 // wrapped IMedium properties
235 HRESULT getId(com::Guid &aId);
236 HRESULT getDescription(AutoCaller &autoCaller, com::Utf8Str &aDescription);
237 HRESULT setDescription(AutoCaller &autoCaller, const com::Utf8Str &aDescription);
238 HRESULT getState(MediumState_T *aState);
239 HRESULT getVariant(std::vector<MediumVariant_T> &aVariant);
240 HRESULT getLocation(com::Utf8Str &aLocation);
241 HRESULT setLocation(const com::Utf8Str &aLocation);
242 HRESULT getName(com::Utf8Str &aName);
243 HRESULT getDeviceType(DeviceType_T *aDeviceType);
244 HRESULT getHostDrive(BOOL *aHostDrive);
245 HRESULT getSize(LONG64 *aSize);
246 HRESULT getFormat(com::Utf8Str &aFormat);
247 HRESULT getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat);
248 HRESULT getType(AutoCaller &autoCaller, MediumType_T *aType);
249 HRESULT setType(AutoCaller &autoCaller, MediumType_T aType);
250 HRESULT getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes);
251 HRESULT getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent);
252 HRESULT getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren);
253 HRESULT getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase);
254 HRESULT getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly);
255 HRESULT getLogicalSize(LONG64 *aLogicalSize);
256 HRESULT getAutoReset(BOOL *aAutoReset);
257 HRESULT setAutoReset(BOOL aAutoReset);
258 HRESULT getLastAccessError(com::Utf8Str &aLastAccessError);
259 HRESULT getMachineIds(std::vector<com::Guid> &aMachineIds);
260
261 // wrapped IMedium methods
262 HRESULT setIds(AutoCaller &aAutoCaller,
263 BOOL aSetImageId,
264 const com::Guid &aImageId,
265 BOOL aSetParentId,
266 const com::Guid &aParentId);
267 HRESULT refreshState(AutoCaller &aAutoCaller,
268 MediumState_T *aState);
269 HRESULT getSnapshotIds(const com::Guid &aMachineId,
270 std::vector<com::Guid> &aSnapshotIds);
271 HRESULT lockRead(ComPtr<IToken> &aToken);
272 HRESULT lockWrite(ComPtr<IToken> &aToken);
273 HRESULT close(AutoCaller &aAutoCaller);
274 HRESULT getProperty(const com::Utf8Str &aName,
275 com::Utf8Str &aValue);
276 HRESULT setProperty(const com::Utf8Str &aName,
277 const com::Utf8Str &aValue);
278 HRESULT getProperties(const com::Utf8Str &aNames,
279 std::vector<com::Utf8Str> &aReturnNames,
280 std::vector<com::Utf8Str> &aReturnValues);
281 HRESULT setProperties(const std::vector<com::Utf8Str> &aNames,
282 const std::vector<com::Utf8Str> &aValues);
283 HRESULT createBaseStorage(LONG64 aLogicalSize,
284 const std::vector<MediumVariant_T> &aVariant,
285 ComPtr<IProgress> &aProgress);
286 HRESULT deleteStorage(ComPtr<IProgress> &aProgress);
287 HRESULT createDiffStorage(AutoCaller &autoCaller,
288 const ComPtr<IMedium> &aTarget,
289 const std::vector<MediumVariant_T> &aVariant,
290 ComPtr<IProgress> &aProgress);
291 HRESULT mergeTo(const ComPtr<IMedium> &aTarget,
292 ComPtr<IProgress> &aProgress);
293 HRESULT cloneTo(const ComPtr<IMedium> &aTarget,
294 const std::vector<MediumVariant_T> &aVariant,
295 const ComPtr<IMedium> &aParent,
296 ComPtr<IProgress> &aProgress);
297 HRESULT cloneToBase(const ComPtr<IMedium> &aTarget,
298 const std::vector<MediumVariant_T> &aVariant,
299 ComPtr<IProgress> &aProgress);
300 HRESULT moveTo(AutoCaller &autoCaller,
301 const com::Utf8Str &aLocation,
302 ComPtr<IProgress> &aProgress);
303 HRESULT compact(ComPtr<IProgress> &aProgress);
304 HRESULT resize(LONG64 aLogicalSize,
305 ComPtr<IProgress> &aProgress);
306 HRESULT reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress);
307 HRESULT changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
308 const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
309 ComPtr<IProgress> &aProgress);
310 HRESULT getEncryptionSettings(AutoCaller &autoCaller, com::Utf8Str &aCipher, com::Utf8Str &aPasswordId);
311 HRESULT checkEncryptionPassword(const com::Utf8Str &aPassword);
312 HRESULT openForIO(BOOL aWritable, com::Utf8Str const &aPassword, ComPtr<IMediumIO> &aMediumIO);
313
314 // Private internal nmethods
315 HRESULT i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller);
316 HRESULT i_canClose();
317 HRESULT i_unregisterWithVirtualBox();
318 HRESULT i_setStateError();
319 HRESULT i_setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str::Empty);
320 HRESULT i_setFormat(const Utf8Str &aFormat);
321 VDTYPE i_convertDeviceType();
322 DeviceType_T i_convertToDeviceType(VDTYPE enmType);
323 Utf8Str i_vdError(int aVRC);
324
325 bool i_isPropertyForFilter(const com::Utf8Str &aName);
326
327 HRESULT i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
328 std::vector<com::Utf8Str> &aReturnValues);
329
330 HRESULT i_preparationForMoving(const Utf8Str &aLocation);
331 bool i_isMoveOperation(const ComObjPtr<Medium> &pTarget) const;
332 bool i_resetMoveOperationData();
333 Utf8Str i_getNewLocationForMoving() const;
334
335 static DECLCALLBACK(void) i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
336 const char *pszFormat, va_list va);
337 static DECLCALLBACK(bool) i_vdConfigAreKeysValid(void *pvUser,
338 const char *pszzValid);
339 static DECLCALLBACK(int) i_vdConfigQuerySize(void *pvUser, const char *pszName,
340 size_t *pcbValue);
341 static DECLCALLBACK(int) i_vdConfigQuery(void *pvUser, const char *pszName,
342 char *pszValue, size_t cchValue);
343 static DECLCALLBACK(int) i_vdTcpSocketCreate(uint32_t fFlags, PVDSOCKET pSock);
344 static DECLCALLBACK(int) i_vdTcpSocketDestroy(VDSOCKET Sock);
345 static DECLCALLBACK(int) i_vdTcpClientConnect(VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
346 RTMSINTERVAL cMillies);
347 static DECLCALLBACK(int) i_vdTcpClientClose(VDSOCKET Sock);
348 static DECLCALLBACK(bool) i_vdTcpIsClientConnected(VDSOCKET Sock);
349 static DECLCALLBACK(int) i_vdTcpSelectOne(VDSOCKET Sock, RTMSINTERVAL cMillies);
350 static DECLCALLBACK(int) i_vdTcpRead(VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
351 static DECLCALLBACK(int) i_vdTcpWrite(VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
352 static DECLCALLBACK(int) i_vdTcpSgWrite(VDSOCKET Sock, PCRTSGBUF pSgBuf);
353 static DECLCALLBACK(int) i_vdTcpFlush(VDSOCKET Sock);
354 static DECLCALLBACK(int) i_vdTcpSetSendCoalescing(VDSOCKET Sock, bool fEnable);
355 static DECLCALLBACK(int) i_vdTcpGetLocalAddress(VDSOCKET Sock, PRTNETADDR pAddr);
356 static DECLCALLBACK(int) i_vdTcpGetPeerAddress(VDSOCKET Sock, PRTNETADDR pAddr);
357
358 static DECLCALLBACK(bool) i_vdCryptoConfigAreKeysValid(void *pvUser,
359 const char *pszzValid);
360 static DECLCALLBACK(int) i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName,
361 size_t *pcbValue);
362 static DECLCALLBACK(int) i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
363 char *pszValue, size_t cchValue);
364
365 static DECLCALLBACK(int) i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
366 const uint8_t **ppbKey, size_t *pcbKey);
367 static DECLCALLBACK(int) i_vdCryptoKeyRelease(void *pvUser, const char *pszId);
368 static DECLCALLBACK(int) i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword);
369 static DECLCALLBACK(int) i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId);
370 static DECLCALLBACK(int) i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore);
371 static DECLCALLBACK(int) i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
372 const uint8_t *pbDek, size_t cbDek);
373
374 class Task;
375 class CreateBaseTask;
376 class CreateDiffTask;
377 class CloneTask;
378 class MoveTask;
379 class CompactTask;
380 class ResizeTask;
381 class ResetTask;
382 class DeleteTask;
383 class MergeTask;
384 class ImportTask;
385 class EncryptTask;
386 friend class Task;
387 friend class CreateBaseTask;
388 friend class CreateDiffTask;
389 friend class CloneTask;
390 friend class MoveTask;
391 friend class CompactTask;
392 friend class ResizeTask;
393 friend class ResetTask;
394 friend class DeleteTask;
395 friend class MergeTask;
396 friend class ImportTask;
397 friend class EncryptTask;
398
399 HRESULT i_taskCreateBaseHandler(Medium::CreateBaseTask &task);
400 HRESULT i_taskCreateDiffHandler(Medium::CreateDiffTask &task);
401 HRESULT i_taskMergeHandler(Medium::MergeTask &task);
402 HRESULT i_taskCloneHandler(Medium::CloneTask &task);
403 HRESULT i_taskMoveHandler(Medium::MoveTask &task);
404 HRESULT i_taskDeleteHandler(Medium::DeleteTask &task);
405 HRESULT i_taskResetHandler(Medium::ResetTask &task);
406 HRESULT i_taskCompactHandler(Medium::CompactTask &task);
407 HRESULT i_taskResizeHandler(Medium::ResizeTask &task);
408 HRESULT i_taskImportHandler(Medium::ImportTask &task);
409 HRESULT i_taskEncryptHandler(Medium::EncryptTask &task);
410
411 void i_taskEncryptSettingsSetup(struct MediumCryptoFilterSettings *pSettings, const char *pszCipher,
412 const char *pszKeyStore, const char *pszPassword,
413 bool fCreateKeyStore);
414
415 struct Data; // opaque data struct, defined in MediumImpl.cpp
416 Data *m;
417};
418
419
420/**
421 * Settings for a crypto filter instance.
422 */
423struct MediumCryptoFilterSettings
424{
425 MediumCryptoFilterSettings()
426 : fCreateKeyStore(false),
427 pszPassword(NULL),
428 pszKeyStore(NULL),
429 pszKeyStoreLoad(NULL),
430 pbDek(NULL),
431 cbDek(0),
432 pszCipher(NULL),
433 pszCipherReturned(NULL)
434 { }
435
436 bool fCreateKeyStore;
437 const char *pszPassword;
438 char *pszKeyStore;
439 const char *pszKeyStoreLoad;
440
441 const uint8_t *pbDek;
442 size_t cbDek;
443 const char *pszCipher;
444
445 /** The cipher returned by the crypto filter. */
446 char *pszCipherReturned;
447
448 PVDINTERFACE vdFilterIfaces;
449
450 VDINTERFACECONFIG vdIfCfg;
451 VDINTERFACECRYPTO vdIfCrypto;
452};
453
454
455
456#endif /* !____H_MEDIUMIMPL */
457
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette