將安全性指派給 IRP_MJ_CREATE 上的新檔案
建立處理的最後一項工作是將安全性指派給新檔案。 雖然 Windows 安全性模型支援繼承 (個別 ACE 專案會以在建立新檔案或目錄時加以繼承的方式標示) 這會在檔案系統外部實作。 因此,檔案系統內的大量邏輯是專用於儲存新的安全性描述元。 以下是範例常式:
NTSTATUS FsdAssignInitialSecurity( PIRP_CONTEXT IrpContext,
PFCB Fcb, PFCB Directory)
{
NTSTATUS status = STATUS_SUCCESS;
BOOLEAN CreateDir = ((IrpContext->IrpSp->Parameters.Create.Options
& FILE_DIRECTORY_FILE)==FILE_DIRECTORY_FILE);
PACCESS_STATE AccessState =
IrpContext->IrpSp->Parameters.Create.SecurityContext->AccessState;
PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
//
// Make sure the parent directory's security descriptor is loaded.
//
(void) FsdLoadSecurityDescriptor(IrpContext, Directory);
//
// don't care about the return code here, as it is handled later
//
if (Directory->SecurityDescriptor == NULL) {
//
// If the parent has no security, then we are outside
// of the normal Windows paradigm.
//
// The child (that is, the target of the create) will also have
// a NULL SD.
//
// Note that you can always assign security to the file object
// explicitly at later on.
//
return STATUS_SUCCESS;
}
//
// Now create the security descriptor.
//
status = SeAssignSecurity(Directory->SecurityDescriptor,
AccessState->SecurityDescriptor,
&SecurityDescriptor,
CreateDir,
&AccessState->SubjectSecurityContext,
IoGetFileObjectGenericMapping(),
PagedPool);
if (!NT_SUCCESS(status)) {
return status;
}
//
// Associate the SD with the file; use our own storage so when
// cleanup occurs it is unnecessary to know if the storage came from the
// security reference monitor.
//
Fcb->SecurityDescriptorLength =
RtlLengthSecurityDescriptor( SecurityDescriptor );
Fcb->SecurityDescriptor = ExAllocatePoolWithTag(PagedPool,
Fcb->SecurityDescriptorLength, 'DSyM');
if (!Fcb->SecurityDescriptor) {
//
// There is no paged pool.
//
SeDeassignSecurity(&SecurityDescriptor);
Fcb->SecurityDescriptorLength = 0;
return STATUS_NO_MEMORY;
}
RtlCopyMemory(Fcb->SecurityDescriptor, SecurityDescriptor,
Fcb->SecurityDescriptorLength);
SeDeassignSecurity(&SecurityDescriptor);
//
// Store the SD persistently (this is file system specific).
//
(void) FsdStoreSecurityDescriptor(IrpContext, Fcb);
return STATUS_SUCCESS;
}
請注意,建構初始安全性描述元的邏輯 (瞭解繼承,例如,檔案系統內不會處理) 。 這與處理檔案系統層內安全性描述元的簡單模型保持一樣。