Protecting Objects

 < Day Day Up > 

Object protection and access logging is the essence of discretionary access control and auditing. The objects that can be protected on Windows include files, devices, mailslots, pipes (named and anonymous), jobs, processes, threads, events, keyed events, event pairs, mutexes, semaphores, shared memory sections, I/O completion ports, LPC ports, waitable timers, access tokens, volumes, window stations, desktops, network shares, services, registry keys, printers, and Active Directory objects.

Because system resources that are exported to user mode (and hence require security validation) are implemented as objects in kernel mode, the Windows object manager plays a key role in enforcing object security. (For more information on the object manager, see Chapter 3.) To control who can manipulate an object, the security system must first be sure of each user's identity. This need to guarantee the user's identity is the reason that Windows requires authenticated logon before accessing any system resources. When a process requests a handle to an object, the object manager and the security system use the caller's security identification to determine whether the caller should be assigned a handle that grants the process access to the object it desires.

As discussed later in this chapter, a thread can assume a different security context than that of its process. This mechanism is called impersonation, and when a thread is impersonating, security validation mechanisms use the thread's security context instead of that of the thread's process. When a thread isn't impersonating, security validation falls back on using the security context of the thread's owning process. It's important to keep in mind that all the threads in a process share the same handle table, so when a thread opens an object even if it's impersonating all the threads of the process have access to the object.

Access Checks

The Windows security model requires that a thread specify up front, at the time that it opens an object, what types of actions it wants to perform on the object. The system performs access checks based on a thread's desired access, and if the access is granted, a handle is assigned to the thread's process with which the thread (or other threads in the process) can perform further operations on the object. As explained in Chapter 3, the object manager records the access permissions granted for a handle in the process's handle table.

One event that causes the object manager to perform security access validation is when a process opens an existing object using a name. When an object is opened by name, the object manager performs a lookup of the specified object in the object manager namespace. If the object isn't located in a secondary namespace, such as the configuration manager's registry namespace or a file system driver's file system namespace, the object manager calls the internal function ObpCreateHandle once it locates the object. As its name implies, ObpCreateHandle creates an entry in the process's handle table that becomes associated with the object. However, ObpCreateHandle calls the executive function ExCreateHandle instead to create the handle only if another object manager function, ObpIncrementHandleCount, indicates that the thread has permission to access the object. Another object manager function, ObCheckObject- Access, actually carries out the security access check and returns the results to ObpIncrement- HandleCount.

ObpIncrementHandleCount passes ObCheckObjectAccess the security credentials of the thread opening the object, the types of access to the object that the thread is requesting (read, write, delete, and so forth), and a pointer to the object. ObCheckObjectAccess first locks the object's security and the security context of the thread. The object security lock prevents another thread in the system from changing the object's security while the access check is in progress. The lock on the thread's security context prevents another thread of that process or a different process from altering the security identity of the thread while security validation is in progress. ObCheckObjectAccess then calls the object's security method to obtain the security settings of the object. (See Chapter 3 for a description of object methods.) The call to the security method might invoke a function in a different executive component. However, many executive objects rely on the system's default security management support.

When an executive component defining an object doesn't want to override the SRM's default security policy, it marks the object type as having default security. Whenever the SRM calls an object's security method, it first checks to see whether the object has default security. An object with default security stores its security information in its header, and its security method is SeDefaultObjectMethod. An object that doesn't rely on default security must manage its own security information and supply a specific security method. Objects that rely on default security include mutexes, events, and semaphores. A file object is an example of an object that overrides default security. The I/O manager, which defines the file object type, has the file system driver on which a file resides manage (or choose not to implement) the security for its files. Thus, when the system queries the security on a file object that represents a file on an NTFS volume, the I/O manager file object security method retrieves the file's security using the NTFS file system driver. Note, however, that ObCheckObjectAccess isn't executed when files are opened, because they reside in secondary namespaces; the system invokes a file object's security method only when a thread explicitly queries or sets the security on a file (with the Windows SetFileSecurity or GetFileSecurity functions, for example).

After obtaining an object's security information, ObCheckObjectAccess invokes the SRM function SeAccessCheck. SeAccessCheck is one of the functions at the heart of the Windows security model. Among the input parameters SeAccessCheck accepts are the object's security information, the security identity of the thread as captured by ObCheckObjectAccess, and the access that the thread is requesting. SeAccessCheck returns True or False, depending on whether the thread is granted the access it requested to the object.

Another event that causes the object manager to execute access validation is when a process references an object using an existing handle. Such references often occur indirectly, as when a process calls on a Windows API to manipulate an object and passes an object handle. For example, a thread opening a file can request access to the object that permits it to read from the file. If the thread has permission to access the object in this way, as dictated by its security context and the security settings of the file, the object manager creates a handle representing the file in the handle table of the thread's process. The accesses the process is granted through the handle are stored with the handle by the object manager.

Subsequently, the thread can attempt to write to the file using the WriteFile Windows function, passing the file's handle as a parameter. The system service NtWriteFile, which WriteFile calls via Ntdll.dll, uses the object manager function ObReferenceObjectByHandle to obtain a pointer to the file object from the handle. ObReferenceObjectByHandle accepts the access that the caller wants from the object as a parameter. After finding the handle entry in the process's handle table, ObReferenceObjectByHandle compares the access being requested with the access granted at the time the file was opened. In this case, ObReferenceObjectByHandle will indicate that the write operation should fail because the caller didn't obtain write access when the file was opened.

The Windows security functions also enable Windows applications to define their own private objects and to call on the services of the SRM to enforce the Windows security model on those objects. Many kernel-mode functions that the object manager and other executive components use to protect their own objects are exported as Windows user-mode APIs. The user- mode equivalent of SeAccessCheck is AccessCheck, for example. Windows applications can therefore leverage the flexibility of the security model and transparently integrate with the authentication and administrative interfaces that are present in Windows.

