Skip to content

Commit 12bb303

Browse files
committed
Some cleanup of OMNS.
1 parent dc21a6c commit 12bb303

3 files changed

Lines changed: 42 additions & 40 deletions

File tree

NtApiDotNet/NtObjectWithDuplicate.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,17 @@ public NtResult<O> Duplicate(A access, bool throw_on_error)
188188
/// <returns>The duplicated object</returns>
189189
public O Duplicate()
190190
{
191-
return Duplicate(default, AttributeFlags.None, DuplicateObjectOptions.SameAccess, true).Result;
191+
return Duplicate(true).Result;
192+
}
193+
194+
/// <summary>
195+
/// Duplicate the object with same access rights
196+
/// </summary>
197+
/// <param name="throw_on_error">True to throw on error.</param>
198+
/// <returns>The duplicated object</returns>
199+
public NtResult<O> Duplicate(bool throw_on_error)
200+
{
201+
return Duplicate(default, AttributeFlags.None, DuplicateObjectOptions.SameAccess, throw_on_error);
192202
}
193203

194204
private O ShallowClone(SafeKernelObjectHandle handle, bool query_basic_info)

NtObjectManager/NtDirectoryEntry.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace NtObjectManager
2222
/// </summary>
2323
public class NtDirectoryEntry
2424
{
25-
private readonly NtDirectory _base_directory;
25+
private readonly NtObject _root;
2626
private SecurityDescriptor _sd;
2727
private string _symlink_target;
2828
private Enum _maximum_granted_access;
@@ -51,7 +51,7 @@ private void PopulateData()
5151

5252
if (obj is NtSymbolicLink link && link.IsAccessGranted(SymbolicLinkAccessRights.Query))
5353
{
54-
_symlink_target = link.GetTarget(false).GetResultOrDefault();
54+
_symlink_target = link.GetTarget(false).GetResultOrDefault(string.Empty);
5555
}
5656

5757
_maximum_granted_access = obj.GrantedAccessMask.ToSpecificAccess(obj.NtType.AccessRightsType);
@@ -133,7 +133,7 @@ public Enum MaximumGrantedAccess
133133
/// <exception cref="System.ArgumentException">Thrown if invalid typename.</exception>
134134
public NtResult<NtObject> ToObject(bool throw_on_error)
135135
{
136-
return NtObject.OpenWithType(TypeName, RelativePath, _base_directory,
136+
return NtObject.OpenWithType(TypeName, RelativePath, _root,
137137
AttributeFlags.CaseInsensitive, GenericAccessRights.MaximumAllowed, null, throw_on_error);
138138
}
139139

@@ -148,12 +148,12 @@ public NtObject ToObject()
148148
return ToObject(true).Result;
149149
}
150150

151-
internal NtDirectoryEntry(NtDirectory base_directory, string relative_path, string name, string typename)
151+
internal NtDirectoryEntry(NtObject root, string relative_path, string name, string typename)
152152
{
153153
Name = name;
154154
TypeName = typename;
155155
RelativePath = relative_path;
156-
_base_directory = base_directory;
156+
_root = root;
157157

158158
switch (typename.ToLower())
159159
{

NtObjectManager/NtObjectManagerProvider.cs

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ private static string NTPathToPS(string path)
7070
/// <returns>The list of default drives.</returns>
7171
protected override Collection<PSDriveInfo> InitializeDefaultDrives()
7272
{
73-
PSDriveInfo drive = new PSDriveInfo("NtObject", this.ProviderInfo, GLOBAL_ROOT, "NT Object Manager Root Directory", null);
73+
PSDriveInfo drive = new PSDriveInfo("NtObject", ProviderInfo, GLOBAL_ROOT, "NT Object Manager Root Directory", null);
7474
int session_id = Process.GetCurrentProcess().SessionId;
75-
string base_dir = null;
75+
string base_dir;
7676
if (session_id == 0)
7777
{
7878
base_dir = GLOBAL_ROOT + "BaseNamedObjects";
@@ -223,23 +223,23 @@ private string GetRelativePath(string path)
223223

224224
private ObjectManagerPSDriveInfo GetDrive()
225225
{
226-
return (ObjectManagerPSDriveInfo)this.PSDriveInfo;
226+
return (ObjectManagerPSDriveInfo)PSDriveInfo;
227227
}
228228

229-
private NtDirectory GetPathDirectory(string path)
229+
private NtResult<NtDirectory> GetPathDirectory(string path, bool throw_on_error)
230230
{
231231
int last_slash = path.LastIndexOf('\\');
232232
if (last_slash == -1)
233233
{
234-
return GetDrive().DirectoryRoot.Duplicate();
234+
return GetDrive().DirectoryRoot.Duplicate(throw_on_error);
235235
}
236236
else
237237
{
238238
NtDirectory dir = GetDrive().DirectoryRoot;
239239
string base_path = path.Substring(0, last_slash);
240240

241241
return NtDirectory.Open(base_path,
242-
dir, DirectoryAccessRights.MaximumAllowed);
242+
dir, DirectoryAccessRights.MaximumAllowed, throw_on_error);
243243
}
244244
}
245245

@@ -286,17 +286,14 @@ protected override bool ItemExists(string path)
286286
{
287287
return true;
288288
}
289-
290-
try
289+
290+
using (var dir = GetPathDirectory(path, false))
291291
{
292-
using (NtDirectory dir = GetPathDirectory(path))
292+
if (dir.IsSuccess)
293293
{
294-
exists = GetEntry(dir, path) != null;
294+
exists = GetEntry(dir.Result, path) != null;
295295
}
296296
}
297-
catch (NtException)
298-
{
299-
}
300297

301298
// If we can't find it indirectly, at least see if there's a directory with this name.
302299
return exists || GetDrive().DirectoryRoot.DirectoryExists(path);
@@ -323,18 +320,15 @@ protected override bool IsItemContainer(string path)
323320
return true;
324321
}
325322

326-
try
323+
using (var dir = GetPathDirectory(path, false))
327324
{
328-
using (NtDirectory dir = GetPathDirectory(path))
325+
if (dir.IsSuccess)
329326
{
330-
ObjectDirectoryInformation dir_info = GetEntry(dir, path);
327+
ObjectDirectoryInformation dir_info = GetEntry(dir.Result, path);
331328
is_container = dir_info != null
332329
&& dir_info.NtTypeName.Equals("directory", StringComparison.OrdinalIgnoreCase);
333330
}
334331
}
335-
catch (NtException)
336-
{
337-
}
338332

339333
return is_container || GetDrive().DirectoryRoot.DirectoryExists(path);
340334
}
@@ -452,16 +446,18 @@ protected override void GetItem(string path)
452446
}
453447

454448
string relative_path = GetRelativePath(PSPathToNT(path));
455-
using (NtDirectory dir = GetPathDirectory(relative_path))
449+
using (var dir = GetPathDirectory(relative_path, false))
456450
{
451+
if (!dir.IsSuccess)
452+
return;
457453
if (relative_path.Length == 0)
458454
{
459455
WriteItemObject(new NtDirectoryEntry(GetDrive().DirectoryRoot, relative_path, string.Empty, "Directory"),
460456
NTPathToPS(BuildDrivePath(relative_path)), true);
461457
}
462458
else
463459
{
464-
ObjectDirectoryInformation dir_info = GetEntry(dir, relative_path);
460+
ObjectDirectoryInformation dir_info = GetEntry(dir.Result, relative_path);
465461
if (dir_info != null)
466462
{
467463
WriteItemObject(new NtDirectoryEntry(GetDrive().DirectoryRoot, relative_path, dir_info.Name, dir_info.NtTypeName),
@@ -535,15 +531,11 @@ private void AddMatches(NtDirectory root, string base_path, IEnumerable<string>
535531
{
536532
foreach (ObjectDirectoryInformation entry in matching_entries)
537533
{
538-
try
539-
{
540-
using (NtDirectory dir = NtDirectory.Open(entry.Name, root, DirectoryAccessRights.Query))
541-
{
542-
AddMatches(dir, base_path + entry.Name + @"\", remaining.Skip(1), matches);
543-
}
544-
}
545-
catch (NtException)
534+
using (var dir = NtDirectory.Open(entry.Name, root, DirectoryAccessRights.Query, false))
546535
{
536+
if (!dir.IsSuccess)
537+
continue;
538+
AddMatches(dir.Result, base_path + entry.Name + @"\", remaining.Skip(1), matches);
547539
}
548540
}
549541
}
@@ -650,15 +642,15 @@ protected override void NewItem(string path, string itemTypeName, object newItem
650642
void ISecurityDescriptorCmdletProvider.GetSecurityDescriptor(string path, AccessControlSections includeSections)
651643
{
652644
string relative_path = GetRelativePath(PSPathToNT(path));
653-
using (NtDirectory dir = GetPathDirectory(relative_path))
645+
using (var dir = GetPathDirectory(relative_path, true))
654646
{
655647
if (relative_path.Length == 0)
656648
{
657-
WriteItemObject(new GenericObjectSecurity(dir, includeSections), path, true);
649+
WriteItemObject(new GenericObjectSecurity(dir.Result, includeSections), path, true);
658650
}
659651
else
660652
{
661-
ObjectDirectoryInformation dir_info = GetEntry(dir, relative_path);
653+
ObjectDirectoryInformation dir_info = GetEntry(dir.Result, relative_path);
662654
if (dir_info == null)
663655
{
664656
throw new NtException(NtStatus.STATUS_OBJECT_NAME_NOT_FOUND);
@@ -677,9 +669,9 @@ void ISecurityDescriptorCmdletProvider.SetSecurityDescriptor(string path, Object
677669
if (securityDescriptor is GenericObjectSecurity obj_security)
678670
{
679671
string relative_path = GetRelativePath(PSPathToNT(path));
680-
using (NtDirectory dir = GetPathDirectory(relative_path))
672+
using (var dir = GetPathDirectory(relative_path, true))
681673
{
682-
ObjectDirectoryInformation dir_info = GetEntry(dir, relative_path);
674+
ObjectDirectoryInformation dir_info = GetEntry(dir.Result, relative_path);
683675
if (dir_info == null)
684676
{
685677
throw new NtException(NtStatus.STATUS_OBJECT_NAME_NOT_FOUND);

0 commit comments

Comments
 (0)