Generally, foreigners are not allowed to buy guns in the United States. But there are exceptions written into federal law, which may explain how the Saudi flight student who shot three servicemen to death at the Pensacola naval base was able to purchase a weapon.

For example, a foreigner who manages to obtain a state hunting license and can show proof of residency in that state can legally buy a gun.

"It seems every day we find a new loophole," said Adam Winkler, a professor at the University of California, Los Angeles, School of Law and an expert on gun laws and politics.

Authorities have not disclosed precisely how 21-year-old Mohammed Alshamrani, a Royal Saudi Air Force officer undergoing months of flight training at the Florida military base, obtained the Glock 9 mm handgun he used in the attack Friday that ended with him being killed. But the FBI said it was purchased legally in Florida.

In the aftermath of the rampage, which the FBI is treating as a terrorist attack, Florida Gov. Ron DeSantis questioned whether foreigners should be allowed to buy guns. The Republican governor said he supports the Second Amendment but it "does not apply to Saudi Arabians."

Law enforcement officials from nations friendly to the United States who are here on official business as well as foreigners who have entered the U.S. through the visa waiver program are among those exempted from the laws against foreigners buying weapons.

In Florida, like many other states, foreigners and nonresidents can buy a hunting license. Getting one requires no background check, and it can be done online. With that license in hand, a foreigner can then purchase a firearm, provided the would-be buyer can show the dealer proof that he or she is living in the state.

It is at that point that the person would have to undergo a background check.

There have been instances of foreigners seeking to exploit American gun laws.

In one case in 2017, more than half a dozen Chinese students at the University of Arizona obtained hunting licenses and then purchased firearms. In that particular case, it did not appear to be for nefarious purposes but out of a desire to do something not allowed in their home country: possess a gun.

That same year, a Canadian man intent on carrying out an attack at the Flint, Michigan, airport twice tried to buy a weapon at a gun show, expecting it wouldn't require a background check. When that failed, he bought a knife and used it in an attack at the airport that injured two people.

The attacker, who was born in Tunisia and traveled to the U.S. with his Canadian passport, was sentenced to life in prison. At his sentencing, he told the judge: "Do I regret what I did? Never. I regret I didn't get that machine gun. I regret I didn't kill that cop."

UCLA's Winkler said these cases, underscored by the Pensacola attack, expose failings in American gun laws.

"Terrorists see our lax gun laws as an opening for terrorist attacks, that they don't need to use bombs, they don't need to commit suicide and blow themselves up. All they need to do is come to America and have easy access to guns," he said.

Gun-rights supporters said there are valid reasons for allowing foreigners to obtain guns for hunting. Just as Americans travel overseas to hunt big game, foreigners are keen to travel to the U.S. to bag animals that don't exist in their home countries.

"There is a vibrant hunting industry that happens here in the United States," said Mark Oliva, spokesman for the National Shooting Sports Foundation. "There are species that are iconic to North America."

That said, NSSF, which represents the gun industry, is also trying to find out more about how Alshamrani got his weapon and whether any laws were broken when he obtained it.

Obtaining the hunting license wouldn't guarantee a foreigner a gun; buying one would still require demonstrating valid residency and passing a background check.

Still, gun-control advocates suggest there are limits to what a background check might flag when looking into the past of a foreigner. In the U.S., people prohibited from purchasing a firearm include convicted felons, domestic abusers, substance abusers and those involuntarily committed to a mental institution.

"Obviously our background check system is largely not going to pick up criminal histories from abroad," said David Chipman, a retired agent with the federal Bureau of Alcohol, Tobacco, Firearms and Explosive who is now a senior policy adviser with Giffords, the gun-control group.

"There are obviously loopholes in our gun laws," he said. "We live in a country where the priority is easy access to the sale of guns because of a powerful lobby that has set it up that way because they want to profit from it."

Lisa Marie Pane reports for the Associated Press.