The essence of the SRM's security model is an equation that takes three inputs: the security identity of a thread, the access that the thread wants to an object, and the security settings of the object. The output is either "yes" or "no" and indicates whether or not the security model grants the thread the access it desires. The following sections describe the inputs in more detail and then document the model's access validation algorithm.

Security Identifiers (SIDs)

Instead of using names (which might or might not be unique) to identify entities that perform actions in a system, Windows uses security identifiers (SIDs). Users have SIDs, and so do local and domain groups, local computers, domains, and domain members. A SID is a variable- length numeric value that consists of a SID structure revision number, a 48-bit identifier authority value, and a variable number of 32-bit subauthority or relative identifier (RID) values. The authority value identifies the agent that issued the SID, and this agent is typically a Windows local system or a domain. Subauthority values identify trustees relative to the issuing authority, and RIDs are simply a way for Windows to create unique SIDs based on a common- base SID. Because SIDs are long and Windows takes care to generate truly random values within each SID, it is virtually impossible for Windows to issue the same SID twice on machines or domains anywhere in the world.

When displayed textually, each SID carries an S prefix, and its various components are separated with hyphens:

S-1-5-21-1463437245-1224812800-863842198-1128

In this SID, the revision number is 1, the identifier authority value is 5 (the Windows security authority), and four subauthority values plus one RID (1128) make up the remainder of the SID. This SID is a domain SID, but a local computer on the domain would have a SID with the same revision number, identifier authority value, and number of subauthority values.

When you install Windows, the Windows Setup program issues the computer a SID. Windows assigns SIDs to local accounts on the computer. Each local-account SID is based on the source computer's SID and has a RID at the end. RIDs for user accounts and groups start at 1000 and increase in increments of 1 for each new user or group. Similarly, Dcpromo.exe, the utility used to create a new Windows domain, issues a SID to domains it creates. Windows issues to new domain accounts SIDS that are based on the domain SID and have an appended RID (again starting at 1000 and increasing in increments of 1 for each new user or group). A RID of 1028 indicates that the SID is the 29th SID the domain issued.

Windows issues SIDS that consist of a computer or domain SID with a predefined RID to many predefined accounts and groups. For example, the RID for the administrator account is 500, and the RID for the guest account is 501. A computer's local administrator account, for example, has the computer SID as its base with the RID of 500 appended to it:

S-1-5-21-13124455-12541255-61235125-500

Windows also defines a number of built-in local and domain SIDs to represent groups. For example, a SID that identifies any and every account is the Everyone, or World, SID: S-1 1 0. Another example of a group that a SID can represent is the network group, which is the group that represents users who have logged on to a machine from the network. The network-group SID is S-1-5-2. Table 8-2, reproduced here from the Platform SDK documentation, shows some basic well-known SIDs, their numeric values, and their use.

Table 8-2. Well-Known SIDs

SID

Group

Use

S-1 1 0

Everyone

A group that includes all users.

S-1-2-0

Local

Users who log on to terminals locally (physically) connected to the system.

S-1 3 0

Creator Owner ID

A security identifier to be replaced by the security identifier of the user who created a new object. This SID is used in inheritable ACEs.

S-1 3 1

Creator Group ID

Identifies a security identifier to be replaced by the primary-group SID of the user who created a new object. Use this SID in inheritable ACEs.


Finally, Winlogon creates a unique logon SID for each interactive logon session. A typical use of a logon SID is in an access-control entry (ACE) that allows access for the duration of a client's logon session. For example, a Windows service can use the LogonUser function to start a new logon session. The LogonUser function returns an access token from which the service can extract the logon SID. The service can then use the SID in an ACE that allows the client's logon session to access the interactive window station and desktop. The SID for a logon session is S-1-5-5-0, and the RID is randomly generated.

EXPERIMENT: Using PsGetSid to View Account SIDs

You can easily see the SID representation for any account you're using by running the PsGetSid utility (from http://www.sysinternals.com). It has the following interface:

C:\>psgetsid -? PsGetSid displays the machine SID for the local or remote Windows  system. Usage: psgetsid [\\RemoteComputer [-u Username [-p Password]]]  [account | SID]     -u        Specifies optional user name for login to               remote computer.     -p        Specifies optional password for user name. If you  omit this               you will be prompted to enter a hidden password.     account   PsGetSid will report the SID for the specified user  account               rather than the computer.     SID       PsGetSid will report the account for the specified SID.

PsGetSid's options allow you to translate machine and user account names to their corresponding SIDs and vice versa.

If you run PsGetSid with no options, it prints the SID assigned to the local computer. By using the fact that the Administrator's account always has a RID of 500, you can determine the name assigned to the account (in cases where a system administrator has renamed the account for security reasons) simply by passing the machine SID appended with "-500" as PsGetSid's command-line argument.

To obtain the SID of a domain account, enter the user name with the domain as a prefix:

c:\>psgetsid redmond\daryl

You can determine the SID of a domain by specifying the domain's name as the argument to PsGetSid:

c:\>psgetsid Redmond

Finally, by examining the RID of your own account, you know at least a number of security accounts equal to the number resulting from subtracting 999 from your RID have been created in your domain or on your local machine (depending on whether you are using a domain or local machine account). You can determine what accounts have been assigned RIDs by passing a SID with the RID you want to query to PsGetSid. If PsGetSid reports that no mapping between the SID and an account name was possible and the RID is lower than that of your account, you know that the account assigned the RID has been deleted.

For example, to find out the name of the account assigned the twenty-eighth RID, pass the domain SID appended with "-1027" to PsGetSid:

c:\>psgetsid S-1-5-21-1787744166-3910675280-2727264193-1027 Account for S-1-5-21-1787744166-3910675280-2727264193-1027: User: redmond\daryl


Tokens

