This article discusses the security model of Microsoft
SQL Server 7.0/2000 and security best practices to help you secure your data.
Special thanks to my friend Divya Kalra for her valuable input and content review.
Security is a major concern for the modern age systems/network/database
administrators. It is natural for an administrator to worry about hackers and
external attacks while implementing security. But there is more to it. It is
essential to first implement security within the organization, to make sure
right people have access to the right data. Without these security measures in
place, you might find someone destroying your valuable data, or selling your
company's secrets to your competitors or someone invading the privacy of
others. Primarily a security plan must identify which users in the organization
can see which data and perform which activities in the database.
SQL Server security model
To be able to access data from a database, a user must pass through two stages
of authentication, one at the SQL Server level and the other at the database
level. These two stages are implemented using Logins names and User accounts
respectively. A valid login is required to connect to SQL Server and a valid user
account is required to access a database.
Login: A valid login name is required to connect to an SQL
Server instance. A login could be:
These login names are maintained within the master
database. So, it is essential to backup the master database after adding new
logins to SQL Server.
User: A valid user account within a database is required
to access that database. User accounts are specific to a database. All
permissions and ownership of objects in the database are controlled by the user
account. SQL Server logins are associated with these user accounts. A login can
have associated users in different databases, but only one user per database.
During a new connection request, SQL Server verifies the
login name supplied, to make sure, that login is authorized to access SQL
Server. This verification is called Authentication. SQL Server supports two
authentication modes:
Point to note is that, whatever mode you configure your
SQL Server to use, you can always login using Windows authentication.
Windows authentication is the recommended security mode, as it is more secure
and you don't have to send login names and passwords over the network. You
should avoid mixed mode, unless you have a non-Windows NT/2000 environment or
when your SQL Server is installed on Windows 95/98 or for backward
compatibility with your existing applications.
SQL Server's authentication mode can be changed using Enterprise Manager (Right
click on the server name and click on Properties. Go to the Security tab).
Authentication mode can also be changed using SQL DMO object model.
Here is a list of helpful stored procedures for managing logins and users:
|
sp_addlogin |
Creates a new login that allows users to connect to
SQL Server using SQL Server authentication |
|
sp_grantlogin |
Allows a Windows NT/2000 user account or group to
connect to SQL Server using Windows authentication |
|
sp_droplogin |
Drops an SQL Server login |
|
sp_revokelogin |
Drops a Windows NT/2000 login/group from SQL Server |
|
sp_denylogin |
Prevents a Windows NT/2000 login/group from
connecting to SQL Server |
|
sp_password |
Adds or changes the password for an SQL Server login |
|
sp_helplogins |
Provides information about logins and their
associated users in each database |
|
sp_defaultdb |
Changes the default database for a login |
|
sp_grantdbaccess |
Adds an associated user account in the current
database for an SQL Server login or Windows NT/2000 login |
|
sp_revokedbaccess |
Drops a user account from the current database |
|
sp_helpuser |
Reports information about the Microsoft users and
roles in the current database |
Now let's talk about controlling access to objects within
the database and managing permissions. Apart from managing permissions at the
individual database user level, SQL Server 7.0/2000 implements permissions
using roles. A role is nothing but a group to which individual logins/users can
be added, so that the permissions can be applied to the group, instead of
applying the permissions to all the individual logins/users. There are three
types of roles in SQL Server 7.0/2000:
Fixed server roles: These are server-wide roles. Logins can be added to
these roles to gain the associated administrative permissions of the role.
Fixed server roles cannot be altered and new server roles cannot be created.
Here are the fixed server roles and their associated permissions in SQL Server
2000:
|
Fixed server role |
Description |
|
sysadmin |
Can perform any activity in SQL Server |
|
serveradmin |
Can set server-wide configuration options, shut down
the server |
|
setupadmin |
Can manage linked servers and startup procedures |
|
securityadmin |
Can manage logins and CREATE DATABASE permissions,
also read error logs and change passwords |
|
processadmin |
Can manage processes running in SQL Server |
|
dbcreator |
Can create, alter, and drop databases |
|
diskadmin |
Can manage disk files |
|
bulkadmin |
Can execute BULK INSERT statements |
Here is a list of stored procedures that are helpful in
managing fixed server roles:
|
sp_addsrvrolemember |
Adds a login as a member of a fixed server role |
|
sp_dropsrvrolemember |
Removes an SQL Server login, Windows user or group
from a fixed server role |
|
sp_helpsrvrole |
Returns a list of the fixed server roles |
|
sp_helpsrvrolemember |
Returns information about the members of fixed
server roles |
|
sp_srvrolepermission |
Returns the permissions applied to a fixed server
role |
Fixed database roles: Each database has a set of fixed database roles, to
which database users can be added. These fixed database roles are unique within
the database. While the permissions of fixed database roles cannot be altered,
new database roles can be created. Here are the fixed database roles and their
associated permissions in SQL Server 2000:
|
Fixed database role |
Description |
|
db_owner |
Has all permissions in the database |
|
db_accessadmin |
Can add or remove user IDs |
|
db_securityadmin |
Can manage all permissions, object ownerships, roles
and role memberships |
|
db_ddladmin |
Can issue ALL DDL, but cannot issue GRANT, REVOKE,
or DENY statements |
|
db_backupoperator |
Can issue DBCC, CHECKPOINT, and BACKUP statements |
|
db_datareader |
Can select all data from any user table in the
database |
|
db_datawriter |
Can modify any data in any user table in the
database |
|
db_denydatareader |
Cannot select any data from any user table in the
database |
|
db_denydatawriter |
Cannot modify any data in any user table in the
database |
Here is a list of stored procedures that are helpful in
managing fixed database roles:
|
sp_addrole |
Creates a new database role in the current database |
|
sp_addrolemember |
Adds a user to an existing database role in the
current database |
|
sp_dbfixedrolepermission |
Displays permissions for each fixed database role |
|
sp_droprole |
Removes a database role from the current database |
|
sp_helpdbfixedrole |
Returns a list of fixed database roles |
|
sp_helprole |
Returns information about the roles in the current
database |
|
sp_helprolemember |
Returns information about the members of a role in
the current database |
|
sp_droprolemember |
Removes users from the specified role in the current
database |
Application roles: Application roles are another way of implementing
permissions. These are quite different from the server and database roles.
After creating and assigning the required permissions to an application role,
the client application needs to activate this role at run-time to get the
permissions associated with that application role. Application roles simplify
the job of DBAs, as they don't have to worry about managing permissions at
individual user level. All they need to do is to create an application role and
assign permissions to it. The application that is connecting to the database
activates the application role and inherits the permissions associated with
that role. Here are the characteristics of application roles:
Here are the stored procedures that are required to
manage application roles:
|
sp_addapprole |
Adds an application role in the current database |
|
sp_approlepassword |
Changes the password of an application role in the
current database |
|
sp_dropapprole |
Drops an application role from the current database |
|
sp_setapprole |
Activates the permissions associated with an
application role in the current database |
Now that we discussed different kinds of roles, let's
talk about granting/revoking permissions to/from database users and database
roles and application roles. The following T-SQL commands are used to manage
permissions at the user and role level.
Using the above commands, permissions can be
granted/denied/revoked to users/roles on all database objects. You can manage
permissions at as low as the column level.
Note: There is no way to manage permissions at the row
level. That is, in a given table, you can't grant SELECT permission on a
specific row to User1 and deny SELECT permission on another row to User2. This
kind of security can be implemented by using views and stored procedures
effectively. Click here to read about row level security
implementation in SQL Server databases. Just an FYI, Oracle has a
feature called "Virtual Private Databases" (VPD) that allows DBAs to
configure permissions at row level.
SQL Server security best
practices
Here is an ideal implementation
of security in a Windows NT/2000 environment with SQL Server 7.0/2000 database
server:
Here is a security checklist and some standard security
practices and tips:
