Skip to content

Commit 204a920

Browse files
committed
Merge tag 'Smack-for-6.19' of https://github.com/cschaufler/smack-next
Pull smack updates from Casey Schaufler: - fix several cases where labels were treated inconsistently when imported from user space - clean up the assignment of extended attributes - documentation improvements * tag 'Smack-for-6.19' of https://github.com/cschaufler/smack-next: Smack: function parameter 'gfp' not described smack: fix kernel-doc warnings for smk_import_valid_label() smack: fix bug: setting task label silently ignores input garbage smack: fix bug: unprivileged task can create labels smack: fix bug: invalid label of unix socket file smack: always "instantiate" inode in smack_inode_init_security() smack: deduplicate xattr setting in smack_inode_init_security() smack: fix bug: SMACK64TRANSMUTE set on non-directory smack: deduplicate "does access rule request transmutation"
2 parents 0eae328 + 29c701f commit 204a920

4 files changed

Lines changed: 275 additions & 119 deletions

File tree

Documentation/admin-guide/LSM/Smack.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,15 @@ specification.
601601
Task Attribute
602602
~~~~~~~~~~~~~~
603603

604-
The Smack label of a process can be read from /proc/<pid>/attr/current. A
605-
process can read its own Smack label from /proc/self/attr/current. A
604+
The Smack label of a process can be read from ``/proc/<pid>/attr/current``. A
605+
process can read its own Smack label from ``/proc/self/attr/current``. A
606606
privileged process can change its own Smack label by writing to
607-
/proc/self/attr/current but not the label of another process.
607+
``/proc/self/attr/current`` but not the label of another process.
608+
609+
Format of writing is : only the label or the label followed by one of the
610+
3 trailers: ``\n`` (by common agreement for ``/proc/...`` interfaces),
611+
``\0`` (because some applications incorrectly include it),
612+
``\n\0`` (because we think some applications may incorrectly include it).
608613

609614
File Attribute
610615
~~~~~~~~~~~~~~
@@ -696,6 +701,11 @@ sockets.
696701
A privileged program may set this to match the label of another
697702
task with which it hopes to communicate.
698703

704+
UNIX domain socket (UDS) with a BSD address functions both as a file in a
705+
filesystem and as a socket. As a file, it carries the SMACK64 attribute. This
706+
attribute is not involved in Smack security enforcement and is immutably
707+
assigned the label "*".
708+
699709
Smack Netlabel Exceptions
700710
~~~~~~~~~~~~~~~~~~~~~~~~~
701711

security/smack/smack.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,12 @@ int smk_tskacc(struct task_smack *, struct smack_known *,
300300
int smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
301301
int smack_str_from_perm(char *string, int access);
302302
struct smack_known *smack_from_secid(const u32);
303+
int smk_parse_label_len(const char *string, int len);
303304
char *smk_parse_smack(const char *string, int len);
304305
int smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
305306
struct smack_known *smk_import_entry(const char *, int);
307+
struct smack_known *smk_import_valid_label(const char *label, int label_len,
308+
gfp_t gfp);
306309
void smk_insert_entry(struct smack_known *skp);
307310
struct smack_known *smk_find_entry(const char *);
308311
bool smack_privileged(int cap);

security/smack/smack_access.c

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -443,34 +443,53 @@ struct smack_known *smk_find_entry(const char *string)
443443
}
444444

445445
/**
446-
* smk_parse_smack - parse smack label from a text string
447-
* @string: a text string that might contain a Smack label
448-
* @len: the maximum size, or zero if it is NULL terminated.
446+
* smk_parse_label_len - calculate the length of the starting segment
447+
* in the string that constitutes a valid smack label
448+
* @string: a text string that might contain a Smack label at the beginning
449+
* @len: the maximum size to look into, may be zero if string is null-terminated
449450
*
450-
* Returns a pointer to the clean label or an error code.
451+
* Returns the length of the segment (0 < L < SMK_LONGLABEL) or an error code.
451452
*/
452-
char *smk_parse_smack(const char *string, int len)
453+
int smk_parse_label_len(const char *string, int len)
453454
{
454-
char *smack;
455455
int i;
456456

457-
if (len <= 0)
458-
len = strlen(string) + 1;
457+
if (len <= 0 || len > SMK_LONGLABEL)
458+
len = SMK_LONGLABEL;
459459

460460
/*
461461
* Reserve a leading '-' as an indicator that
462462
* this isn't a label, but an option to interfaces
463463
* including /smack/cipso and /smack/cipso2
464464
*/
465465
if (string[0] == '-')
466-
return ERR_PTR(-EINVAL);
466+
return -EINVAL;
467467

468468
for (i = 0; i < len; i++)
469469
if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
470470
string[i] == '"' || string[i] == '\\' || string[i] == '\'')
471471
break;
472472