The SRM uses an object called a token (or access token) to identify the security context of a process or thread. A security context consists of information that describes the privileges, accounts, and groups associated with the process or thread. During the logon process (described at the end of this chapter), Winlogon creates an initial token to represent the user logging on and attaches the token to the initial process(es) it starts (by default, Userinit.exe). Because child processes by default inherit a copy of the token of its creator, all processes in the user's session run under the same token. You can also generate a token by using the Windows LogonUser function. You can then use this token to create a process that runs within the security context of the user logged on by the LogonUser function by passing the token to the Windows CreateProcessAsUser function. The CreateProcessWithLogon function also creates a token by creating a new logon session with an initial process. This is how the Runas command launches processes under alternate tokens.

Tokens vary in size because different user accounts have different sets of privileges and associated group accounts. However, all tokens contain the same information, shown in Figure 8-3.

Figure 8-3. Access tokens


The security mechanisms in Windows use two token components to determine what objects can be accessed and what secure operations can be performed. One component comprises the token's user account SID and group SID fields. The SRM uses SIDs to determine whether a process or thread can obtain requested access to a securable object, such as an NTFS file.

The group SIDs in a token signify which groups a user's account is a member of. A server application can disable specific groups to restrict a token's credentials when the server application is performing actions a client requests. Disabling a group produces nearly the same effect as if the group wasn't present in the token. (Disabled SIDs are used as part of security access checks, described later in the chapter.)

The second component in a token that determines what the token's thread or process can do is the privilege array. A token's privilege array is a list of rights associated with the token. An example privilege is the right for the process or thread associated with the token to shut down the computer. Privileges are described in more detail later in this chapter. A token's default primary group field and default discretionary access-control list (DACL) field are security attributes that Windows applies to objects that a process or thread creates when it uses the token. By including security information in tokens, Windows makes it convenient for a process or thread to create objects with standard security attributes, because the process or thread doesn't need to request discrete security information for every object it creates.

Each token's type distinguishes a primary token (a token that identifies the security context of a process) from an impersonation token (a type of token that threads use to temporarily adopt a different security context, usually of another user). Impersonation tokens carry an impersonation level that signifies what type of impersonation is active in the token. (Impersonation is described later in this chapter.)

The remainder of the fields in a token serve informational purposes. The token source field contains a short textual description of the entity that created the token. Programs that want to know where a token originated use the token source to distinguish among sources such as the Windows Session Manager, a network file server, or the remote procedure call (RPC) server. The token identifier is a locally unique identifier (LUID) that the SRM assigns to the token when it creates the token. The Windows executive maintains the executive LUID, a counter it uses to assign a unique numeric identifier to each token.

The token authentication ID is another kind of LUID. A token's creator assigns the token's authentication ID when calling the LsaLogonUser function. If the creator doesn't specify a LUID, Lsass obtains the LUID from the executive LUID. Lsass copies the authentication ID for all tokens descended from an initial logon token. A program can obtain a token's authentication ID to see whether the token belongs to the same logon session as other tokens the program has examined.

The executive LUID refreshes the modified ID every time a token's characteristics are modified. An application can test the modified ID to discover changes in a security context since the context's last use.

Tokens contain an expiration time field that has been present but unused in Windows NT technology since Windows NT 3.1. A future version of Windows might allow for tokens that are valid for a period of time before expiring. Consider a user for which the systems administrator sets an account expiration time. Currently, if the user logs on and remains logged on past the account expiration, the system will let the user continue to access resources. The only way to prevent the user from accessing resources is to forcibly log the user off the machine. If Windows supported token expiration, the system could prevent the user from opening resources past the token expiration time.

EXPERIMENT: Viewing Access Tokens

The kernel debugger dt _TOKEN command displays the format of an internal token object. Although this structure differs from the user-mode token structure returned by Windows API security functions, the fields are similar. For further information on tokens, see the description in the Platform SDK documentation.

The following output is from the kernel debugger's dtTOKEN command:

kd> dt _TOKEN    +0x000 TokenSource      : _TOKEN_SOURCE    +0x010 TokenId          : _LUID    +0x018 AuthenticationId : _LUID    +0x020 ParentTokenId    : _LUID    +0x028 ExpirationTime   : _LARGE_INTEGER    +0x030 TokenLock        : Ptr32 _ERESOURCE    +0x034 ModifiedId       : _LUID    +0x03c SessionId        : Uint4B    +0x040 UserAndGroupCount : Uint4B    +0x044 RestrictedSidCount : Uint4B    +0x048 PrivilegeCount   : Uint4B    +0x04c VariableLength   : Uint4B    +0x050 DynamicCharged   : Uint4B    +0x054 DynamicAvailable : Uint4B    +0x058 DefaultOwnerIndex : Uint4B    +0x05c UserAndGroups    : Ptr32 _SID_AND_ATTRIBUTES    +0x060 RestrictedSids   : Ptr32 _SID_AND_ATTRIBUTES    +0x064 PrimaryGroup     : Ptr32 Void    +0x068 Privileges       : Ptr32 _LUID_AND_ATTRIBUTES    +0x06c DynamicPart      : Ptr32 Uint4B    +0x070 DefaultDacl      : Ptr32 _ACL    +0x074 TokenType        : _TOKEN_TYPE    +0x078 ImpersonationLevel : _SECURITY_IMPERSONATION_LEVEL    +0x07c TokenFlags       : UChar    +0x07d TokenInUse       : UChar    +0x080 ProxyData        : Ptr32 _SECURITY_TOKEN_PROXY_DATA    +0x084 AuditData        : Ptr32 _SECURITY_TOKEN_AUDIT_DATA    +0x088 VariablePart     : Uint4B

You can examine the token for a process with the !token command. You'll find the address of the token in the output of the !process command, as shown here:

kd> !process 3801 !process 380 1 Searchingfor  Process  with Cid == 380 PROCESS ff8027a0 SessionId: 0 Cid: 0380 Peb: 7ffdf000 ParentCid: 0124     DirBase: 06433000 ObjectTable: ff7e0b68 TableSize: 23.     Image:cmd.exe     VadRoot84c30568  Clone 0Private 77.Modified0. Locked  0.     DeviceMap818a3368     Token                            e22bc730     ElapsedTime                      14:22:56.0536     UserTime                         0:00:00.0040     KernelTime                       0:00:00.0100     QuotaPoolUsage[PagedPool]        13628     QuotaPoolUsage[NonPagedPool]     1616     WorkingSetSizes(now,min,max)     (261,  50,  345)(1044KB,   200KB,  1380KB)     PeakWorkingSetSize               262     VirtualSize                      11Mb     PeakVirtualSize                  11Mb     PageFaultCount                   313     MemoryPriority                   FOREGROUND     BasePriority                     8     CommitCharge                     86 kd>! tokene22bc730 _TOKENe22bc730 TSSession ID: 0 User:S-1-5-21-1787744166-3910675280-2727264193-500 Groups:   00 S-1-5-21-1787744166-3910675280-2727264193-513      Attributes - Mandatory DefaultEnabled   01 S-1-1-0      Attributes - Mandatory DefaultEnabled   02 S-1-5-32-544      Attributes - Mandatory  DefaultEnabledOwner   03 S-1-5-32-545      Attributes - Mandatory DefaultEnabled   04 S-1-5-5-0-92587      Attributes - Mandatory  DefaultEnabledLogonId   05 S-1-2-0      Attributes - Mandatory DefaultEnabled   06 S-1-5-14      Attributes - Mandatory DefaultEnabled   07 S-1-5-4      Attributes - Mandatory DefaultEnabled   08 S-1-5-11      Attributes - Mandatory DefaultEnabled Primary  Group: S-1-5-21-1787744166-3910675280-2727264193-513 Privs:  00 0x000000017  SeChangeNotifyPrivilege          Attributes -  EnabledDefault  01 0x000000008  SeSecurityPrivilege              Attributes -  02 0x000000011  SeBackupPrivilege                Attributes -  03 0x000000012  SeRestorePrivilege               Attributes -  04 0x00000000c  SeSystemtimePrivilege            Attributes -  05 0x000000013  SeShutdownPrivilege              Attributes -  06 0x000000018  SeRemoteShutdownPrivilege        Attributes -  07 0x000000009  SeTakeOwnershipPrivilege         Attributes -  08 0x000000014  SeDebugPrivilege                 Attributes -  09 0x000000016  SeSystemEnvironmentPrivilege     Attributes -  10 0x00000000b  SeSystemProfilePrivilege         Attributes -  11 0x00000000d  SeProfileSingleProcessPrivilege  Attributes -  12 0x00000000e  SeIncreaseBasePriorityPrivilege  Attributes -  13 0x00000000a  SeLoadDriverPrivilege            Attributes -  Enabled  14 0x00000000f  SeCreatePagefilePrivilege        Attributes -  15 0x000000005  SeIncreaseQuotaPrivilege         Attributes -  16 0x000000019  SeUndockPrivilege                Attributes -  Enabled  17 0x00000001c  SeManageVolumePrivilege          Attributes - Authentication ID:        (0,169e4) Impersonation Level:      Anonymous TokenType:                Primary Source: User32            TokenFlags: 0x9 ( Token  in  use ) TokenID: 3a86d0d          ParentTokenID: 0 ModifiedID:               (0,3a86d0f) RestrictedSidCount: 0     RestrictedSids: 00000000

You can indirectly view token contents with Process Explorer's (available from http://www.sysinternals.com) security tab of its process properties dialog box. The dialog box shows the groups and privileges included in the token of the process you examine.


Impersonation

Impersonation is a powerful feature Windows uses frequently in its security model. Windows also uses impersonation in its client/server programming model. For example, a server application can export resources such as files, printers, or databases. Clients wanting to access a resource send a request to the server. When the server receives the request, it must ensure that the client has permission to perform the desired operations on the resource. For example, if a user on a remote machine tries to delete a file on an NTFS share, the server exporting the share must determine whether the user is allowed to delete the file. The obvious process to determine whether a user has permission is for the server to query the user's account and group SIDs and scan the security attributes on the file. This process is tedious to program, prone to errors, and wouldn't permit new security features to be supported transparently. Thus, Windows provides impersonation services to simplify the server's job.

Impersonation lets a server notify the SRM that the server is temporarily adopting the security profile of a client making a resource request. The server can then access resources on behalf of the client, and the SRM carries out the access validations. Usually, a server has access to more resources than a client does and loses some of its security credentials during impersonation. However, the reverse can be true: the server can gain security credentials during impersonation.

A server impersonates a client only within the thread that makes the impersonation request. Thread-control data structures contain an optional entry for an impersonation token. However, a thread's primary token, which represents the thread's real security credentials, is always accessible in the process's control structure.

Windows makes impersonation available through several mechanisms. If a server communicates with a client through a named pipe, the server can use the ImpersonateNamedPipeClient Windows API function to tell the SRM that it wants to impersonate the user on the other end of the pipe. If the server is communicating with the client through Dynamic Data Exchange (DDE) or an RPC, it can make similar impersonation requests using DdeImpersonateClient and RpcImpersonateClient. A thread can create an impersonation token that's simply a copy of its process token with the ImpersonateSelf function. The thread can then alter its impersonation token, to disable SIDs or privileges, for example. Finally, a Security Support Provider Interface (SSPI) package can impersonate its clients with ImpersonateSecurityContext. SSPIs implement a network security model such as LAN Manager version 2 or Kerberos.

After the server thread finishes its task, it reverts to its primary security profile. These forms of impersonation are convenient for carrying out specific actions at the request of a client and for ensuring that object accesses are audited correctly. (For example, the audit that is generated gives the identity of the impersonated client rather than that of the server process.) The disadvantage to these forms of impersonation is that they can't execute an entire program in the context of a client. In addition, an impersonation token can't access files or printers on network shares unless it is a delegation-level impersonation (described shortly) and has sufficient credentials to authenticate to the remote machine, or the file or printer share supports null sessions. (A null session is one that results from an anonymous logon.)

If an entire application must execute in a client's security context or must access network resources, the client must be logged on to the system. The LogonUser Windows API function enables this action. LogonUser takes an account name, a password, a domain or computer name, a logon type (such as interactive, batch, or service), and a logon provider as input, and it returns a primary token. A server thread can adopt the token as an impersonation token, or the server can start a program that has the client's credentials as its primary token. From a security standpoint, a process created using the token returned from an interactive logon via LogonUser, such as with the CreateProcessAsUser API, looks like a program a user starts by logging on to the machine interactively. The disadvantage to this approach is that a server must obtain the user's account name and password. If the server transmits this information across the network, the server must encrypt it securely so that a malicious user snooping network traffic can't capture it.

To prevent the misuse of impersonation, Windows doesn't let servers perform impersonation without a client's consent. A client process can limit the level of impersonation that a server process can perform by specifying a security quality of service (SQOS) when connecting to the server. A process can specify SECURITY_ANONYMOUS, SECURITY_IDENTIFICATION, SECURITY_IMPERSONATION, and SECURITY_DELEGATION as flags for the Windows CreateFile function. Each level lets a server perform different types of operations with respect to the client's security context:

  • SecurityAnonymous is the most restrictive level of impersonation the server can't impersonate or identify the client.

  • SecurityIdentification level lets the server obtain the identity (the SIDs) of the client and the client's privileges, but the server can't impersonate the client.

  • SecurityImpersonation level lets the server identify and impersonate the client on the local system.

  • SecurityDelegation is the most permissive level of impersonation. It lets the server impersonate the client on local and remote systems. Windows NT 4 and earlier don't fully support SecurityDelegation-level impersonation.

If the client doesn't set an impersonation level, Windows chooses the SecurityImpersonation level by default. The CreateFile function also accepts SECURITY_EFFECTIVE_ONLY and SECURITY_CONTEXT_TRACKING as modifiers for the impersonation setting:

  • SECURITY_EFFECTIVE_ONLY prevents a server from enabling or disabling a client's privileges or groups while the server is impersonating.

  • SECURITY_CONTEXT_TRACKING specifies that any changes a client makes to its security context is reflected in a server that is impersonating it. If this option isn't specified, the server adopts the context of the client at the time of the impersonation and doesn't receive any changes. This option is honored only when the client and server process are on the same system.

Restricted Tokens

A restricted token is created from a primary or impersonation token using the CreateRestricted- Token function. The restricted token is a copy of the token it's derived from, with the following possible modifications:

  • Privileges can be removed from the token's privilege array.

  • SIDs in the token can be marked as deny-only.

  • SIDs in the token can be marked as restricted.

The behavior of deny-only and restricted SIDs is covered shortly. Restricted tokens are useful when an application wants to impersonate a client at a reduced security level, primarily for safety reasons when running untrusted code. For example, the restricted token can have the shutdown-system privilege removed from it to prevent code executed in the restricted token's security context from rebooting the system.

EXPERIMENT: Looking at Restricted Tokens

You can make Explorer create a process with a restricted token by following these steps on Windows XP or Windows Server 2003:

  1. Create a shortcut to \Windows\Notepad.exe on your desktop.

  2. Edit the properties of the shortcut, and select the Run With Different Credentials check box. Note that the descriptive text for the check box tells you that you can run the program as yourself while protecting your computer from unauthorized program activity.

  3. Dismiss the shortcut properties, and launch the shortcut.

  4. Accept the default options of running in the current account and protecting your computer from unauthorized program activity that the resulting Run As dialog displays.

  5. Run Process Explorer, and view the Security tab of the properties of the Notepad process you launched. Note that the token contains restricted SIDs and a denyonly SID, and that it has only one privilege. The properties on the left in the following screen shot are from a Notepad running with a nonrestricted token, and the properties on the right are from one launched using these steps:



There are several side-effects of the restricted token you see:

  • All privileges except SeChangeNotifyPrivilege are removed.

  • Any Administrators or Power Users SIDs are marked Deny-Only. This SID removes access to any resources for which administrator access is denied with a matching access-denied ACE, but that would otherwise be overridden by an ACE granting the administrator's group access earlier in the security descriptor.

  • The RESTRICTED SID is added to the Restricted SIDs list, as are all other token SIDs except for the user SID and any Administrators or Power Users SIDs.

  • The SID of the account from which you ran the process is not listed as a restricted SID. This means that the process won't be able to access any objects that permit access to your user account but not to any of the groups that you belong to. For example, your profile directory under \Documents and Settings has a default security descriptor granting your account, the administrator's group, and the System account access. When you try to open that directory from the Notepad you launched, you'll be denied access because the internal second access check, performed with restricted SIDs, will fail because the user SID isn't present in the list of restricted SIDs.


Security Descriptors and Access Control

Tokens, which identify a user's credentials, are only part of the object security equation. Another part of the equation is the security information associated with an object, which specifies who can perform what actions on the object. The data structure for this information is called a security descriptor. A security descriptor consists of the following attributes:

  • Revision number The version of the SRM security model used to create the descriptor.

  • Flags Optional modifiers that define the behavior or characteristics of the descriptor. An example is the SE_DACL_PROTECTED flag, which prevents the descriptor from inheriting a security setting from another object.

  • Owner SID The owner's security ID.

  • Group SID The security ID of the primary group for the object (used only by POSIX).

  • Discretionary access-control list (DACL) Specifies who has what access to the object.

  • System access-control list (SACL) Specifies which operations by which users should be logged in the security audit log.

An access-control list (ACL) is made up of a header and zero or more access-control entry (ACE) structures. There are two types of ACLs: DACLs and SACLs. In a DACL, each ACE contains a SID and an access mask (and a set of flags, explained shortly). Four types of ACEs can appear in a DACL: access allowed, access denied, allowed-object, and denied-object. As you would expect, the access-allowed ACE grants access to a user, and the access-denied ACE denies the access rights specified in the access mask.

The difference between allowed-object and access allowed, and between denied-object and access denied, is that the object types are used only within Active Directory. ACEs of these types have a GUID (globally unique identifier) field that indicates that the ACE applies only to particular objects or subobjects (those that have GUID identifiers). In addition, another optional GUID indicates what type of child object will inherit the ACE when a child is created within an Active Directory container that has the ACE applied to it. (A GUID is a 128-bit identifier guaranteed to be universally unique.)

