Deleted Added
full compact
34c34
< * $FreeBSD: head/sys/security/mac_lomac/mac_lomac.c 115715 2003-06-02 18:49:11Z rwatson $
---
> * $FreeBSD: head/sys/security/mac_lomac/mac_lomac.c 116701 2003-06-23 01:26:34Z rwatson $
51a52
> #include <sys/sbuf.h>
486,487c487,488
< static int mac_lomac_to_string(char *string, size_t size,
< size_t *caller_len, struct mac_lomac *mac_lomac);
---
> static int mac_lomac_to_string(struct sbuf *sb,
> struct mac_lomac *mac_lomac);
492a494,497
> struct sbuf subjlabel_sb, subjtext_sb, objlabel_sb;
> char *subjlabeltext, *objlabeltext, *subjtext;
> struct mac_lomac cached_subjlabel;
> struct mac_lomac_proc *subj;
494,496d498
< static char xxx[] = "<<XXX>>";
< struct mac_lomac_proc *subj = PSLOT(&curthread->td_proc->p_label);
< char *subjlabeltext, *objlabeltext, *subjtext, *text;
498d499
< size_t len;
500a502,503
> subj = PSLOT(&curthread->td_proc->p_label);
>
534,542c537,542
< subjtext = subjlabeltext = objlabeltext = xxx;
< if (mac_lomac_to_string(NULL, 0, &len, &subj->mac_lomac) == 0 &&
< (text = malloc(len + 1, M_MACLOMAC, M_NOWAIT)) != NULL) {
< if (mac_lomac_to_string(text, len + 1, &len,
< &subj->mac_lomac) == 0)
< subjtext = text;
< else
< free(text, M_MACLOMAC);
< }
---
>
> /*
> * Avoid memory allocation while holding a mutex; cache the
> * label.
> */
> mac_lomac_copy_single(&subj->mac_lomac, &cached_subjlabel);
544,559c544,559
< if (mac_lomac_to_string(NULL, 0, &len, subjlabel) == 0 &&
< (text = malloc(len + 1, M_MACLOMAC, M_NOWAIT)) != NULL) {
< if (mac_lomac_to_string(text, len + 1, &len,
< subjlabel) == 0)
< subjlabeltext = text;
< else
< free(text, M_MACLOMAC);
< }
< if (mac_lomac_to_string(NULL, 0, &len, objlabel) == 0 &&
< (text = malloc(len + 1, M_MACLOMAC, M_NOWAIT)) != NULL) {
< if (mac_lomac_to_string(text, len + 1, &len,
< objlabel) == 0)
< objlabeltext = text;
< else
< free(text, M_MACLOMAC);
< }
---
>
> sbuf_new(&subjlabel_sb, NULL, 0, SBUF_AUTOEXTEND);
> mac_lomac_to_string(&subjlabel_sb, subjlabel);
> sbuf_finish(&subjlabel_sb);
> subjlabeltext = sbuf_data(&subjlabel_sb);
>
> sbuf_new(&subjtext_sb, NULL, 0, SBUF_AUTOEXTEND);
> mac_lomac_to_string(&subjtext_sb, &subj->mac_lomac);
> sbuf_finish(&subjtext_sb);
> subjtext = sbuf_data(&subjtext_sb);
>
> sbuf_new(&objlabel_sb, NULL, 0, SBUF_AUTOEXTEND);
> mac_lomac_to_string(&objlabel_sb, objlabel);
> sbuf_finish(&objlabel_sb);
> objlabeltext = sbuf_data(&objlabel_sb);
>
574a575,578
>
> sbuf_delete(&subjlabel_sb);
> sbuf_delete(&subjtext_sb);
> sbuf_delete(&objlabel_sb);
576,581d579
< if (subjlabeltext != xxx)
< free(subjlabeltext, M_MACLOMAC);
< if (objlabeltext != xxx)
< free(objlabeltext, M_MACLOMAC);
< if (subjtext != xxx)
< free(subjtext, M_MACLOMAC);
662,669c660,661
< /*
< * mac_lomac_element_to_string() is basically an snprintf wrapper with
< * the same properties as snprintf(). It returns the length it would
< * have added to the string in the event the string is too short.
< */
< static size_t
< mac_lomac_element_to_string(char *string, size_t size,
< struct mac_lomac_element *element)
---
> static int
> mac_lomac_element_to_string(struct sbuf *sb, struct mac_lomac_element *element)
674c666
< return (snprintf(string, size, "high"));
---
> return (sbuf_printf(sb, "high"));
677c669
< return (snprintf(string, size, "low"));
---
> return (sbuf_printf(sb, "low"));
680c672
< return (snprintf(string, size, "equal"));
---
> return (sbuf_printf(sb, "equal"));
683c675
< return (snprintf(string, size, "%d", element->mle_grade));
---
> return (sbuf_printf(sb, "%d", element->mle_grade));
692,693c684
< mac_lomac_to_string(char *string, size_t size, size_t *caller_len,
< struct mac_lomac *mac_lomac)
---
> mac_lomac_to_string(struct sbuf *sb, struct mac_lomac *mac_lomac)
695,696d685
< size_t left, len, curlen;
< char *curptr;
698,718d686
< /*
< * Also accept NULL string to allow for predetermination of total
< * string length.
< */
< if (string != NULL)
< bzero(string, size);
< else if (size != 0)
< return (EINVAL);
< curptr = string;
< left = size;
< curlen = 0;
<
< #define INCLEN(length, leftover) do { \
< if (string != NULL) { \
< if (length >= leftover) \
< return (EINVAL); \
< leftover -= length; \
< curptr += length; \
< } \
< curlen += length; \
< } while (0)
720,722c688,690
< len = mac_lomac_element_to_string(curptr, left,
< &mac_lomac->ml_single);
< INCLEN(len, left);
---
> if (mac_lomac_element_to_string(sb, &mac_lomac->ml_single)
> == -1)
> return (EINVAL);
726,727c694,695
< len = snprintf(curptr, left, "[");
< INCLEN(len, left);
---
> if (sbuf_putc(sb, '[') == -1)
> return (EINVAL);
729,731c697,699
< len = mac_lomac_element_to_string(curptr, left,
< &mac_lomac->ml_auxsingle);
< INCLEN(len, left);
---
> if (mac_lomac_element_to_string(sb, &mac_lomac->ml_auxsingle)
> == -1)
> return (EINVAL);
733,734c701,702
< len = snprintf(curptr, left, "]");
< INCLEN(len, left);
---
> if (sbuf_putc(sb, ']') == -1)
> return (EINVAL);
738,739c706,707
< len = snprintf(curptr, left, "(");
< INCLEN(len, left);
---
> if (sbuf_putc(sb, '(') == -1)
> return (EINVAL);
741,743c709,711
< len = mac_lomac_element_to_string(curptr, left,
< &mac_lomac->ml_rangelow);
< INCLEN(len, left);
---
> if (mac_lomac_element_to_string(sb, &mac_lomac->ml_rangelow)
> == -1)
> return (EINVAL);
745,746c713,714
< len = snprintf(curptr, left, "-");
< INCLEN(len, left);
---
> if (sbuf_putc(sb, '-') == -1)
> return (EINVAL);
748,750c716,718
< len = mac_lomac_element_to_string(curptr, left,
< &mac_lomac->ml_rangehigh);
< INCLEN(len, left);
---
> if (mac_lomac_element_to_string(sb, &mac_lomac->ml_rangehigh)
> == -1)
> return (EINVAL);
752,753c720,721
< len = snprintf(curptr, left, ")");
< INCLEN(len, left);
---
> if (sbuf_putc(sb, '-') == -1)
> return (EINVAL);
755d722
< #undef INCLEN
757d723
< *caller_len = curlen;
763c729
< char *element_data, size_t size, size_t *len, int *claimed)
---
> struct sbuf *sb, int *claimed)
766d731
< int error;
774,776d738
< error = mac_lomac_to_string(element_data, size, len, mac_lomac);
< if (error)
< return (error);
778,779c740
< *len = strlen(element_data);
< return (0);
---
> return (mac_lomac_to_string(sb, mac_lomac));