Transact SQL - Security
Transact SQL - Security
PREPARED BY:
DR. YOUSSEF ROUMIEH
E-MAIL : [email protected]
Reference:
2
Introduction
The privileges are, for one or more users the possibility of using certain objects and
among these objects, certain SQL orders.
Privilege assignment
This is the GRANT SQL statement that allows to assign a privilege to different users on
different objects.
All users must communicate their username in order to access the database.
The user who creates a table is considered the owner of this table.
He/she has all the rights on this table and its contents. On the other hand the other
users have no rights on this table (neither reading nor modification) unless the owner
gives them explicitly these rights with a GRANT order.
Example 1:
Fouad allows to run SQL SELECT statements on Students table.
GRANT SELECT ON Students TO Fouad
Example 2:
Authorizes Mike and Fouad to modify the data by all update SQL commands (INSERT,
UPDATE, DELETE) but not to read them!
GRANT INSERT, UPDATE, DELETE ON Students
TO Mike, Fouad
Example 3:
Authorizes Maria to launch SQL SELECT statements on the table Students but also to
transmit to any other user the rights he has acquired in this order.
GRANT SELECT ON Students TO Maria
WITH GRANT OPTION
Example 4:
Allow Remie to run SQL SELECT, INSERT, DELETE statements on the Students table but also
to transmit to any other user the rights he has acquired in this order.
GRANT SELECT, INSERT, DELETE ON Students TO Remie
WITH GRANT OPTION
Example 5:
Allows present and future users to run SQL SELECT and UPDATE statements on the
Students table.
GRANT SELECT, UPDATE ON Students TO PUBLIC
Example 6 :
Remie launches the following order:
GRANT ALL PRIVILEGES ON Students TO Fouad
Which authorizes Fouad to launch on the table Students, the same orders SQL, than
those authorized with Remie (SELECT, INSERT, DELETE).
This is called rights inheritance, meaning that the user with these rights can again assign
them to one or more other users.
Example 7 :
Remie launches the following order:
GRANT UPDATE ON Students TO Fouad
This command will cause an error because Remie is not allowed to launch UPDATE
statements on the Students table and cannot transmit a right that it does not have!
We can define the usable columns for an insertion order for a user:
Example 9
GRANT INSERT (First_Name, Last_Name, Address, Phone)ON Students TO Sami
This order allows Sami to insert a new row into the table, only by specifying the
listed columns. The problem is that in this list does not appear the key column ... In
other words, Sami can never insert anything at all, unless the key is calculated by a
trigger before insertion.
Malak will be able to select and delete without problems in all tables (Students,
Enroll).
She will be able to update the data and to insert without any problem in the
Student table.
However, she will sometimes encounter a database refusal to update the Enroll
table. Worse, she will be impossible for her to insert data in this last table...
What is the reason?
A user who has granted a privilege can resume it with the REVOKE
command
SQL syntax:
Example 11 :
Removes the selection privilege from the Students table assigned to Fouad
REVOKE SELECT ON Students FROM Fouad
Example 12 :
Removes the insert and delete privileges from the Students table assigned to mike and
fouad in example 2, but not the update one (UPDATE).
REVOKE INSERT, DELETE ON Students FROM mike, fouad
Example 13 :
Removes the possibility for maria to transmit the selection privilege on the Students
table.
REVOKE GRANT OPTION FOR SELECT ON Students FROM maria
There are, however, some pitfalls in the use of the revocation mechanism.
We will show some of them using different examples.
Suppose whoever launches the orders is the user sami.
Unlike “systems” rights, privileges are cumulative. The same privilege can
thus be obtained on the same object several times from different users.
The privilege will be completely removed when all users who have given
this privilege have removed it.
Example 14:
GRANT SELECT ON T_CLIENT TO mike
WITH GRANT OPTION
GRANT SELECT ON T_CLIENT TO maria
It is now mike who is the user who will launch the following order:
GRANT SELECT ON T_CLIENT TO maria
Here is another problem. The super user PUBLIC does not target anyone in
particular nor in general. Therefore, a particular privilege cannot be
removed from a given user even if privileges have been assigned to
“PUBLIC”.
Patient
Login LastName FirstName Age Gender
Pfranck Franck Patricia 60 F
Mrobert Robert Martin 35 M
Diagnostic
Login Disease
Pfranck Ulcere
Mrobert Pneumonie
Mrobert Asthme
Physicians have permission to view and edit both tables without restriction.
The secretaries are allowed to see the patient table.
Nurses are allowed to see both tables.
Patients are allowed to see the information that concerns them.
Problem for the last rule because the granularity level of the SQL security
model is the table and not the tuple
Creating 2 views
GRANT Physician TO …
GRANT Nurse TO …
GRANT Secretary TO …
GRANT Disease TO pfranck;
GRANT Disease TO mrobert;
alter role patient add member pfranck
6. Suppose Bob uses SQL Server authentication to access the Sales schema.
Write the SQL statements that:
a. Create a login for Bob.
b. Create a Bob user for the login created with the Sales default schema.
c. Create the R_Emp role.
d. Assign the R_Emp role to the Bob user.
7. An employee is allowed to see only his information. Write the SQL
statements to assign this right to the created Role R_Emp.