The accumulation of access rights granted by individual ACEs forms the set of access rights granted by an ACL. If no DACL is present (a null DACL) in a security descriptor, everyone has full access to the object. If the DACL is empty (that is, it has 0 ACEs), no user has access to the object.

The ACEs used in DACLs also have a set of flags that control and specify characteristics of the ACE related to inheritance. Some object namespaces have container objects and leaf objects (or just objects). A container can hold other container objects and leaf objects, which are its child objects. Examples of containers are directories in the file system namespace and keys in the registry namespace. Certain flags in an ACE control how the ACE propagates to child objects of the container associated with the ACE. Table 8-3, reproduced in part from the Platform SDK, lists the inheritance rules for ACE flags.

Table 8-3. Inheritance Rules for ACE Flags

Flag

Inheritance rule

CONTAINER_INHERIT_ACE

Child objects that are containers, such as directories, inherit the ACE as an effective ACE. The inherited ACE is inheritable unless the NO_PROPAGATE_INHERIT_ACE bit flag is also set.

INHERIT_ONLY_ACE

This flag indicates an inherit-only ACE that doesn't control access to the object it's attached to.

INHERITED_ACE

This flag indicates that the ACE was inherited. The system sets this bit when it propagates an inheritable ACE to a child object.

NO_PROPAGATE_INHERIT_ACE

If the ACE is inherited by a child object, the system clears the OBJECT_INHERIT_ACE and CONTAINER_INHERIT_ACE flags in the inherited ACE. This action prevents the ACE from being inherited by subsequent generations of objects.

OBJECT_INHERIT_ACE

Noncontainer child objects inherit the ACE as an effective ACE. For child objects that are containers, the ACE is inherited as an inherit-only ACE unless the NO_PROPAGATE_INHERIT_ACE bit flag is also set.


A SACL contains two types of ACEs, system audit ACEs and system audit-object ACEs. These ACEs specify which operations performed on the object by specific users or groups should be audited. Audit information is stored in the system Audit Log. Both successful and unsuccessful attempts can be audited. Like their DACL object-specific ACE cousins, system audit-object ACEs specify a GUID indicating the types of objects or subobjects that the ACE applies to and an optional GUID that controls propagation of the ACE to particular child object types. If a SACL is null, no auditing takes place on the object. (Security auditing is described later in this chapter.) The inheritance flags that apply to DACL ACEs also apply to system audit and system audit-object ACEs.

Figure 8-4 is a simplified picture of a file object and its DACL.

Figure 8-4. Discretionary access-control list (DACL)


As shown in Figure 8-4, the first ACE allows USER1 to query the file. The second ACE allows members of the group TEAM1 to have read and write access to the file, and the third ACE grants all other users (Everyone) execute access.

EXPERIMENT: Viewing a Security Descriptor

