Skip to content

Commit 253050f

Browse files
WOnder93pcmoore
authored andcommitted
selinux: factor out loop body from filename_trans_read()
It simplifies cleanup in the error path. This will be extra useful in later patch. Signed-off-by: Ondrej Mosnacek <[email protected]> Acked-by: Stephen Smalley <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 4ca54d3 commit 253050f

File tree

1 file changed

+63
-59
lines changed

1 file changed

+63
-59
lines changed

security/selinux/ss/policydb.c

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,88 +1880,92 @@ static int range_read(struct policydb *p, void *fp)
18801880
return rc;
18811881
}
18821882

1883-
static int filename_trans_read(struct policydb *p, void *fp)
1883+
static int filename_trans_read_one(struct policydb *p, void *fp)
18841884
{
18851885
struct filename_trans *ft;
1886-
struct filename_trans_datum *otype;
1887-
char *name;
1888-
u32 nel, len;
1886+
struct filename_trans_datum *otype = NULL;
1887+
char *name = NULL;
1888+
u32 len;
18891889
__le32 buf[4];
1890-
int rc, i;
1890+
int rc;
18911891

1892-
if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1893-
return 0;
1892+
ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1893+
if (!ft)
1894+
return -ENOMEM;
1895+
1896+
rc = -ENOMEM;
1897+
otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1898+
if (!otype)
1899+
goto out;
18941900

1901+
/* length of the path component string */
18951902
rc = next_entry(buf, fp, sizeof(u32));
18961903
if (rc)
1897-
return rc;
1898-
nel = le32_to_cpu(buf[0]);
1899-
1900-
for (i = 0; i < nel; i++) {
1901-
otype = NULL;
1902-
name = NULL;
1903-
1904-
rc = -ENOMEM;
1905-
ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1906-
if (!ft)
1907-
goto out;
1908-
1909-
rc = -ENOMEM;
1910-
otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1911-
if (!otype)
1912-
goto out;
1913-
1914-
/* length of the path component string */
1915-
rc = next_entry(buf, fp, sizeof(u32));
1916-
if (rc)
1917-
goto out;
1918-
len = le32_to_cpu(buf[0]);
1904+
goto out;
1905+
len = le32_to_cpu(buf[0]);
19191906

1920-
/* path component string */
1921-
rc = str_read(&name, GFP_KERNEL, fp, len);
1922-
if (rc)
1923-
goto out;
1907+
/* path component string */
1908+
rc = str_read(&name, GFP_KERNEL, fp, len);
1909+
if (rc)
1910+
goto out;
19241911

1925-
ft->name = name;
1912+
ft->name = name;
19261913

1927-
rc = next_entry(buf, fp, sizeof(u32) * 4);
1928-
if (rc)
1929-
goto out;
1914+
rc = next_entry(buf, fp, sizeof(u32) * 4);
1915+
if (rc)
1916+
goto out;
19301917

1931-
ft->stype = le32_to_cpu(buf[0]);
1932-
ft->ttype = le32_to_cpu(buf[1]);
1933-
ft->tclass = le32_to_cpu(buf[2]);
1918+
ft->stype = le32_to_cpu(buf[0]);
1919+
ft->ttype = le32_to_cpu(buf[1]);
1920+
ft->tclass = le32_to_cpu(buf[2]);
19341921

1935-
otype->otype = le32_to_cpu(buf[3]);
1922+
otype->otype = le32_to_cpu(buf[3]);
19361923

1937-
rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1938-
if (rc)
1939-
goto out;
1924+
rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1925+
if (rc)
1926+
goto out;
19401927

1941-
rc = hashtab_insert(p->filename_trans, ft, otype);
1942-
if (rc) {
1943-
/*
1944-
* Do not return -EEXIST to the caller, or the system
1945-
* will not boot.
1946-
*/
1947-
if (rc != -EEXIST)
1948-
goto out;
1949-
/* But free memory to avoid memory leak. */
1950-
kfree(ft);
1951-
kfree(name);
1952-
kfree(otype);
1953-
}
1928+
rc = hashtab_insert(p->filename_trans, ft, otype);
1929+
if (rc) {
1930+
/*
1931+
* Do not return -EEXIST to the caller, or the system
1932+
* will not boot.
1933+
*/
1934+
if (rc == -EEXIST)
1935+
rc = 0;
1936+
goto out;
19541937
}
1955-
hash_eval(p->filename_trans, "filenametr");
19561938
return 0;
19571939
out:
19581940
kfree(ft);
19591941
kfree(name);
19601942
kfree(otype);
1961-
19621943
return rc;
19631944
}
19641945

1946+
static int filename_trans_read(struct policydb *p, void *fp)
1947+
{
1948+
u32 nel;
1949+
__le32 buf[1];
1950+
int rc, i;
1951+
1952+
if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1953+
return 0;
1954+
1955+
rc = next_entry(buf, fp, sizeof(u32));
1956+
if (rc)
1957+
return rc;
1958+
nel = le32_to_cpu(buf[0]);
1959+
1960+
for (i = 0; i < nel; i++) {
1961+
rc = filename_trans_read_one(p, fp);
1962+
if (rc)
1963+
return rc;
1964+
}
1965+
hash_eval(p->filename_trans, "filenametr");
1966+
return 0;
1967+
}
1968+
19651969
static int genfs_read(struct policydb *p, void *fp)
19661970
{
19671971
int i, j, rc;

0 commit comments

Comments
 (0)