473473
if (i == 0 || i >= SMK_LONGLABEL)
474+
return -EINVAL;
475+
476+
return i;
477+
}
478+
479+
/**
480+
* smk_parse_smack - copy the starting segment in the string
481+
* that constitutes a valid smack label
482+
* @string: a text string that might contain a Smack label at the beginning
483+
* @len: the maximum size to look into, may be zero if string is null-terminated
484+
*
485+
* Returns a pointer to the copy of the label or an error code.
486+
*/
487+
char *smk_parse_smack(const char *string, int len)
488+
{
489+
char *smack;
490+
int i = smk_parse_label_len(string, len);
491+
492+
if (i < 0)
474493
return ERR_PTR(-EINVAL);
475494

476495
smack = kstrndup(string, i, GFP_NOFS);
@@ -554,31 +573,26 @@ int smack_populate_secattr(struct smack_known *skp)
554573
}
555574

556575
/**
557-
* smk_import_entry - import a label, return the list entry
558-
* @string: a text string that might be a Smack label
559-
* @len: the maximum size, or zero if it is NULL terminated.
576+
* smk_import_valid_allocated_label - import a label, return the list entry
577+
* @smack: a text string that is a valid Smack label and may be kfree()ed.
578+
* It is consumed: either becomes a part of the entry or kfree'ed.
579+
* @gfp: Allocation type
560580
*
561-
* Returns a pointer to the entry in the label list that
562-
* matches the passed string, adding it if necessary,
563-
* or an error code.
581+
* Returns: see description of smk_import_entry()
564582
*/
565-
struct smack_known *smk_import_entry(const char *string, int len)
583+
static struct smack_known *
584+
smk_import_allocated_label(char *smack, gfp_t gfp)
566585
{
567586
struct smack_known *skp;
568-
char *smack;
569587
int rc;
570588

571-
smack = smk_parse_smack(string, len);
572-
if (IS_ERR(smack))
573-
return ERR_CAST(smack);
574-
575589
mutex_lock(&smack_known_lock);
576590

577591
skp = smk_find_entry(smack);
578592
if (skp != NULL)
579593
goto freeout;
580594

581-
skp = kzalloc(sizeof(*skp), GFP_NOFS);
595+
skp = kzalloc(sizeof(*skp), gfp);
582596
if (skp == NULL) {
583597
skp = ERR_PTR(-ENOMEM);
584598
goto freeout;
@@ -608,6 +622,44 @@ struct smack_known *smk_import_entry(const char *string, int len)
608622
return skp;
609623
}
610624

625+
/**
626+
* smk_import_entry - import a label, return the list entry
627+
* @string: a text string that might contain a Smack label at the beginning
628+
* @len: the maximum size to look into, may be zero if string is null-terminated
629+
*
630+
* Returns a pointer to the entry in the label list that
631+
* matches the passed string, adding it if necessary,
632+
* or an error code.
633+
*/
634+
struct smack_known *smk_import_entry(const char *string, int len)
635+
{
636+
char *smack = smk_parse_smack(string, len);
637+
638+
if (IS_ERR(smack))
639+
return ERR_CAST(smack);
640+
641+
return smk_import_allocated_label(smack, GFP_NOFS);
642+
}
643+
644+
/**
645+
* smk_import_valid_label - import a label, return the list entry
646+
* @label: a text string that is a valid Smack label, not null-terminated
647+
* @label_len: the length of the text string in the @label
648+
* @gfp: the GFP mask used for allocating memory for the @label text string copy
649+
*
650+
* Return: see description of smk_import_entry()
651+
*/
652+
struct smack_known *
653+
smk_import_valid_label(const char *label, int label_len, gfp_t gfp)
654+
{
655+
char *smack = kstrndup(label, label_len, gfp);
656+
657+
if (!smack)
658+
return ERR_PTR(-ENOMEM);
659+
660+
return smk_import_allocated_label(smack, gfp);
661+
}
662+
611663
/**
612664
* smack_from_secid - find the Smack label associated with a secid
613665
* @secid: an integer that might be associated with a Smack label

0 commit comments

Comments
 (0)