Most executive subsystems rely on the object manager's default security functionality to manage security descriptors for their objects. The object manager's default security functions use the security descriptor pointer to store security descriptors for such objects. For example, the Process Manager uses default security so the object manager stores process and thread security descriptors in the object headers of process and thread objects, respectively. The security descriptor pointer of events, mutexes and semaphores, also store their security descriptors. You can use live kernel debugging to view the security descriptors of these objects once you locate their object header, as outlined in the following steps:

  1. Start the kernel debugger.

  2. Type !process 0 0 to show the address of a Winlogon. (There will be multiple Winlogon's running on a system with more than one active Terminal Server session.) Then type !process again with the address of one of the Winlogon processes:

    kd> !process 88d0c020 PROCESS 88d0c020 SessionId: 0 Cid: 0bdc Peb: 7ffdf000 ParentCid: 00a8     DirBase: 59a58000 ObjectTable: e21df920 HandleCount: 62.     Image: winlogon.exe

  3. Type !object with the address following the word PROCESS of the output of the previous command as the argument to show the object data structure:

    kd> !object 88d0c020 Object: 88d0c020 Type: (8a0ed388) Process     ObjectHeader: 88d0c008     HandleCount: 1 PointerCount: 32

  4. Type dt _OBJECT_HEADER and the address of the object header field from the previous command's output to show the object header data structure, including the security descriptor pointer value:

    lkd> dt _OBJECT_HEADER 88d0c008    +0x000 PointerCount     : 36    +0x004 HandleCount      : 1    +0x004 NextToFree       : 0x00000001    +0x008 Type             : 0x8a0ed388    +0x00c NameInfoOffset   : 0 ''    +0x00d HandleInfoOffset : 0 ''    +0x00e QuotaInfoOffset  : 0 ''    +0x00f Flags            : 0x20 ' '    +0x010 ObjectCreateInfo : 0x89e596a0    +0x010 QuotaBlockCharged : 0x89e596a0    +0x014 SecurityDescriptor : 0xe242b864    +0x018 Body             : _QUAD

  5. Security descriptor pointers in the object header use the low three bits as flags, so type the following command to dump the security descriptor by entering the address displayed in the object header structure, but with the low three bits removed:

    lkd> !sd 0xe242b864 & -8 ->Revision: 0x1 ->Sbz1    : 0x0 ->Control : 0x8004             SE_DACL_PRESENT             SE_SELF_RELATIVE ->Owner   : S-1-5-21-1787744166-3910675280-2727264193-500 ->Group   : S-1-5-21-1787744166-3910675280-2727264193-513 ->Dacl    : ->Dacl    : ->AclRevision: 0x2 ->Dacl    : ->Sbz1       : 0x0 ->Dacl    : ->AclSize    : 0x40 ->Dacl    : ->AceCount   : 0x2 ->Dacl    : ->Sbz2       : 0x0 ->Dacl    : ->Ace[0]: ->AceType: ACCESS_ALLOWED_ACE_TYPE ->Dacl    : ->Ace[0]: ->AceFlags: 0x0 ->Dacl    : ->Ace[0]: ->AceSize: 0x24 ->Dacl    : ->Ace[0]: ->Mask : 0x001f0fff ->Dacl    : ->Ace[0]: ->SID:  S-1-5-21-1787744166-3910675280-2727264193-500 ->Dacl    : ->Ace[1]: ->AceType: ACCESS_ALLOWED_ACE_TYPE ->Dacl    : ->Ace[1]: ->AceFlags: 0x0 ->Dacl    : ->Ace[1]: ->AceSize: 0x14 ->Dacl    : ->Ace[1]: ->Mask : 0x001f0fff ->Dacl    : ->Ace[1]: ->SID: S-1-5-18 ->Sacl    :  is  NULL

The security descriptor contains two access-allowed ACEs with one specifying the administrator's account (recognizable by its RID of 500) and the other specifying the System account (which is always S-1-5-18). Without decoding the bits that are set in the access masks of the ACEs and determining which process access types they map to, it's not possible to easily say what accesses each account has to Winlogon's process object. However, doing so, with the help of SDK header files, reveals that both have full access.


ACL Assignment

To determine which DACL to assign to a new object, the security system uses the first applicable rule of the following four assignment rules:

  1. If a caller explicitly provides a security descriptor when creating the object, the security system applies it to the object. If the object has a name and resides in a container object (for example, a named event object in the \BaseNamedObjects object manager namespace directory), the system merges any inheritable ACEs (ACEs that might propagate from the object's container) into the DACL unless the security descriptor has the SE_DACL_PROTECTED flag set, which prevents inheritance.

  2. If a caller doesn't supply a security descriptor and the object has a name, the security system looks at the security descriptor in the container in which the new object name is stored. Some of the object directory's ACEs might be marked as inheritable, meaning that they should be applied to new objects created in the object directory. If any of these inheritable ACEs are present, the security system forms them into an ACL, which it attaches to the new object. (Separate flags indicate ACEs that should be inherited only by container objects rather than by objects that aren't containers.)

  3. If no security descriptor is specified and the object doesn't inherit any ACEs, the security system retrieves the default DACL from the caller's access token and applies it to the new object. Several subsystems on Windows have hard-coded DACLs that they assign on object creation (for example, services, LSA, and SAM objects).

  4. If there is no specified descriptor, no inherited ACEs, and no default DACL, the system creates the object with no DACL, which allows everyone (all users and groups) full access to the object. This rule is the same as the third rule in which a token contains a null default DACL.

The rules the system uses when assigning a SACL to a new object are similar to those used for DACL assignment, with some exceptions. The first is that inherited system audit ACEs don't propagate to objects with security descriptors marked with the SE_SACL_PROTECTED flag (similar to the SE_DACL_PROTECTED flag, which protects DACLs). Second, if there are no specified security audit ACEs and there is no inherited SACL, no SACL is applied to the object. This behavior is different from that used to apply default DACLs because tokens don't have a default SACL.

When a new security descriptor containing inheritable ACEs is applied to a container, the system automatically propagates the inheritable ACEs to the security descriptors of child objects. (Note that a security descriptor's DACL doesn't accept inherited DACL ACEs if its SE_DACL_PROTECTED flag is enabled, and its SACL doesn't inherit SACL ACEs if the descriptor has the SE_SACL_PROTECTED flag set.) The order with which inheritable ACEs are merged with an existing child object's security descriptor is such that any ACEs that were explicitly applied to the ACL are kept ahead of ACEs that the object inherits. The system uses the following rules for propagating inheritable ACEs:

  • If a child object with no DACL inherits an ACE, the result is a child object with a DACL containing only the inherited ACE.

  • If a child object with an empty DACL inherits an ACE, the result is a child object with a DACL containing only the inherited ACE.

  • For objects in Active Directory only, if an inheritable ACE is removed from a parent object, automatic inheritance removes any copies of the ACE inherited by child objects.

  • For objects in Active Directory only, if automatic inheritance results in the removal of all ACEs from a child object's DACL, the child object has an empty DACL rather than no DACL.

As you'll soon discover, the order of ACEs in an ACL is an important aspect of the Windows security model.

Note

Inheritance is generally not directly supported by the object stores such as file systems, the Registry, or Active Directory. Windows APIs that support inheritance, including Set- SecurityInfo and SetNamedSecurityInfo, do so by invoking appropriate functions within the security inheritance support DLL (\Windows\System32\Ntmarta.Dll) that know how to traverse those object stores.


Determining Access

Two algorithms are used for determining access to an object:

  • One to determine the maximum access allowed to the object, a form of which is exported to user mode with the Windows GetEffectiveRightsFromAcl function

  • One to determine whether a specific desired access is allowed, which can be done with the Windows AccessCheck function or the AccessCheckByType function

The first algorithm examines the entries in the DACL as follows:

  1. If the object has no DACL (a null DACL), the object has no protection and the security system grants all access.

  2. If the caller has the take-ownership privilege, the security system grants write-owner access before examining the DACL. (Take-ownership privilege and write-owner access are explained in a moment.)

  3. If the caller is the owner of the object, the read-control and write-DACL access rights are granted.

  4. For each access-denied ACE that contains a SID that matches one in the caller's access token, the ACE's access mask is removed from the granted-access mask.

  5. For each access-allowed ACE that contains a SID that matches one in the caller's access token, the ACE's access mask is added to the granted-access mask being computed, unless that access has already been denied.

When all the entries in the DACL have been examined, the computed granted-access mask is returned to the caller as the maximum allowed access to the object. This mask represents the total set of access types that the caller will be able to successfully request when opening the object.

The preceding description applies only to the kernel-mode form of the algorithm. The Windows version implemented by GetEffectiveRightsFromAcl differs in that it doesn't perform step 2, and it considers a single user or group SID rather than an access token.

The second algorithm is used to determine whether a specific access request can be granted, based on the caller's access token. Each open function in the Windows API that deals with securable objects has a parameter that specifies the desired access mask, which is the last component of the security equation. To determine whether the caller has access, the following steps are performed:

  1. If the object has no DACL (a null DACL), the object has no protection and the security system grants the desired access.

  2. If the caller has the take-ownership privilege, the security system grants write-owner access and then examines the DACL. However, if write-owner access was the only access requested by a caller with take-ownership privilege, the security system grants that access and never examines the DACL.

  3. If the caller is the owner of the object, the read-control and write-DACL access rights are granted. If these rights were the only access rights that caller requested, access is granted without examining the DACL.

  4. Each ACE in the DACL is examined from first to last. An ACE is processed if one of the following conditions is satisfied:

    1. The ACE is an access-deny ACE, and the SID in the ACE matches an enabled SID (SIDs can be enabled or disabled) or a deny-only SID in the caller's access token.

    2. The ACE is an access-allowed ACE, and the SID in the ACE matches an enabled SID in the caller's token that isn't of type deny-only.

    3. It is the second pass through the descriptor for restricted-SID checks, and the SID in the ACE matches a restricted SID in the caller's access token.

  5. If it is an access-allowed ACE, the rights in the access mask in the ACE that were requested are granted; if all the requested access rights have been granted, the access check succeeds. If it is an access-denied ACE and any of the requested access rights are in the denied-access rights, access is denied to the object.

  6. If the end of the DACL is reached and some of the requested access rights still haven't been granted, access is denied.

  7. If all accesses are granted but the caller's access token has at least one restricted SID, the system rescans the DACL's ACEs looking for ACEs with access-mask matches for the accesses the user is requesting and a match of the ACE's SID with any of the caller's restricted SIDs. Only if both scans of the DACL grant the requested access rights is the user granted access to the object.

The behavior of both access-validation algorithms depends on the relative ordering of allow and deny ACEs. Consider an object with only two ACEs where one ACE specifies that a certain user is allowed full access to an object and the other ACE denies the user access. If the allow ACE precedes the deny ACE, the user can obtain full access to the object, but if the order is reversed, the user can not gain any access to the object.

Older Windows functions, such as AddAccessAllowedAce, add ACEs to the end of a DACL, which usually isn't the desired behavior, so most Windows applications prior to Windows 2000 have no choice but to construct DACLs manually, with deny ACEs placed at the front of the list. Several Windows functions, such as SetSecurityInfo and SetNamedSecurityInfo, apply ACEs in the preferred order of deny ACEs preceding allow ACEs. Note that the security editor dialog boxes with which you edit permissions on NTFS files and registry keys, for example, use these functions. SetSecurityInfo and SetNamedSecurityInfo also apply ACE inheritance rules to the security descriptor on which they are applied.

Figure 8-5 shows an example access validation demonstrating the importance of ACE ordering. In the example, access is denied a user wanting to open a file even though an ACE in the object's DACL grants the access because the ACE denying the user access (by virtue of the user's membership in the Writers group) precedes the ACE granting access.

Figure 8-5. Access validation example


As we stated earlier, because it wouldn't be efficient for the security system to process the DACL every time a process uses a handle, the SRM makes this access check only when a handle is opened, not each time the handle is used. Thus, once a process successfully opens a handle, the security system can't revoke the access rights that have been granted, even if the object's DACL changes. Also keep in mind that because kernel-mode code uses pointers rather than handles to access objects, the access check isn't performed when the operating system uses objects. In other words, the Windows executive "trusts" itself in a security sense.

The fact that an object's owner is always granted write-DACL access to an object means that users can never be prevented from accessing the objects they own. If, for some reason, an object had an empty DACL (no access), the owner would still be able to open the object with write-DACL access and then apply a new DACL with the desired access permissions.

A Warning Regarding the GUI Security Editors

When you use the GUI permissions editors to modify security settings on a file, Registry, Active Directory object, or other securable object, the main security dialog box shows you a potentially misleading view of the security that's applied to the object. The top portion of the dialog box shows in alphabetical ordering the groups and users that have ACEs in the object's access control list. If you allow Full Control to the Administrators group and deny the Everyone group Full Control, the list might lead you to believe that the Administrators group access-allowed ACE precedes the Everyone deny ACE because that's the order in which they appear. However, as we've said, the editors place deny ACEs before allow ACEs when they apply the ACL to the object.



The Permissions tab of the Advanced Security Settings dialog box shows the order of ACEs in the DACL. However, even this dialog box can be confusing because a complex DACL can have deny ACEs for various accesses followed by allow ACEs for other access types.



The only definitive way to know what accesses a particular user or group will have to an object (other than having that user or a member of the group try to access the object) is to use the Effective Permissions tab of the dialog box that displays when you press the Advanced button on the dialog box. Enter the name of the user or group you want to check and the dialog box shows you what permissions they are allowed for the object.




The AuthZ API

The AuthZ Windows API, which was introduced in Windows XP, implements the same security model as the Security Reference Monitor, but implements it totally in user mode in the \Windows\System32\Authz.Dll library. This gives applications that want to protect their own private objects, such as database tables, the ability to leverage the Windows security model without incurring the cost of user-mode to kernel-mode transitions that they would make if they relied on the Security Reference Monitor.

The AuthZ API uses standard security descriptor data structures, SIDs, and privileges. Instead of using tokens to represent clients, AuthZ uses the AUTHZ_CLIENT_CONTEXT. AuthZ includes user-mode equivalents of all access- check and Windows security functions for example, AuthzAccessCheck is the AuthZ version of the AccessCheck Windows API that uses the SeAccessCheck Security Reference Monitor function.

Another advantage available to applications that use AuthZ is that they can direct AuthZ to cache the results of security checks to improve subsequent checks that use the same client context and security descriptor.

AuthZ is fully documented in the Platform SDK.


     < Day Day Up > 


    Microsoft Windows Internals
    Microsoft Windows Internals (4th Edition): Microsoft Windows Server 2003, Windows XP, and Windows 2000
    ISBN: 0735619174
    EAN: 2147483647
    Year: 2004
    Pages: 158

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net